diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/DefaultMessageEndpoint.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/DefaultMessageEndpoint.java index 53519dbcd4..325c1507c0 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/DefaultMessageEndpoint.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/DefaultMessageEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * 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. @@ -185,6 +185,12 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA } public void afterPropertiesSet() { + if (this.handler == null) { + return; + } + if (this.handler instanceof ChannelRegistryAware) { + ((ChannelRegistryAware) this.handler).setChannelRegistry(this.channelRegistry); + } if (this.concurrencyPolicy != null || this.handler instanceof ConcurrentHandler) { if (!(this.handler instanceof ConcurrentHandler)) { int capacity = concurrencyPolicy.getQueueCapacity(); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java index 55693b2292..16d770fffe 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * 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. @@ -60,6 +60,10 @@ public abstract class AbstractMessageHandlerAdapter implements MessageHandler this.object = object; } + protected Object getObject() { + return this.object; + } + public void setMethodName(String methodName) { Assert.notNull(methodName, "'methodName' must not be null"); this.methodName = methodName; @@ -82,6 +86,7 @@ public abstract class AbstractMessageHandlerAdapter implements MessageHandler this.invoker = new SimpleMethodInvoker(this.object, this.methodName); this.initialized = true; } + this.initialize(); } public final Message handle(Message message) { @@ -109,6 +114,12 @@ public abstract class AbstractMessageHandlerAdapter implements MessageHandler protected void validate() { } + /** + * Subclasses may override this method to provide additional initialization. + */ + protected void initialize() { + } + protected Message createReplyMessage(Object payload, MessageHeader originalMessageHeader) { return new GenericMessage(payload, originalMessageHeader); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/router/RouterMessageHandlerAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/router/RouterMessageHandlerAdapter.java index 49accd0325..d86e43ab75 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/router/RouterMessageHandlerAdapter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/router/RouterMessageHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * 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. @@ -65,6 +65,14 @@ public class RouterMessageHandlerAdapter extends AbstractMessageHandlerAdapter i this.channelRegistry = channelRegistry; } + @Override + protected void initialize() { + Object target = this.getObject(); + if (target != null && this.channelRegistry != null && (target instanceof ChannelRegistryAware)) { + ((ChannelRegistryAware) target).setChannelRegistry(this.channelRegistry); + } + } + @Override protected Object doHandle(Message message, SimpleMethodInvoker invoker) { if (method.getParameterTypes().length != 1) { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/router/RouterMessageHandlerAdapterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/router/RouterMessageHandlerAdapterTests.java index 395c88ff10..65158f2c62 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/router/RouterMessageHandlerAdapterTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/router/RouterMessageHandlerAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * 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. @@ -30,6 +30,7 @@ import org.junit.Test; import org.springframework.integration.MessagingConfigurationException; import org.springframework.integration.channel.ChannelRegistry; +import org.springframework.integration.channel.ChannelRegistryAware; import org.springframework.integration.channel.DefaultChannelRegistry; import org.springframework.integration.channel.MessageChannel; import org.springframework.integration.channel.SimpleChannel; @@ -440,6 +441,26 @@ public class RouterMessageHandlerAdapterTests { assertNull(result6); } + @Test + public void testChannelRegistryAwareTarget() throws Exception { + SimpleChannel fooChannel = new SimpleChannel(); + ChannelRegistry channelRegistry = new DefaultChannelRegistry(); + channelRegistry.registerChannel("foo-channel", fooChannel); + ChannelRegistryAwareTestBean testBean = new ChannelRegistryAwareTestBean(); + Method routingMethod = testBean.getClass().getMethod("route", String.class); + Map attribs = new ConcurrentHashMap(); + RouterMessageHandlerAdapter adapter = new RouterMessageHandlerAdapter(testBean, routingMethod, attribs); + adapter.setChannelRegistry(channelRegistry); + assertNull(testBean.getChannelRegistry()); + adapter.afterPropertiesSet(); + assertNotNull(testBean.getChannelRegistry()); + Message message = new StringMessage("foo-channel"); + adapter.handle(message); + Message result = fooChannel.receive(0); + assertNotNull(result); + assertEquals("foo-channel", result.getPayload()); + } + public static class SingleChannelNameRoutingTestBean { @@ -552,4 +573,22 @@ public class RouterMessageHandlerAdapterTests { } } + + public static class ChannelRegistryAwareTestBean implements ChannelRegistryAware { + + private ChannelRegistry channelRegistry; + + public void setChannelRegistry(ChannelRegistry channelRegistry) { + this.channelRegistry = channelRegistry; + } + + public ChannelRegistry getChannelRegistry() { + return this.channelRegistry; + } + + public MessageChannel route(String channelName) { + return this.channelRegistry.lookupChannel(channelName); + } + } + }