Search in sources :

Example 81 with BeanDefinitionBuilder

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

the class DefaultInboundChannelAdapterParser method parseExpression.

private BeanMetadataElement parseExpression(String expressionString, Element expressionElement, Element element, ParserContext parserContext) {
    BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingMessageSource.class);
    BeanDefinition expressionDef = null;
    if (StringUtils.hasText(expressionString)) {
        expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
        expressionDef.getConstructorArgumentValues().addGenericArgumentValue(expressionString);
    } else {
        BeanDefinitionBuilder dynamicExpressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DynamicExpression.class);
        String key = expressionElement.getAttribute("key");
        String expressionSourceReference = expressionElement.getAttribute("source");
        dynamicExpressionBuilder.addConstructorArgValue(key);
        dynamicExpressionBuilder.addConstructorArgReference(expressionSourceReference);
        expressionDef = dynamicExpressionBuilder.getBeanDefinition();
    }
    sourceBuilder.addConstructorArgValue(expressionDef);
    sourceBuilder.addConstructorArgValue(null);
    this.parseHeaderExpressions(sourceBuilder, element, parserContext);
    return sourceBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 82 with BeanDefinitionBuilder

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

the class DefaultInboundChannelAdapterParser method parseMethodInvokingSource.

private BeanMetadataElement parseMethodInvokingSource(BeanMetadataElement targetObject, String methodName, Element element, ParserContext parserContext) {
    BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingMessageSource.class);
    sourceBuilder.addPropertyValue("object", targetObject);
    sourceBuilder.addPropertyValue("methodName", methodName);
    this.parseHeaderExpressions(sourceBuilder, element, parserContext);
    return sourceBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 83 with BeanDefinitionBuilder

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

the class DelayerParser method parseHandler.

@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DelayHandler.class);
    String id = element.getAttribute(ID_ATTRIBUTE);
    if (!StringUtils.hasText(id)) {
        parserContext.getReaderContext().error("The 'id' attribute is required.", element);
    }
    String defaultDelay = element.getAttribute("default-delay");
    String expression = element.getAttribute(EXPRESSION_ATTRIBUTE);
    Element expressionElement = DomUtils.getChildElementByTagName(element, "expression");
    boolean hasDefaultDelay = StringUtils.hasText(defaultDelay);
    boolean hasExpression = StringUtils.hasText(expression);
    boolean hasExpressionElement = expressionElement != null;
    if (!(hasDefaultDelay | hasExpression | hasExpressionElement)) {
        parserContext.getReaderContext().error("The 'default-delay' or 'expression' attributes, or 'expression' sub-element should be provided.", element);
    }
    if (hasExpression & hasExpressionElement) {
        parserContext.getReaderContext().error("'expression' attribute and 'expression' sub-element are mutually exclusive.", element);
    }
    builder.addConstructorArgValue(id + ".messageGroupId");
    String scheduler = element.getAttribute("scheduler");
    if (StringUtils.hasText(scheduler)) {
        builder.addConstructorArgReference(scheduler);
    }
    if (hasDefaultDelay) {
        builder.addPropertyValue("defaultDelay", defaultDelay);
    }
    BeanDefinitionBuilder expressionBuilder = null;
    if (hasExpression) {
        expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
        expressionBuilder.addConstructorArgValue(expression);
    } else if (expressionElement != null) {
        expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DynamicExpression.class);
        String key = expressionElement.getAttribute("key");
        String expressionSourceReference = expressionElement.getAttribute("source");
        expressionBuilder.addConstructorArgValue(key);
        expressionBuilder.addConstructorArgReference(expressionSourceReference);
    }
    if (expressionBuilder != null) {
        builder.addPropertyValue("delayExpression", expressionBuilder.getBeanDefinition());
    }
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-store");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-expression-failures");
    Element txElement = DomUtils.getChildElementByTagName(element, "transactional");
    Element adviceChainElement = DomUtils.getChildElementByTagName(element, "advice-chain");
    IntegrationNamespaceUtils.configureAndSetAdviceChainIfPresent(adviceChainElement, txElement, builder.getRawBeanDefinition(), parserContext, "delayedAdviceChain");
    if (txElement != null) {
        element.removeChild(txElement);
    }
    return builder;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) Element(org.w3c.dom.Element)

Example 84 with BeanDefinitionBuilder

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

the class GatewayParser method parse.

@Override
@SuppressWarnings("rawtypes")
public BeanDefinition parse(final Element element, ParserContext parserContext) {
    boolean isNested = parserContext.isNested();
    final Map<String, Object> gatewayAttributes = new HashMap<String, Object>();
    gatewayAttributes.put("name", element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE));
    gatewayAttributes.put("defaultPayloadExpression", element.getAttribute("default-payload-expression"));
    gatewayAttributes.put("defaultRequestChannel", element.getAttribute(isNested ? "request-channel" : "default-request-channel"));
    gatewayAttributes.put("defaultReplyChannel", element.getAttribute(isNested ? "reply-channel" : "default-reply-channel"));
    gatewayAttributes.put("errorChannel", element.getAttribute("error-channel"));
    String asyncExecutor = element.getAttribute("async-executor");
    if (!element.hasAttribute("async-executor") || StringUtils.hasLength(asyncExecutor)) {
        gatewayAttributes.put("asyncExecutor", asyncExecutor);
    } else {
        gatewayAttributes.put("asyncExecutor", null);
    }
    gatewayAttributes.put("mapper", element.getAttribute("mapper"));
    gatewayAttributes.put("defaultReplyTimeout", element.getAttribute(isNested ? "reply-timeout" : "default-reply-timeout"));
    gatewayAttributes.put("defaultRequestTimeout", element.getAttribute(isNested ? "request-timeout" : "default-request-timeout"));
    List<Element> headerElements = DomUtils.getChildElementsByTagName(element, "default-header");
    if (!CollectionUtils.isEmpty(headerElements)) {
        List<Map<String, Object>> headers = new ArrayList<Map<String, Object>>(headerElements.size());
        for (Element e : headerElements) {
            Map<String, Object> header = new HashMap<String, Object>();
            header.put("name", e.getAttribute("name"));
            header.put("value", e.getAttribute("value"));
            header.put("expression", e.getAttribute("expression"));
            headers.add(header);
        }
        gatewayAttributes.put("defaultHeaders", headers.toArray(new Map[headers.size()]));
    }
    List<Element> methodElements = DomUtils.getChildElementsByTagName(element, "method");
    if (!CollectionUtils.isEmpty(methodElements)) {
        Map<String, BeanDefinition> methodMetadataMap = new ManagedMap<String, BeanDefinition>();
        for (Element methodElement : methodElements) {
            String methodName = methodElement.getAttribute("name");
            BeanDefinitionBuilder methodMetadataBuilder = BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodMetadata.class);
            methodMetadataBuilder.addPropertyValue("requestChannelName", methodElement.getAttribute("request-channel"));
            methodMetadataBuilder.addPropertyValue("replyChannelName", methodElement.getAttribute("reply-channel"));
            methodMetadataBuilder.addPropertyValue("requestTimeout", methodElement.getAttribute("request-timeout"));
            methodMetadataBuilder.addPropertyValue("replyTimeout", methodElement.getAttribute("reply-timeout"));
            boolean hasMapper = StringUtils.hasText(element.getAttribute("mapper"));
            Assert.state(!hasMapper || !StringUtils.hasText(element.getAttribute("payload-expression")), "'payload-expression' is not allowed when a 'mapper' is provided");
            IntegrationNamespaceUtils.setValueIfAttributeDefined(methodMetadataBuilder, methodElement, "payload-expression");
            List<Element> invocationHeaders = DomUtils.getChildElementsByTagName(methodElement, "header");
            if (!CollectionUtils.isEmpty(invocationHeaders)) {
                Assert.state(!hasMapper, "header elements are not allowed when a 'mapper' is provided");
                Map<String, Object> headerExpressions = new ManagedMap<String, Object>();
                for (Element headerElement : invocationHeaders) {
                    BeanDefinition expressionDef = IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("value", "expression", parserContext, headerElement, true);
                    headerExpressions.put(headerElement.getAttribute("name"), expressionDef);
                }
                methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions);
            }
            methodMetadataMap.put(methodName, methodMetadataBuilder.getBeanDefinition());
        }
        gatewayAttributes.put("methods", methodMetadataMap);
    }
    gatewayAttributes.put("serviceInterface", element.getAttribute("service-interface"));
    BeanDefinitionHolder gatewayHolder = this.registrar.parse(gatewayAttributes);
    if (isNested) {
        return gatewayHolder.getBeanDefinition();
    } else {
        BeanDefinitionReaderUtils.registerBeanDefinition(gatewayHolder, parserContext.getRegistry());
        return null;
    }
}
Also used : HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ManagedMap(org.springframework.beans.factory.support.ManagedMap) HashMap(java.util.HashMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 85 with BeanDefinitionBuilder

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

the class ChainParser method parseHandler.

@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MessageHandlerChain.class);
    if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE))) {
        this.logger.info("It is useful to provide an explicit 'id' attribute on 'chain' elements " + "to simplify the identification of child elements in logs etc.");
    }
    String chainHandlerId = this.resolveId(element, builder.getRawBeanDefinition(), parserContext);
    List<BeanMetadataElement> handlerList = new ManagedList<BeanMetadataElement>();
    Set<String> handlerBeanNameSet = new HashSet<String>();
    NodeList children = element.getChildNodes();
    int childOrder = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE && !"poller".equals(child.getLocalName())) {
            BeanMetadataElement childBeanMetadata = this.parseChild(chainHandlerId, (Element) child, childOrder++, parserContext, builder.getBeanDefinition());
            if (childBeanMetadata instanceof RuntimeBeanReference) {
                String handlerBeanName = ((RuntimeBeanReference) childBeanMetadata).getBeanName();
                if (!handlerBeanNameSet.add(handlerBeanName)) {
                    parserContext.getReaderContext().error("A bean definition is already registered for " + "beanName: '" + handlerBeanName + "' within the current <chain>.", element);
                    return null;
                }
            }
            if ("gateway".equals(child.getLocalName())) {
                BeanDefinitionBuilder gwBuilder = BeanDefinitionBuilder.genericBeanDefinition(IntegrationConfigUtils.BASE_PACKAGE + ".gateway.RequestReplyMessageHandlerAdapter");
                gwBuilder.addConstructorArgValue(childBeanMetadata);
                handlerList.add(gwBuilder.getBeanDefinition());
            } else {
                handlerList.add(childBeanMetadata);
            }
        }
    }
    builder.addPropertyValue("handlers", handlerList);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
    return builder;
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) HashSet(java.util.HashSet)

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