diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java index 6bfb810afc..b2d4a3081e 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java @@ -19,9 +19,15 @@ package org.springframework.integration.adapter.jms.config; import org.w3c.dom.Element; import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.config.RuntimeBeanReference; +import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.adapter.jms.JmsTargetAdapter; +import org.springframework.integration.bus.Subscription; +import org.springframework.integration.endpoint.DefaultMessageEndpoint; import org.springframework.util.StringUtils; /** @@ -45,11 +51,13 @@ public class JmsTargetAdapterParser extends AbstractSingleBeanDefinitionParser { private static final String CHANNEL_ATTRIBUTE = "channel"; - private static final String CHANNEL_PROPERTY = "channel"; + private static final String HANDLER_PROPERTY = "handler"; + + private static final String SUBSCRIPTION_PROPERTY = "subscription"; protected Class getBeanClass(Element element) { - return JmsTargetAdapter.class; + return DefaultMessageEndpoint.class; } protected boolean shouldGenerateId() { @@ -60,27 +68,32 @@ public class JmsTargetAdapterParser extends AbstractSingleBeanDefinitionParser { return true; } - protected void doParse(Element element, BeanDefinitionBuilder builder) { + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { String jmsTemplate = element.getAttribute(JMS_TEMPLATE_ATTRIBUTE); String connectionFactory = element.getAttribute(CONNECTION_FACTORY_ATTRIBUTE); String destination = element.getAttribute(DESTINATION_ATTRIBUTE); + RootBeanDefinition adapterDef = new RootBeanDefinition(JmsTargetAdapter.class); if (StringUtils.hasText(jmsTemplate)) { if (StringUtils.hasText(connectionFactory) || StringUtils.hasText(destination)) { throw new BeanCreationException("when providing a 'jms-template' reference, neither " + "'connection-factory' or 'destination' should be provided."); } - builder.addPropertyReference(JMS_TEMPLATE_PROPERTY, jmsTemplate); + adapterDef.getPropertyValues().addPropertyValue(JMS_TEMPLATE_PROPERTY, new RuntimeBeanReference(jmsTemplate)); } else if (StringUtils.hasText(connectionFactory) && StringUtils.hasText(destination)) { - builder.addPropertyReference(CONNECTION_FACTORY_PROPERTY, connectionFactory); - builder.addPropertyReference(DESTINATION_PROPERTY, destination); + adapterDef.getPropertyValues().addPropertyValue(CONNECTION_FACTORY_PROPERTY, new RuntimeBeanReference(connectionFactory)); + adapterDef.getPropertyValues().addPropertyValue(DESTINATION_PROPERTY, new RuntimeBeanReference(destination)); } else { throw new BeanCreationException("either a 'jms-template' reference or both " + "'connection-factory' and 'destination' references must be provided."); } String channel = element.getAttribute(CHANNEL_ATTRIBUTE); - builder.addPropertyReference(CHANNEL_PROPERTY, channel); + Subscription subscription = new Subscription(channel); + String adapterBeanName = parserContext.getReaderContext().generateBeanName(adapterDef); + parserContext.registerBeanComponent(new BeanComponentDefinition(adapterDef, adapterBeanName)); + builder.addPropertyReference(HANDLER_PROPERTY, adapterBeanName); + builder.addPropertyValue(SUBSCRIPTION_PROPERTY, subscription); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParserTests.java new file mode 100644 index 0000000000..218a00c31b --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParserTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.adapter.jms.config; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.adapter.jms.JmsTargetAdapter; +import org.springframework.integration.endpoint.DefaultMessageEndpoint; + +/** + * @author Mark Fisher + */ +public class JmsTargetAdapterParserTests { + + @Test + public void testTargetAdapterWithConnectionFactoryAndDestination() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "targetAdapterWithConnectionFactoryAndDestination.xml", this.getClass()); + DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter"); + assertEquals(JmsTargetAdapter.class, endpoint.getHandler().getClass()); + assertEquals("adapter", endpoint.getName()); + assertEquals("testChannel", endpoint.getSubscription().getChannelName()); + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/jms/config/targetAdapterWithConnectionFactoryAndDestination.xml b/spring-integration-core/src/test/java/org/springframework/integration/adapter/jms/config/targetAdapterWithConnectionFactoryAndDestination.xml new file mode 100644 index 0000000000..7c5aadf267 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/jms/config/targetAdapterWithConnectionFactoryAndDestination.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + +