Search in sources :

Example 81 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class AbstractConsumerEndpointParser method parseInternal.

@Override
protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder handlerBuilder = this.parseHandler(element, parserContext);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(handlerBuilder, element, "output-channel");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "order");
    Element txElement = DomUtils.getChildElementByTagName(element, "transactional");
    Element adviceChainElement = DomUtils.getChildElementByTagName(element, IntegrationNamespaceUtils.REQUEST_HANDLER_ADVICE_CHAIN);
    @SuppressWarnings("rawtypes") ManagedList adviceChain = IntegrationNamespaceUtils.configureAdviceChain(adviceChainElement, txElement, true, handlerBuilder.getRawBeanDefinition(), parserContext);
    if (!CollectionUtils.isEmpty(adviceChain)) {
        handlerBuilder.addPropertyValue("adviceChain", adviceChain);
    }
    AbstractBeanDefinition handlerBeanDefinition = handlerBuilder.getBeanDefinition();
    String inputChannelAttributeName = this.getInputChannelAttributeName();
    boolean hasInputChannelAttribute = element.hasAttribute(inputChannelAttributeName);
    if (parserContext.isNested()) {
        String elementDescription = IntegrationNamespaceUtils.createElementDescription(element);
        if (hasInputChannelAttribute) {
            parserContext.getReaderContext().error("The '" + inputChannelAttributeName + "' attribute isn't allowed for a nested (e.g. inside a <chain/>) endpoint element: " + elementDescription + ".", element);
        }
        if (!replyChannelInChainAllowed(element)) {
            if (StringUtils.hasText(element.getAttribute("reply-channel"))) {
                parserContext.getReaderContext().error("The 'reply-channel' attribute isn't" + " allowed for a nested (e.g. inside a <chain/>) outbound gateway element: " + elementDescription + ".", element);
            }
        }
        return handlerBeanDefinition;
    } else {
        if (!hasInputChannelAttribute) {
            String elementDescription = IntegrationNamespaceUtils.createElementDescription(element);
            parserContext.getReaderContext().error("The '" + inputChannelAttributeName + "' attribute is required for the top-level endpoint element: " + elementDescription + ".", element);
        }
    }
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class);
    if (!CollectionUtils.isEmpty(adviceChain)) {
        builder.addPropertyValue("adviceChain", adviceChain);
    }
    String handlerBeanName = BeanDefinitionReaderUtils.generateBeanName(handlerBeanDefinition, parserContext.getRegistry());
    String[] handlerAlias = IntegrationNamespaceUtils.generateAlias(element);
    parserContext.registerBeanComponent(new BeanComponentDefinition(handlerBeanDefinition, handlerBeanName, handlerAlias));
    builder.addPropertyReference("handler", handlerBeanName);
    String inputChannelName = element.getAttribute(inputChannelAttributeName);
    if (!parserContext.getRegistry().containsBeanDefinition(inputChannelName)) {
        if (parserContext.getRegistry().containsBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME)) {
            BeanDefinition channelRegistry = parserContext.getRegistry().getBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME);
            ConstructorArgumentValues caValues = channelRegistry.getConstructorArgumentValues();
            ValueHolder vh = caValues.getArgumentValue(0, Collection.class);
            if (vh == null) {
                // although it should never happen if it does we can fix it
                caValues.addIndexedArgumentValue(0, new ManagedSet<String>());
            }
            @SuppressWarnings("unchecked") Collection<String> channelCandidateNames = (Collection<String>) caValues.getArgumentValue(0, Collection.class).getValue();
            channelCandidateNames.add(inputChannelName);
        } else {
            parserContext.getReaderContext().error("Failed to locate '" + IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME + "'", parserContext.getRegistry());
        }
    }
    IntegrationNamespaceUtils.checkAndConfigureFixedSubscriberChannel(element, parserContext, inputChannelName, handlerBeanName);
    builder.addPropertyValue("inputChannelName", inputChannelName);
    List<Element> pollerElementList = DomUtils.getChildElementsByTagName(element, "poller");
    if (!CollectionUtils.isEmpty(pollerElementList)) {
        if (pollerElementList.size() != 1) {
            parserContext.getReaderContext().error("at most one poller element may be configured for an endpoint", element);
        }
        IntegrationNamespaceUtils.configurePollerMetadata(pollerElementList.get(0), builder, parserContext);
    }
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.ROLE);
    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    String beanName = this.resolveId(element, beanDefinition, parserContext);
    parserContext.registerBeanComponent(new BeanComponentDefinition(beanDefinition, beanName));
    return null;
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Collection(java.util.Collection) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Example 82 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class AbstractRouterParser method parseRouter.

protected final BeanDefinition parseRouter(Element element, ParserContext parserContext) {
    BeanDefinition beanDefinition = this.doParseRouter(element, parserContext);
    if (beanDefinition != null) {
        // check if mapping is provided otherwise returned values will be treated as channel names
        List<Element> mappingElements = DomUtils.getChildElementsByTagName(element, "mapping");
        if (!CollectionUtils.isEmpty(mappingElements)) {
            ManagedMap<String, String> channelMappings = new ManagedMap<String, String>();
            for (Element mappingElement : mappingElements) {
                String key = mappingElement.getAttribute(this.getMappingKeyAttributeName());
                channelMappings.put(key, mappingElement.getAttribute("channel"));
            }
            beanDefinition.getPropertyValues().add("channelMappings", channelMappings);
        }
    }
    return beanDefinition;
}
Also used : Element(org.w3c.dom.Element) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 83 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class AbstractRouterParser method parseHandler.

@Override
protected final BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(RouterFactoryBean.class);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "default-output-channel");
    if (StringUtils.hasText(element.getAttribute("timeout")) && StringUtils.hasText(element.getAttribute("send-timeout"))) {
        parserContext.getReaderContext().error("Only one of 'timeout' and 'send-timeout' is allowed", element);
    }
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "timeout", "sendTimeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "resolution-required");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "apply-sequence");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-send-failures");
    BeanDefinition targetRouterBeanDefinition = this.parseRouter(element, parserContext);
    builder.addPropertyValue("targetObject", targetRouterBeanDefinition);
    return builder;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 84 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class RecipientListRouterParser method doParseRouter.

@Override
protected BeanDefinition doParseRouter(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder recipientListRouterBuilder = BeanDefinitionBuilder.genericBeanDefinition(RecipientListRouter.class);
    List<Element> childElements = DomUtils.getChildElementsByTagName(element, "recipient");
    ManagedList<BeanDefinition> recipientList = new ManagedList<BeanDefinition>();
    for (Element childElement : childElements) {
        BeanDefinitionBuilder recipientBuilder = BeanDefinitionBuilder.genericBeanDefinition(Recipient.class);
        recipientBuilder.addConstructorArgReference(childElement.getAttribute("channel"));
        String expression = childElement.getAttribute("selector-expression");
        if (StringUtils.hasText(expression)) {
            BeanDefinition selectorDef = new RootBeanDefinition(ExpressionEvaluatingSelector.class);
            selectorDef.getConstructorArgumentValues().addGenericArgumentValue(expression);
            String selectorBeanName = parserContext.getReaderContext().registerWithGeneratedName(selectorDef);
            recipientBuilder.addConstructorArgReference(selectorBeanName);
        }
        recipientList.add(recipientBuilder.getBeanDefinition());
    }
    if (recipientList.size() > 0) {
        recipientListRouterBuilder.addPropertyValue("recipients", recipientList);
    }
    return recipientListRouterBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 85 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class ScatterGatherParser method parseHandler.

@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
    String scatterChannel = element.getAttribute("scatter-channel");
    boolean hasScatterChannel = StringUtils.hasText(scatterChannel);
    Element scatterer = DomUtils.getChildElementByTagName(element, "scatterer");
    boolean hasScatterer = scatterer != null;
    if (hasScatterChannel & hasScatterer) {
        parserContext.getReaderContext().error("'scatter-channel' attribute and 'scatterer' sub-element are mutually exclusive", element);
    }
    if (!hasScatterChannel & !hasScatterer) {
        parserContext.getReaderContext().error("The 'scatter-channel' attribute or 'scatterer' sub-element must be specified", element);
    }
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ScatterGatherHandler.class);
    AbstractBeanDefinition scatterGatherDefinition = builder.getRawBeanDefinition();
    String id = resolveId(element, scatterGatherDefinition, parserContext);
    if (hasScatterChannel) {
        builder.addConstructorArgReference(scatterChannel);
    } else {
        BeanDefinition scattererDefinition = null;
        if (!hasScatterer) {
            scattererDefinition = new RootBeanDefinition(RecipientListRouter.class);
        } else {
            scattererDefinition = SCATTERER_PARSER.parse(scatterer, new ParserContext(parserContext.getReaderContext(), parserContext.getDelegate(), scatterGatherDefinition));
        }
        String scattererId = id + ".scatterer";
        if (hasScatterer && scatterer.hasAttribute(ID_ATTRIBUTE)) {
            scattererId = scatterer.getAttribute(ID_ATTRIBUTE);
        }
        parserContext.getRegistry().registerBeanDefinition(scattererId, scattererDefinition);
        builder.addConstructorArgValue(new RuntimeBeanReference(scattererId));
    }
    Element gatherer = DomUtils.getChildElementByTagName(element, "gatherer");
    BeanDefinition gathererDefinition = null;
    if (gatherer == null) {
        try {
            gatherer = documentBuilderFactory.newDocumentBuilder().newDocument().createElement("aggregator");
        } catch (ParserConfigurationException e) {
            parserContext.getReaderContext().error(e.getMessage(), element);
        }
    }
    gathererDefinition = GATHERER_PARSER.parse(gatherer, new ParserContext(parserContext.getReaderContext(), parserContext.getDelegate(), scatterGatherDefinition));
    String gathererId = id + ".gatherer";
    if (gatherer != null && gatherer.hasAttribute(ID_ATTRIBUTE)) {
        gathererId = gatherer.getAttribute(ID_ATTRIBUTE);
    }
    parserContext.getRegistry().registerBeanDefinition(gathererId, gathererDefinition);
    builder.addConstructorArgValue(new RuntimeBeanReference(gathererId));
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "gather-channel");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "gather-timeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "requires-reply");
    return builder;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ParserContext(org.springframework.beans.factory.xml.ParserContext) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RecipientListRouter(org.springframework.integration.router.RecipientListRouter)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)601 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)116 Test (org.junit.jupiter.api.Test)112 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)72 Test (org.junit.Test)66 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)53 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)41 ManagedList (org.springframework.beans.factory.support.ManagedList)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)35 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)34 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)34 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)31 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 Method (java.lang.reflect.Method)28 ArrayList (java.util.ArrayList)28 Map (java.util.Map)27