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

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

Introduction

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

Prototype

@Nullable
    public final BeanDefinition getContainingBeanDefinition() 

Source Link

Usage

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

/** 
 * called by nested tags to push inner beans into the enclosing {@link BehaviorTrackingManagerImpl}.
 * @return <span>true if the inner bean was added to an enclosing {@link BehaviorTrackingManagerImpl}.  Otherwise, the bean definition
 * is not nested inside a &lt;bt:manager&gt; tag and therefore will have to be registered as a global bean in the application
 * context.</span>/*from   w  w w  .  jav a 2 s.  c  om*/
 */
protected static boolean registerNestedBean(BeanDefinitionHolder nested, String parentProperty,
        ParserContext parserContext) {
    //add parsed inner bean element to containing manager definition; e.g. persister or SessionContext impls.
    CompositeComponentDefinition parent = parserContext.getContainingComponent();
    if (parent instanceof ManagerComponentDefinition) {
        //we are nested; add to enclosing bean def.
        ManagerComponentDefinition mcd = (ManagerComponentDefinition) parent;
        BeanDefinition managerDef = parserContext.getContainingBeanDefinition();

        MutablePropertyValues props = managerDef.getPropertyValues();
        PropertyValue current = props.getPropertyValue(parentProperty);
        boolean innerBean = true;

        if (current != null) {
            //if the original value is a reference, replace it with an alias to the nested bean definition.
            //this means the nested bean takes the place of the default definition
            //in other places where it might be referenced, as well as during mbean export
            Object value = current.getValue();
            DefaultListableBeanFactory factory = mcd.getTemplateFactory();
            if (value instanceof RuntimeBeanReference) {
                String ref = ((RuntimeBeanReference) value).getBeanName();

                if (factory.getBeanDefinition(ref) == nested.getBeanDefinition()) {
                    //the nested definition is the same as the default definition
                    //by reference, so we don't need to make it an inner bean definition.
                    innerBean = false;
                }
            }
        }
        if (innerBean)
            props.addPropertyValue(parentProperty, nested);
        mcd.addNestedProperty(parentProperty);
        return true;
    }
    //bean is not nested inside bt:manager
    return false;
}

From source file:com.predic8.membrane.annot.AbstractNamespaceHandler.java

private BeanDefinitionParser findParserForElement(Element element, ParserContext parserContext) {
    String localName = parserContext.getDelegate().getLocalName(element);

    if (parserContext.getContainingBeanDefinition() != null) {
        String beanClassName = parserContext.getContainingBeanDefinition().getBeanClassName();
        Map<String, BeanDefinitionParser> parentLocalParsers = localParsers.get(beanClassName);
        if (parentLocalParsers != null) {
            BeanDefinitionParser parser = parentLocalParsers.get(localName);
            if (parser != null)
                return parser;
        }//  w w  w.  ja  v a 2  s .co  m
    }

    BeanDefinitionParser parser = this.parsers.get(localName);
    if (parser == null) {
        parserContext.getReaderContext()
                .fatal("Cannot locate BeanDefinitionParser for element [" + localName + "]", element);
    }
    return parser;
}

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

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

    //compile required constructor arguments from attributes and nested elements.  first up, event type.
    builder.addConstructorArg(element.getAttribute(ATT_EVENT_TYPE));

    //manager ID from enclosing tag, or ref attribute.
    String managerId = parserContext.isNested()
            ? (String) parserContext.getContainingBeanDefinition().getAttribute("id")
            : element.getAttribute(BtNamespaceConstants.ATT_TRACKING_MANAGER);
    builder.addConstructorArgReference(managerId);

    //parameter list to include in event data, if any.
    String paramList = element.getAttribute(ATT_PARAMETERS);
    builder.addConstructorArg(parseList(paramList));

    //parameter list to include in event name, if any
    String nameList = element.getAttribute(ATT_NAME_PARAMETERS);
    builder.addConstructorArg(parseList(nameList));

    //URI patterns, if any.  can be specified as attribute or nested elements.
    ArrayList<Pattern> accum = new ArrayList<Pattern>();
    if (element.hasAttribute(ATT_URI_PATTERN))
        accum.add(Pattern.compile(element.getAttribute(ATT_URI_PATTERN)));

    NodeList nl = element.getElementsByTagNameNS("*", ATT_URI_PATTERN);
    for (int i = 0; i < nl.getLength(); ++i) {
        Element e = (Element) nl.item(i);
        String pattern = e.getTextContent();
        if (StringUtils.hasText(pattern))
            accum.add(Pattern.compile(pattern));
    }/*from  ww  w .  j  a  v a  2 s .c  o m*/

    if (accum.isEmpty())
        builder.addConstructorArg(null);
    else
        builder.addConstructorArg(accum.toArray(new Pattern[accum.size()]));

    if (parserContext.isNested())
        parserContext.getReaderContext().registerWithGeneratedName(builder.getBeanDefinition());
}

From source file:com.apporiented.spring.override.AbstractGenericBeanDefinitionParser.java

/**
 * Creates a {@link org.springframework.beans.factory.support.BeanDefinitionBuilder} instance for the
 * bean class and passes it to the//from w  ww  .  j  a  v  a2s. c  om
 * {@link #doParse} strategy method.
 * @param element the element that is to be parsed into a single BeanDefinition
 * @param parserContext the object encapsulating the current state of the parsing process
 * @return the BeanDefinition resulting from the parsing of the supplied {@link org.w3c.dom.Element}
 * @throws IllegalStateException if the bean {@link Class} returned from
 * bean class is <code>null</code>
 * @see #doParse
 */
protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(beanClass);
    builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
    if (parserContext.isNested()) {
        // Inner bean definition must receive same scope as containing bean.
        builder.setScope(parserContext.getContainingBeanDefinition().getScope());
    }
    if (parserContext.isDefaultLazyInit()) {
        // Default-lazy-init applies to custom bean definitions as well.
        builder.setLazyInit(true);
    }
    builder.setAutowireMode(autowireMode);
    builder.setFactoryMethod(factoryMethod);
    doParse(element, parserContext, builder);
    return builder.getBeanDefinition();
}

From source file:net.phoenix.thrift.xml.ComplexBeanDefinitionParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
    String parentName = getParentName(element);
    if (parentName != null) {
        builder.getRawBeanDefinition().setParentName(parentName);
    }/*  w w w  .  j  a  va2s.  c  o  m*/
    Class<?> beanClass = getBeanClass(element);
    if (beanClass != null) {
        builder.getRawBeanDefinition().setBeanClass(beanClass);
    } else {
        String beanClassName = getBeanClassName(element);
        if (beanClassName != null) {
            builder.getRawBeanDefinition().setBeanClassName(beanClassName);
        }
    }
    builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
    if (parserContext.isNested()) {
        // Inner bean definition must receive same scope as containing bean.
        builder.setScope(parserContext.getContainingBeanDefinition().getScope());
    }
    if (parserContext.isDefaultLazyInit()) {
        // Default-lazy-init applies to custom bean definitions as well.
        builder.setLazyInit(true);
    }
    preParse(element, parserContext, builder);
    AbstractBeanDefinition target = builder.getBeanDefinition();
    postParse(element, parserContext, target);
    return target;
}

From source file:org.mule.transport.zmq.config.InboundEndpointDefinitionParser.java

public BeanDefinition parseChild(Element element, ParserContext parserContent) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder
            .rootBeanDefinition(InboundEndpointMessageSource.class.getName());
    String configRef = element.getAttribute("config-ref");
    if ((configRef != null) && (!StringUtils.isBlank(configRef))) {
        builder.addPropertyValue("moduleObject", configRef);
    }/*from w ww. j  a v  a  2  s.co  m*/
    if (element.hasAttribute("exchange-pattern")) {
        builder.addPropertyValue("exchangePattern", element.getAttribute("exchange-pattern"));
    }
    if (element.hasAttribute("socket-operation")) {
        builder.addPropertyValue("socketOperation", element.getAttribute("socket-operation"));
    }
    if ((element.getAttribute("address") != null) && (!StringUtils.isBlank(element.getAttribute("address")))) {
        builder.addPropertyValue("address", element.getAttribute("address"));
    }
    if ((element.getAttribute("filter") != null) && (!StringUtils.isBlank(element.getAttribute("filter")))) {
        builder.addPropertyValue("filter", element.getAttribute("filter"));
    }
    if ((element.getAttribute("identity") != null)
            && (!StringUtils.isBlank(element.getAttribute("identity")))) {
        builder.addPropertyValue("identity", element.getAttribute("identity"));
    }
    if ((element.getAttribute("multipart") != null)
            && (!StringUtils.isBlank(element.getAttribute("multipart")))) {
        builder.addPropertyValue("multipart", element.getAttribute("multipart"));
    }
    if ((element.getAttribute("retryMax") != null)
            && (!StringUtils.isBlank(element.getAttribute("retryMax")))) {
        builder.addPropertyValue("retryMax", element.getAttribute("retryMax"));
    }

    BeanDefinition definition = builder.getBeanDefinition();
    definition.setAttribute(MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE, Boolean.TRUE);
    MutablePropertyValues propertyValues = parserContent.getContainingBeanDefinition().getPropertyValues();

    propertyValues.addPropertyValue("messageSource", definition);

    return definition;
}

From source file:org.mule.transport.zmq.config.OutboundEndpointDefinitionParser.java

public BeanDefinition parse(Element element, ParserContext parserContent) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder
            .rootBeanDefinition(OutboundEndpointMessageProcessor.class.getName());
    String configRef = element.getAttribute("config-ref");
    if ((configRef != null) && (!StringUtils.isBlank(configRef))) {
        builder.addPropertyValue("moduleObject", configRef);
    }/* www. ja va 2s  .  c o  m*/
    if ((element.getAttribute("payload-ref") != null)
            && (!StringUtils.isBlank(element.getAttribute("payload-ref")))) {
        if (element.getAttribute("payload-ref").startsWith("#")) {
            builder.addPropertyValue("payload", element.getAttribute("payload-ref"));
        } else {
            builder.addPropertyValue("payload", (("#[registry:" + element.getAttribute("payload-ref")) + "]"));
        }
    }
    if ((element.getAttribute("retryMax") != null)
            && (!StringUtils.isBlank(element.getAttribute("retryMax")))) {
        builder.addPropertyValue("retryMax", element.getAttribute("retryMax"));
    }
    if (element.hasAttribute("exchange-pattern")) {
        builder.addPropertyValue("exchangePattern", element.getAttribute("exchange-pattern"));
    }
    if (element.hasAttribute("socket-operation")) {
        builder.addPropertyValue("socketOperation", element.getAttribute("socket-operation"));
    }
    if ((element.getAttribute("address") != null) && (!StringUtils.isBlank(element.getAttribute("address")))) {
        builder.addPropertyValue("address", element.getAttribute("address"));
    }
    if ((element.getAttribute("filter") != null) && (!StringUtils.isBlank(element.getAttribute("filter")))) {
        builder.addPropertyValue("filter", element.getAttribute("filter"));
    }
    if ((element.getAttribute("identity") != null)
            && (!StringUtils.isBlank(element.getAttribute("identity")))) {
        builder.addPropertyValue("identity", element.getAttribute("identity"));
    }
    if ((element.getAttribute("multipart") != null)
            && (!StringUtils.isBlank(element.getAttribute("multipart")))) {
        builder.addPropertyValue("multipart", element.getAttribute("multipart"));
    }
    BeanDefinition definition = builder.getBeanDefinition();
    definition.setAttribute(MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE, Boolean.TRUE);
    MutablePropertyValues propertyValues = parserContent.getContainingBeanDefinition().getPropertyValues();
    if (parserContent.getContainingBeanDefinition().getBeanClassName()
            .equals("org.mule.config.spring.factories.PollingMessageSourceFactoryBean")) {
        propertyValues.addPropertyValue("messageProcessor", definition);
    } else {
        if (parserContent.getContainingBeanDefinition().getBeanClassName()
                .equals("org.mule.enricher.MessageEnricher")) {
            propertyValues.addPropertyValue("enrichmentMessageProcessor", definition);
        } else {
            PropertyValue messageProcessors = propertyValues.getPropertyValue("messageProcessors");
            if ((messageProcessors == null) || (messageProcessors.getValue() == null)) {
                propertyValues.addPropertyValue("messageProcessors", new ManagedList());
            }
            List listMessageProcessors = ((List) propertyValues.getPropertyValue("messageProcessors")
                    .getValue());
            listMessageProcessors.add(definition);
        }
    }
    return definition;
}

From source file:org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser.java

/**
 * Creates a {@link BeanDefinitionBuilder} instance for the {@link #getBeanClass
 * bean Class} and passes it to the {@link #doParse} strategy method.
 *
 * @param element the element that is to be parsed into a single BeanDefinition
 * @param context the object encapsulating the current state of the parsing
 *            process// ww w.j  a  va 2  s.c  o m
 * @return the BeanDefinition resulting from the parsing of the supplied
 *         {@link Element}
 * @throws IllegalStateException if the bean {@link Class} returned from
 *             {@link #getBeanClass(org.w3c.dom.Element)} is <code>null</code>
 * @see #doParse
 */
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) {
    preProcess(element);
    setParserContext(context);
    setRegistry(context.getRegistry());
    checkElementNameUnique(element);
    Class<?> beanClass = getClassInternal(element);
    BeanDefinitionBuilder builder = createBeanDefinitionBuilder(element, beanClass);
    builder.getRawBeanDefinition().setSource(context.extractSource(element));
    builder.setScope(isSingleton() ? BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);

    // Marker for MULE-4813
    // We don't want lifcycle for the following from spring
    if (!Component.class.isAssignableFrom(beanClass) && !MessageSource.class.isAssignableFrom(beanClass)
            && !OutboundRouterCollection.class.isAssignableFrom(beanClass)
            && !OutboundRouter.class.isAssignableFrom(beanClass)
            && !AbstractMessageProcessorOwner.class.isAssignableFrom(beanClass)
            && !MessagingExceptionHandler.class.isAssignableFrom(beanClass)
            && !Transformer.class.isAssignableFrom(beanClass)) {
        if (Initialisable.class.isAssignableFrom(beanClass)) {
            builder.setInitMethodName(Initialisable.PHASE_NAME);
        }

        if (Disposable.class.isAssignableFrom(beanClass)) {
            builder.setDestroyMethodName(Disposable.PHASE_NAME);
        }
    }

    if (context.isNested()) {
        // Inner bean definition must receive same singleton status as containing bean.
        builder.setScope(context.getContainingBeanDefinition().isSingleton() ? BeanDefinition.SCOPE_SINGLETON
                : BeanDefinition.SCOPE_PROTOTYPE);
    }

    doParse(element, context, builder);
    return builder.getBeanDefinition();
}

From source file:org.mule.module.extension.internal.config.OperationBeanDefinitionParser.java

private void attachProcessorDefinition(ParserContext parserContext, BeanDefinition definition) {
    MutablePropertyValues propertyValues = parserContext.getContainingBeanDefinition().getPropertyValues();
    if (parserContext.getContainingBeanDefinition().getBeanClassName()
            .equals(PollingMessageSourceFactoryBean.class.getName())) {

        propertyValues.addPropertyValue("messageProcessor", definition);
    } else {/*www  . j  a  va 2 s .c o m*/
        if (parserContext.getContainingBeanDefinition().getBeanClassName()
                .equals(MessageEnricher.class.getName())) {
            propertyValues.addPropertyValue("enrichmentMessageProcessor", definition);
        } else {
            PropertyValue messageProcessors = propertyValues.getPropertyValue("messageProcessors");
            if ((messageProcessors == null) || (messageProcessors.getValue() == null)) {
                propertyValues.addPropertyValue("messageProcessors", new ManagedList());
            }
            List listMessageProcessors = ((List) propertyValues.getPropertyValue("messageProcessors")
                    .getValue());
            listMessageProcessors.add(definition);
        }
    }
}

From source file:org.mule.module.extension.internal.config.OperationBeanDefinitionParser.java

private void attachSourceDefinition(ParserContext parserContext, BeanDefinition definition) {
    MutablePropertyValues propertyValues = parserContext.getContainingBeanDefinition().getPropertyValues();
    propertyValues.addPropertyValue("messageSource", definition);
}