diff --git a/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBusAware.java b/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBusAware.java new file mode 100644 index 0000000000..0846a76d60 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBusAware.java @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2008 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.bus; + +/** + * Interface to be implemented by classes which need access to the {@link MessageBus}. + * @author Marius Bogoevici + * + */ +public interface MessageBusAware { + + public void setMessageBus(MessageBus messageBus); + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBusAwareBeanPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBusAwareBeanPostProcessor.java new file mode 100644 index 0000000000..724b2d511c --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBusAwareBeanPostProcessor.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2008 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.bus; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.util.Assert; + +/** + * A bean post processor which injects all {@link MessageBusAware} beans with a + * reference to the {@link MessageBus}. + * + * @author Marius Bogoevici + * + */ +public class MessageBusAwareBeanPostProcessor implements BeanPostProcessor { + + private final MessageBus messageBus; + + public MessageBusAwareBeanPostProcessor(MessageBus messageBus) { + Assert.notNull(messageBus, "'messageBus' must not be null"); + this.messageBus = messageBus; + } + + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof MessageBusAware) { + ((MessageBusAware) bean).setMessageBus(messageBus); + } + return bean; + } + + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; + } + + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/bus/MessageBusTests.java b/spring-integration-core/src/test/java/org/springframework/integration/bus/MessageBusTests.java index d78523005d..94165074cc 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/bus/MessageBusTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/bus/MessageBusTests.java @@ -25,10 +25,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.Test; - import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.integration.ConfigurationException; import org.springframework.integration.channel.DispatcherPolicy; import org.springframework.integration.channel.MessageChannel; import org.springframework.integration.channel.QueueChannel; @@ -219,17 +217,11 @@ public class MessageBusTests { assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass()); } - @Test + @Test(expected = BeanCreationException.class) public void testMultipleMessageBusBeans() { - boolean exceptionThrown = false; - try { - new ClassPathXmlApplicationContext("multipleMessageBusBeans.xml", this.getClass()); - } - catch (BeanCreationException e) { - exceptionThrown = true; - assertEquals(ConfigurationException.class, e.getCause().getClass()); - } - assertTrue(exceptionThrown); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("multipleMessageBusBeans.xml", + this.getClass()); + } @Test @@ -259,6 +251,12 @@ public class MessageBusTests { assertEquals("handler should have received error message", 0, latch.getCount()); } + @Test + public void testMessageBusAwareImpl() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("messageBusTests.xml", this.getClass()); + TestMessageBusAwareImpl messageBusAwareBean = (TestMessageBusAwareImpl) context.getBean("messageBusAwareBean"); + assertTrue(messageBusAwareBean.getMessageBus() == context.getBean("bus")); + } private static class FailingSource implements PollableSource { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml b/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml index 6fa2b70033..4f3328ee31 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml @@ -21,5 +21,10 @@ - + + + + + +