Example usage for org.springframework.beans.factory.xml ParserContext popContainingComponent

List of usage examples for org.springframework.beans.factory.xml ParserContext popContainingComponent

Introduction

In this page you can find the example usage for org.springframework.beans.factory.xml ParserContext popContainingComponent.

Prototype

public CompositeComponentDefinition popContainingComponent() 

Source Link

Usage

From source file:com.mtgi.analytics.aop.config.v11.BtPersisterChainBeanDefinitionParser.java

@Override
@SuppressWarnings("unchecked")
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    //propagate shared template factory into sub-components if necessary.  this would not be
    //necessary if parserContext gave us access to the entire component stack instead of just the
    //top.  In that case, deeply nested children could search up the stack until they found
    //the template.  perhaps a future version of the Spring API will support this.
    DefaultListableBeanFactory template = findEnclosingTemplateFactory(parserContext);
    if (template != null)
        parserContext.pushContainingComponent(new TemplateComponentDefinition(element.getNodeName(),
                parserContext.extractSource(element), template));

    try {//from w  ww.j a v a  2 s .c o  m
        //parse delegate persister definitions
        ManagedList persisters = new ManagedList();
        persisters.setSource(element);

        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); ++i) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                String namespaceUri = node.getNamespaceURI();
                NamespaceHandler handler = parserContext.getReaderContext().getNamespaceHandlerResolver()
                        .resolve(namespaceUri);
                Object def = handler == null
                        ? parserContext.getDelegate().parsePropertySubElement((Element) node,
                                builder.getRawBeanDefinition())
                        : handler.parse((Element) node, parserContext);
                persisters.add(def);
            }
        }
        builder.addPropertyValue("delegates", persisters);
    } finally {
        if (template != null)
            parserContext.popContainingComponent();
    }

    //register persister implementation with parent.
    if (parserContext.isNested()) {
        AbstractBeanDefinition def = builder.getBeanDefinition();
        String id = element.hasAttribute("id") ? element.getAttribute("id")
                : parserContext.getReaderContext().generateBeanName(def);
        BeanDefinitionHolder holder = new BeanDefinitionHolder(def, id);
        BtManagerBeanDefinitionParser.registerNestedBean(holder, "persister", parserContext);
    }
}

From source file:com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser.java

/**
 * <p>Load the template BeanDefinition and call {@link #transform(ConfigurableListableBeanFactory, BeanDefinition, Element, ParserContext)}
 * to apply runtime configuration value to it.  <code>builder</code> will be configured to instantiate the bean
 * in the Spring context that we are parsing.</p>
 * /*from  w ww . j  av a2 s  .  co  m*/
 * <p>During parsing, an instance of {@link TemplateBeanDefinitionParser.TemplateComponentDefinition} is pushed onto <code>ParserContext</code> so
 * that nested tags can access the enclosing template configuration with a call to {@link #findEnclosingTemplateFactory(ParserContext)}.
 * Subclasses can override {@link #newComponentDefinition(String, Object, DefaultListableBeanFactory)} to provide a 
 * subclass of {@link TemplateBeanDefinitionParser.TemplateComponentDefinition} to the parser context if necessary.</p>
 */
@Override
protected final void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    //if we have multiple nested bean definitions, we only parse the template factory
    //once.  this allows configuration changes made by enclosing bean parsers to be inherited
    //by contained beans, which is quite useful.
    DefaultListableBeanFactory templateFactory = findEnclosingTemplateFactory(parserContext);
    TemplateComponentDefinition tcd = null;
    if (templateFactory == null) {

        //no nesting -- load the template XML configuration from the classpath.
        final BeanFactory parentFactory = (BeanFactory) parserContext.getRegistry();
        templateFactory = new DefaultListableBeanFactory(parentFactory);

        //load template bean definitions
        DefaultResourceLoader loader = new DefaultResourceLoader();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(templateFactory);
        reader.setResourceLoader(loader);
        reader.setEntityResolver(new ResourceEntityResolver(loader));
        reader.loadBeanDefinitions(templateResource);

        //propagate factory post-processors from the source factory into the template
        //factory.
        BeanDefinition ppChain = new RootBeanDefinition(ChainingBeanFactoryPostProcessor.class);
        ppChain.getPropertyValues().addPropertyValue("targetFactory", templateFactory);
        parserContext.getReaderContext().registerWithGeneratedName(ppChain);

        //push component definition onto the parser stack for the benefit of
        //nested bean definitions.
        tcd = newComponentDefinition(element.getNodeName(), parserContext.extractSource(element),
                templateFactory);
        parserContext.pushContainingComponent(tcd);
    }

    try {
        //allow subclasses to apply overrides to the template bean definition.
        BeanDefinition def = templateFactory.getBeanDefinition(templateId);
        transform(templateFactory, def, element, parserContext);

        //setup our factory bean to instantiate the modified bean definition upon request.
        builder.addPropertyValue("beanFactory", templateFactory);
        builder.addPropertyValue("beanName", templateId);
        builder.getRawBeanDefinition().setAttribute("id", def.getAttribute("id"));

    } finally {
        if (tcd != null)
            parserContext.popContainingComponent();
    }
}