Search in sources :

Example 91 with BeanDefinitionBuilder

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

the class WireTapParser method parse.

@Override
public String parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(WireTap.class);
    String targetRef = element.getAttribute("channel");
    if (!StringUtils.hasText(targetRef)) {
        parserContext.getReaderContext().error("The 'channel' attribute is required.", element);
    }
    builder.addConstructorArgReference(targetRef);
    String selectorRef = element.getAttribute("selector");
    String selectorExpression = element.getAttribute("selector-expression");
    if (StringUtils.hasText(selectorRef) && StringUtils.hasText(selectorExpression)) {
        parserContext.getReaderContext().error("Only one of 'selector' or 'selector-expression' is allowed", source);
    }
    if (StringUtils.hasText(selectorRef)) {
        builder.addConstructorArgReference(selectorRef);
    } else if (StringUtils.hasText(selectorExpression)) {
        BeanDefinitionBuilder expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
        expressionBuilder.addConstructorArgValue(selectorExpression);
        BeanDefinitionBuilder eemsBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingSelector.class);
        eemsBuilder.addConstructorArgValue(expressionBuilder.getBeanDefinition());
        builder.addConstructorArgValue(eemsBuilder.getBeanDefinition());
    }
    String timeout = element.getAttribute("timeout");
    if (StringUtils.hasText(timeout)) {
        builder.addPropertyValue("timeout", Long.parseLong(timeout));
    }
    String id = element.getAttribute("id");
    if (StringUtils.hasText(id)) {
        BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(builder.getBeanDefinition(), id), parserContext.getRegistry());
        return id;
    }
    return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) ExpressionEvaluatingSelector(org.springframework.integration.filter.ExpressionEvaluatingSelector) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Example 92 with BeanDefinitionBuilder

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

the class DatatypeChannelTests method conversionServiceReferenceOverridesDefault.

@Test
public void conversionServiceReferenceOverridesDefault() {
    GenericApplicationContext context = new GenericApplicationContext();
    Converter<Boolean, Integer> defaultConverter = new Converter<Boolean, Integer>() {

        @Override
        public Integer convert(Boolean source) {
            return source ? 1 : 0;
        }
    };
    GenericConversionService customConversionService = new DefaultConversionService();
    customConversionService.addConverter(new Converter<Boolean, Integer>() {

        @Override
        public Integer convert(Boolean source) {
            return source ? 99 : -99;
        }
    });
    BeanDefinitionBuilder conversionServiceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ConversionServiceFactoryBean.class);
    conversionServiceBuilder.addPropertyValue("converters", Collections.singleton(defaultConverter));
    context.registerBeanDefinition("conversionService", conversionServiceBuilder.getBeanDefinition());
    BeanDefinitionBuilder messageConverterBuilder = BeanDefinitionBuilder.genericBeanDefinition(DefaultDatatypeChannelMessageConverter.class);
    messageConverterBuilder.addPropertyValue("conversionService", customConversionService);
    context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_DATATYPE_CHANNEL_MESSAGE_CONVERTER_BEAN_NAME, messageConverterBuilder.getBeanDefinition());
    BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(QueueChannel.class);
    channelBuilder.addPropertyValue("datatypes", "java.lang.Integer, java.util.Date");
    context.registerBeanDefinition("testChannel", channelBuilder.getBeanDefinition());
    context.refresh();
    QueueChannel channel = context.getBean("testChannel", QueueChannel.class);
    assertTrue(channel.send(new GenericMessage<Boolean>(Boolean.TRUE)));
    assertEquals(99, channel.receive().getPayload());
    context.close();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DefaultDatatypeChannelMessageConverter(org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter) GenericConverter(org.springframework.core.convert.converter.GenericConverter) Converter(org.springframework.core.convert.converter.Converter) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 93 with BeanDefinitionBuilder

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

the class TcpInboundChannelAdapterParser method doParse.

@Override
protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(TcpReceivingChannelAdapter.class);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, IpAdapterParserUtils.TCP_CONNECTION_FACTORY);
    builder.addPropertyReference("outputChannel", channelName);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-channel", "errorChannel");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IpAdapterParserUtils.CLIENT_MODE);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IpAdapterParserUtils.RETRY_INTERVAL);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, IpAdapterParserUtils.SCHEDULER, "taskScheduler");
    return builder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 94 with BeanDefinitionBuilder

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

the class UdpOutboundChannelAdapterParser method parseUdp.

private BeanDefinitionBuilder parseUdp(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder;
    String multicast = IpAdapterParserUtils.getMulticast(element);
    if (multicast.equals("true")) {
        builder = BeanDefinitionBuilder.genericBeanDefinition(MulticastSendingMessageHandler.class);
        IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IpAdapterParserUtils.MIN_ACKS_SUCCESS, "minAcksForSuccess");
        IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IpAdapterParserUtils.TIME_TO_LIVE, "timeToLive");
    } else {
        builder = BeanDefinitionBuilder.genericBeanDefinition(UnicastSendingMessageHandler.class);
    }
    IpAdapterParserUtils.addDestinationConfigToConstructor(element, builder, parserContext);
    IpAdapterParserUtils.addConstructorValueIfAttributeDefined(builder, element, IpAdapterParserUtils.CHECK_LENGTH);
    IpAdapterParserUtils.addConstructorValueIfAttributeDefined(builder, element, IpAdapterParserUtils.ACK);
    IpAdapterParserUtils.addConstructorValueIfAttributeDefined(builder, element, IpAdapterParserUtils.ACK_HOST);
    IpAdapterParserUtils.addConstructorValueIfAttributeDefined(builder, element, IpAdapterParserUtils.ACK_PORT);
    IpAdapterParserUtils.addConstructorValueIfAttributeDefined(builder, element, IpAdapterParserUtils.ACK_TIMEOUT);
    String ack = element.getAttribute(IpAdapterParserUtils.ACK);
    if (ack.equals("true")) {
        if (!StringUtils.hasText(element.getAttribute(IpAdapterParserUtils.ACK_HOST)) || !StringUtils.hasText(element.getAttribute(IpAdapterParserUtils.ACK_PORT)) || !StringUtils.hasText(element.getAttribute(IpAdapterParserUtils.ACK_TIMEOUT))) {
            parserContext.getReaderContext().error("When " + IpAdapterParserUtils.ACK + " is true, " + IpAdapterParserUtils.ACK_HOST + ", " + IpAdapterParserUtils.ACK_PORT + ", and " + IpAdapterParserUtils.ACK_TIMEOUT + " must be supplied", element);
        }
    }
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IpAdapterParserUtils.RECEIVE_BUFFER_SIZE);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, IpAdapterParserUtils.TASK_EXECUTOR);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "socket-expression", "socketExpressionString");
    return builder;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) MulticastSendingMessageHandler(org.springframework.integration.ip.udp.MulticastSendingMessageHandler) UnicastSendingMessageHandler(org.springframework.integration.ip.udp.UnicastSendingMessageHandler)

Example 95 with BeanDefinitionBuilder

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

the class JmsMessageDrivenEndpointParser method parseMessageListenerContainer.

private String parseMessageListenerContainer(Element element, ParserContext parserContext, BeanDefinition adapterBeanDefinition) {
    String containerClass = element.getAttribute("container-class");
    if (hasExternalContainer(element)) {
        if (StringUtils.hasText(containerClass)) {
            parserContext.getReaderContext().error("Cannot have both 'container' and 'container-class'", element);
        }
        for (String containerAttribute : containerAttributes) {
            if (element.hasAttribute(containerAttribute)) {
                parserContext.getReaderContext().error("The '" + containerAttribute + "' attribute should not be provided when specifying a 'container' reference.", element);
            }
        }
        return element.getAttribute("container");
    }
    // otherwise, we build a DefaultMessageListenerContainer instance
    BeanDefinitionBuilder builder;
    if (StringUtils.hasText(containerClass)) {
        builder = BeanDefinitionBuilder.genericBeanDefinition(containerClass);
    } else {
        builder = BeanDefinitionBuilder.genericBeanDefinition(DefaultMessageListenerContainer.class);
    }
    String destinationAttribute = this.expectReply ? "request-destination" : "destination";
    String destinationNameAttribute = this.expectReply ? "request-destination-name" : "destination-name";
    String pubSubDomainAttribute = this.expectReply ? "request-pub-sub-domain" : "pub-sub-domain";
    String destination = element.getAttribute(destinationAttribute);
    String destinationName = element.getAttribute(destinationNameAttribute);
    boolean hasDestination = StringUtils.hasText(destination);
    boolean hasDestinationName = StringUtils.hasText(destinationName);
    if (hasDestination == hasDestinationName) {
        parserContext.getReaderContext().error("Exactly one of '" + destinationAttribute + "' or '" + destinationNameAttribute + "' is required.", element);
    }
    builder.addPropertyReference(JmsParserUtils.CONNECTION_FACTORY_PROPERTY, JmsParserUtils.determineConnectionFactoryBeanName(element, parserContext));
    if (hasDestination) {
        builder.addPropertyReference("destination", destination);
    } else {
        builder.addPropertyValue("destinationName", destinationName);
        IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, pubSubDomainAttribute, "pubSubDomain");
    }
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "destination-resolver");
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "transaction-manager");
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "task-executor");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "selector", "messageSelector");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "concurrent-consumers");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "max-concurrent-consumers");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "max-messages-per-task");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "receive-timeout");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "recovery-interval");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "idle-consumer-limit");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "idle-task-execution-limit");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "cache-level");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "subscription-durable");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "subscription-shared");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "subscription-name");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "client-id");
    String beanName = adapterBeanNameRoot(element, parserContext, adapterBeanDefinition) + ".container";
    parserContext.getRegistry().registerBeanDefinition(beanName, builder.getBeanDefinition());
    return beanName;
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

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