diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/AnnotationDrivenParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/AnnotationDrivenParser.java index a6773ae0bd..f56c9d4915 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/AnnotationDrivenParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/AnnotationDrivenParser.java @@ -24,7 +24,6 @@ import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.integration.endpoint.annotation.MessageEndpointAnnotationPostProcessor; /** * Parser for the annotation-driven element of the integration diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/ComponentConfigurer.java b/spring-integration-core/src/main/java/org/springframework/integration/config/ComponentConfigurer.java index 477b6f982f..76ab509ba4 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/ComponentConfigurer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/ComponentConfigurer.java @@ -23,8 +23,8 @@ import org.springframework.beans.factory.support.DefaultBeanNameGenerator; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.integration.endpoint.GenericMessageEndpoint; import org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter; -import org.springframework.integration.endpoint.MessageHandlerAdapter; import org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter; +import org.springframework.integration.handler.DefaultMessageHandlerAdapter; import org.springframework.util.Assert; /** @@ -47,7 +47,7 @@ public class ComponentConfigurer { public String serviceActivator(String inputChannel, String outputChannel, String objectRef, String method) { RootBeanDefinition endpointDef = new RootBeanDefinition(GenericMessageEndpoint.class); - RootBeanDefinition adapterDef = new RootBeanDefinition(MessageHandlerAdapter.class); + RootBeanDefinition adapterDef = new RootBeanDefinition(DefaultMessageHandlerAdapter.class); adapterDef.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(objectRef)); adapterDef.getPropertyValues().addPropertyValue("method", method); String adapterName = beanNameGenerator.generateBeanName(adapterDef, this.registry); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java index 5df6f98c43..1d6a83b338 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java @@ -28,7 +28,7 @@ import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.bus.ConsumerPolicy; import org.springframework.integration.endpoint.GenericMessageEndpoint; -import org.springframework.integration.endpoint.MessageHandlerAdapter; +import org.springframework.integration.handler.DefaultMessageHandlerAdapter; import org.springframework.util.StringUtils; /** @@ -82,7 +82,7 @@ public class EndpointParser implements BeanDefinitionParser { if (StringUtils.hasText(handlerRef)) { String handlerMethod = element.getAttribute(HANDLER_METHOD_ATTRIBUTE); if (StringUtils.hasText(handlerMethod)) { - BeanDefinition handlerAdapterDef = new RootBeanDefinition(MessageHandlerAdapter.class); + BeanDefinition handlerAdapterDef = new RootBeanDefinition(DefaultMessageHandlerAdapter.class); handlerAdapterDef.getPropertyValues().addPropertyValue(OBJECT_PROPERTY, new RuntimeBeanReference(handlerRef)); handlerAdapterDef.getPropertyValues().addPropertyValue(METHOD_PROPERTY, handlerMethod); String adapterBeanName = parserContext.getReaderContext().generateBeanName(handlerAdapterDef); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/MessageEndpointAnnotationPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/config/MessageEndpointAnnotationPostProcessor.java similarity index 93% rename from spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/MessageEndpointAnnotationPostProcessor.java rename to spring-integration-core/src/main/java/org/springframework/integration/config/MessageEndpointAnnotationPostProcessor.java index 7c042299ad..252c9d2ccc 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/MessageEndpointAnnotationPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/MessageEndpointAnnotationPostProcessor.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration.endpoint.annotation; +package org.springframework.integration.config; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @@ -34,8 +34,14 @@ import org.springframework.integration.bus.MessageBus; import org.springframework.integration.endpoint.GenericMessageEndpoint; import org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter; import org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter; +import org.springframework.integration.endpoint.annotation.DefaultOutput; +import org.springframework.integration.endpoint.annotation.MessageEndpoint; +import org.springframework.integration.endpoint.annotation.Polled; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.handler.MessageHandlerChain; +import org.springframework.integration.handler.annotation.AnnotationHandlerCreator; +import org.springframework.integration.handler.annotation.DefaultAnnotationHandlerCreator; +import org.springframework.integration.handler.annotation.Handler; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/SubscriberAnnotationPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/config/SubscriberAnnotationPostProcessor.java index 5fd760657a..cf73bebe7b 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/SubscriberAnnotationPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/SubscriberAnnotationPostProcessor.java @@ -28,8 +28,8 @@ import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.integration.bus.MessageBus; import org.springframework.integration.endpoint.GenericMessageEndpoint; -import org.springframework.integration.endpoint.MessageHandlerAdapter; import org.springframework.integration.endpoint.annotation.Subscriber; +import org.springframework.integration.handler.DefaultMessageHandlerAdapter; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; @@ -81,7 +81,7 @@ public class SubscriberAnnotationPostProcessor implements BeanPostProcessor { Annotation annotation = method.getAnnotation(subscriberAnnotationType); if (annotation != null) { String channelName = (String) AnnotationUtils.getValue(annotation, channelNameAttribute); - MessageHandlerAdapter adapter = new MessageHandlerAdapter(); + DefaultMessageHandlerAdapter adapter = new DefaultMessageHandlerAdapter(); adapter.setMethod(method.getName()); adapter.setObject(bean); adapter.afterPropertiesSet(); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageHandlerAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java similarity index 56% rename from spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageHandlerAdapter.java rename to spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java index 944612c8dc..ee3a19533d 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageHandlerAdapter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java @@ -14,25 +14,27 @@ * limitations under the License. */ -package org.springframework.integration.endpoint; +package org.springframework.integration.handler; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.Ordered; -import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.endpoint.SimpleMethodInvoker; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageMapper; import org.springframework.integration.message.SimplePayloadMessageMapper; import org.springframework.util.Assert; /** - * An implementation of {@link MessageHandler} that invokes the specified method - * on the provided target object. It then uses a {@link MessageMapper} strategy - * for converting the object to a {@link Message}. If the method has a non-null - * return value, a reply message will be generated by the mapper. + * Base implementation of the {@link MessageHandler} interface that creates an + * invoker for the specified method and target object. It also accepts an + * implementation of the {@link MessageMapper} strategy which it exposes to + * subclasses for converting the {@link Message} to an object. Likewise, if the + * method has a non-null return value, a reply message will be generated by the + * mapper. * * @author Mark Fisher */ -public class MessageHandlerAdapter implements MessageHandler, Ordered, InitializingBean { +public abstract class AbstractMessageHandlerAdapter implements MessageHandler, Ordered, InitializingBean { private T object; @@ -50,11 +52,28 @@ public class MessageHandlerAdapter implements MessageHandler, Ordered, Initia this.object = object; } + protected Object getObject() { + return this.object; + } + public void setMethod(String method) { Assert.notNull(method, "'method' must not be null"); this.method = method; } + public String getMethod() { + return this.method; + } + + public void setMapper(MessageMapper mapper) { + Assert.notNull(mapper, "'mapper' must not be null"); + this.mapper = mapper; + } + + protected MessageMapper getMapper() { + return this.mapper; + } + public void setOrder(int order) { this.order = order; } @@ -68,11 +87,21 @@ public class MessageHandlerAdapter implements MessageHandler, Ordered, Initia } public Message handle(Message message) { - Object result = this.invoker.invokeMethod(this.mapper.fromMessage(message)); + Object result = this.doHandle(message, invoker); if (result != null) { + if (result instanceof Message) { + return (Message) result; + } return this.mapper.toMessage(result); } return null; } + /** + * Subclasses must implement this method. The invoker has been created for + * the provided target object and method. May return an object of type + * {@link Message}, else rely on the message mapper to convert. + */ + protected abstract Object doHandle(Message message, SimpleMethodInvoker invoker); + } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/DefaultMessageHandlerAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/DefaultMessageHandlerAdapter.java new file mode 100644 index 0000000000..54827f9270 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/DefaultMessageHandlerAdapter.java @@ -0,0 +1,40 @@ +/* + * 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.handler; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.Ordered; +import org.springframework.integration.endpoint.SimpleMethodInvoker; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageMapper; + +/** + * An implementation of {@link MessageHandler} that invokes the specified method + * on the provided target object. It then uses a {@link MessageMapper} strategy + * for converting the object to a {@link Message}. If the method has a non-null + * return value, a reply message will be generated by the mapper. + * + * @author Mark Fisher + */ +public class DefaultMessageHandlerAdapter extends AbstractMessageHandlerAdapter + implements Ordered, InitializingBean { + + public Object doHandle(Message message, SimpleMethodInvoker invoker) { + return invoker.invokeMethod(this.getMapper().fromMessage(message)); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/AnnotationHandlerCreator.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/AnnotationHandlerCreator.java similarity index 94% rename from spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/AnnotationHandlerCreator.java rename to spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/AnnotationHandlerCreator.java index 7189c24ece..b85029646f 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/AnnotationHandlerCreator.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/AnnotationHandlerCreator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration.endpoint.annotation; +package org.springframework.integration.handler.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Method; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/DefaultAnnotationHandlerCreator.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/DefaultAnnotationHandlerCreator.java similarity index 83% rename from spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/DefaultAnnotationHandlerCreator.java rename to spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/DefaultAnnotationHandlerCreator.java index aa4cdcc306..a921fbb0b5 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/DefaultAnnotationHandlerCreator.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/DefaultAnnotationHandlerCreator.java @@ -14,19 +14,19 @@ * limitations under the License. */ -package org.springframework.integration.endpoint.annotation; +package org.springframework.integration.handler.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; -import org.springframework.integration.endpoint.MessageHandlerAdapter; import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.handler.DefaultMessageHandlerAdapter; /** * Default implementation of the handler creator strategy that creates a - * {@link MessageHandlerAdapter} for the provided object and method. This + * {@link DefaultMessageHandlerAdapter} for the provided object and method. This * version does not even consider the annotation itself. It does however * respect an {@link Order} annotation if present. * @@ -35,7 +35,7 @@ import org.springframework.integration.handler.MessageHandler; public class DefaultAnnotationHandlerCreator implements AnnotationHandlerCreator { public MessageHandler createHandler(Object object, Method method, Annotation annotation) { - MessageHandlerAdapter adapter = new MessageHandlerAdapter(); + DefaultMessageHandlerAdapter adapter = new DefaultMessageHandlerAdapter(); adapter.setObject(object); adapter.setMethod(method.getName()); Order orderAnnotation = (Order) AnnotationUtils.getAnnotation(method, Order.class); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/Handler.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/Handler.java similarity index 90% rename from spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/Handler.java rename to spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/Handler.java index ad42588372..f0367f51bd 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/annotation/Handler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/annotation/Handler.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration.endpoint.annotation; +package org.springframework.integration.handler.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; @@ -23,6 +23,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.integration.endpoint.annotation.MessageEndpoint; + /** * Indicates that a method is capable of handling a message or message payload. * The method may only accept a single parameter, and the enclosing class should diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/InboundChannelAdapterTestBean.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/InboundChannelAdapterTestBean.java index a629218f2e..cca4cb8d76 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/InboundChannelAdapterTestBean.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/InboundChannelAdapterTestBean.java @@ -16,6 +16,8 @@ package org.springframework.integration.endpoint.annotation; +import org.springframework.integration.handler.annotation.Handler; + /** * @author Mark Fisher */ diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/OutboundChannelAdapterTestBean.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/OutboundChannelAdapterTestBean.java index 45e69819b0..3be387b2fc 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/OutboundChannelAdapterTestBean.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/OutboundChannelAdapterTestBean.java @@ -16,6 +16,8 @@ package org.springframework.integration.endpoint.annotation; +import org.springframework.integration.handler.annotation.Handler; + /** * @author Mark Fisher */ diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/SimpleAnnotatedEndpoint.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/SimpleAnnotatedEndpoint.java index 5cd3feb4a2..04a3055259 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/SimpleAnnotatedEndpoint.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/SimpleAnnotatedEndpoint.java @@ -16,6 +16,8 @@ package org.springframework.integration.endpoint.annotation; +import org.springframework.integration.handler.annotation.Handler; + /** * @author Mark Fisher */ diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointTests.xml b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointTests.xml index cecd0e3383..783e2683e3 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointTests.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointTests.xml @@ -15,7 +15,7 @@ - + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointWithAutoCreateChannelTests.xml b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointWithAutoCreateChannelTests.xml index f4fe8ea2cd..f02e95b678 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointWithAutoCreateChannelTests.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/simpleAnnotatedEndpointWithAutoCreateChannelTests.xml @@ -12,7 +12,7 @@ - +