diff --git a/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsMessageDrivenEndpointParser.java b/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsMessageDrivenEndpointParser.java index 4af00e3e5a..9baa236c7b 100644 --- a/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsMessageDrivenEndpointParser.java +++ b/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsMessageDrivenEndpointParser.java @@ -39,6 +39,7 @@ import org.springframework.util.StringUtils; * @author Mark Fisher * @author Michael Bannister * @author Gary Russell + * @author Artem Bilan */ public class JmsMessageDrivenEndpointParser extends AbstractSingleBeanDefinitionParser { @@ -121,6 +122,13 @@ public class JmsMessageDrivenEndpointParser extends AbstractSingleBeanDefinition builder.addConstructorArgValue(hasExternalContainer(element)); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE); + String role = element.getAttribute(IntegrationNamespaceUtils.ROLE); + if (StringUtils.hasText(role)) { + if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE))) { + parserContext.getReaderContext().error("When using 'role', 'id' is required", element); + } + IntegrationNamespaceUtils.putLifecycleInRole(role, element.getAttribute(ID_ATTRIBUTE), parserContext); + } IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "acknowledge", "sessionAcknowledgeMode"); } @@ -170,8 +178,8 @@ public class JmsMessageDrivenEndpointParser extends AbstractSingleBeanDefinition IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, pubSubDomainAttribute, "pubSubDomain"); } - if (StringUtils.hasText(element.getAttribute("subsription-name")) - && StringUtils.hasText(element.getAttribute("durable-subsription-name"))) { + if (StringUtils.hasText(element.getAttribute("subscription-name")) + && StringUtils.hasText(element.getAttribute("durable-subscription-name"))) { parserContext.getReaderContext().error( "Only one of 'subscription-name' or 'durable-subscription-name' is allowed.", element); } diff --git a/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/JmsInboundGatewayParserTests.java b/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/JmsInboundGatewayParserTests.java index a284ee5ace..83928f685c 100644 --- a/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/JmsInboundGatewayParserTests.java +++ b/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/JmsInboundGatewayParserTests.java @@ -19,6 +19,7 @@ package org.springframework.integration.jms.config; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.util.Properties; @@ -31,9 +32,11 @@ import org.junit.Test; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.NotReadablePropertyException; import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.context.SmartLifecycle; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.history.MessageHistory; import org.springframework.integration.jms.JmsMessageDrivenEndpoint; +import org.springframework.integration.support.SmartLifecycleRoleController; import org.springframework.integration.test.util.TestUtils; import org.springframework.jms.connection.JmsTransactionManager; import org.springframework.jms.core.JmsTemplate; @@ -42,10 +45,12 @@ import org.springframework.jms.listener.DefaultMessageListenerContainer; import org.springframework.jms.support.destination.JmsDestinationAccessor; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; +import org.springframework.util.MultiValueMap; /** * @author Mark Fisher * @author Gary Russell + * @author Artem Bilan */ public class JmsInboundGatewayParserTests { @@ -315,7 +320,14 @@ public class JmsInboundGatewayParserTests { public void testGatewayWithContainerReference() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "inboundGatewayWithContainerReference.xml", this.getClass()); - JmsMessageDrivenEndpoint gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithContainerReference"); + JmsMessageDrivenEndpoint gateway = + context.getBean("gatewayWithContainerReference", JmsMessageDrivenEndpoint.class); + SmartLifecycleRoleController roleController = context.getBean(SmartLifecycleRoleController.class); + @SuppressWarnings("unchecked") + MultiValueMap lifecycles = + TestUtils.getPropertyValue(roleController, "lifecycles", MultiValueMap.class); + assertTrue(lifecycles.containsKey("foo")); + assertSame(gateway, lifecycles.getFirst("foo")); gateway.start(); AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer"); diff --git a/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/inboundGatewayWithContainerReference.xml b/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/inboundGatewayWithContainerReference.xml index 1261a7a06c..b19376e44b 100644 --- a/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/inboundGatewayWithContainerReference.xml +++ b/spring-integration-jms/src/test/java/org/springframework/integration/jms/config/inboundGatewayWithContainerReference.xml @@ -17,6 +17,7 @@ diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketInboundChannelAdapterParser.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketInboundChannelAdapterParser.java index c984c657d2..cc2441ee54 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketInboundChannelAdapterParser.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketInboundChannelAdapterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,60 +18,32 @@ package org.springframework.integration.websocket.config; import org.w3c.dom.Element; -import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; -import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.xml.AbstractChannelAdapterParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.integration.websocket.inbound.WebSocketInboundChannelAdapter; -import org.springframework.util.StringUtils; /** - * The {@link AbstractSingleBeanDefinitionParser} implementation for - * the {@code } element. + * The {@link AbstractChannelAdapterParser} implementation for + * the {@code } element. * * @author Artem Bilan * @since 4.1 */ -public class WebSocketInboundChannelAdapterParser extends AbstractSingleBeanDefinitionParser { +public class WebSocketInboundChannelAdapterParser extends AbstractChannelAdapterParser { @Override - protected Class getBeanClass(Element element) { - return WebSocketInboundChannelAdapter.class; - } - - @Override - protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) - throws BeanDefinitionStoreException { - String id = super.resolveId(element, definition, parserContext); - - if (!element.hasAttribute("channel")) { - // the created channel will get the 'id', so the adapter's bean name includes a suffix - id = id + ".adapter"; - } - if (!StringUtils.hasText(id)) { - id = BeanDefinitionReaderUtils.generateBeanName(definition, parserContext.getRegistry()); - } - - return id; - } - - @Override - protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(WebSocketInboundChannelAdapter.class); WebSocketAdapterParsingUtils.configureWebSocketAdapter(builder, parserContext, element); - String channelName = element.getAttribute("channel"); - if (!StringUtils.hasText(channelName)) { - channelName = IntegrationNamespaceUtils.createDirectChannel(element, parserContext); - } builder.addPropertyReference("outputChannel", channelName); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-channel"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "payload-type"); - IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP); - IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "use-broker"); + return builder.getBeanDefinition(); } }