Search in sources :

Example 71 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method createExpressionDefIfAttributeDefined.

public static BeanDefinition createExpressionDefIfAttributeDefined(String expressionElementName, Element element) {
    Assert.hasText(expressionElementName, "'expressionElementName' must no be empty");
    String expressionElementValue = element.getAttribute(expressionElementName);
    if (StringUtils.hasText(expressionElementValue)) {
        BeanDefinitionBuilder expressionDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
        expressionDefBuilder.addConstructorArgValue(expressionElementValue);
        return expressionDefBuilder.getRawBeanDefinition();
    }
    return null;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 72 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder 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 73 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class AbstractOutboundGatewayParser method parseHandler.

@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(this.getGatewayClassName(element));
    String url = this.parseUrl(element, parserContext);
    builder.addConstructorArgValue(url);
    String replyChannel = element.getAttribute("reply-channel");
    if (StringUtils.hasText(replyChannel)) {
        builder.addPropertyReference("replyChannel", replyChannel);
    }
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "requires-reply");
    this.postProcessGateway(builder, element, parserContext);
    return builder;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 74 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class AbstractTransformerParser method parseHandler.

@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MessageTransformingHandler.class);
    BeanDefinitionBuilder transformerBuilder = BeanDefinitionBuilder.genericBeanDefinition(this.getTransformerClassName());
    this.parseTransformer(element, parserContext, transformerBuilder);
    String transformerBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(transformerBuilder.getBeanDefinition(), parserContext.getRegistry());
    builder.addConstructorArgReference(transformerBeanName);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
    return builder;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 75 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder 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)

Aggregations

BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)524 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)116 Element (org.w3c.dom.Element)96 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)61 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)58 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)53 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)47 ManagedList (org.springframework.beans.factory.support.ManagedList)33 ManagedMap (org.springframework.beans.factory.support.ManagedMap)26 HashMap (java.util.HashMap)19 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)19 Test (org.junit.jupiter.api.Test)16 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)16 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)12 Node (org.w3c.dom.Node)12 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)10 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)10