From 70547052c3a275f2e756503f3d051cab7b5168dd Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 3 Dec 2007 12:44:20 +0000 Subject: [PATCH] Added MessagePublishingInterceptor (INT-28). --- spring-eai-core/.classpath | 8 +- spring-eai-core/ivy.xml | 1 + .../aop/MessagePublishingInterceptor.java | 69 +++++++++++++ .../MessagePublishingInterceptorTests.java | 98 +++++++++++++++++++ 4 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 spring-eai-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java create mode 100644 spring-eai-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java diff --git a/spring-eai-core/.classpath b/spring-eai-core/.classpath index 7daf87910e..280f158992 100644 --- a/spring-eai-core/.classpath +++ b/spring-eai-core/.classpath @@ -3,10 +3,12 @@ + - - - + + + + diff --git a/spring-eai-core/ivy.xml b/spring-eai-core/ivy.xml index 2d87c3c6bf..7afa23d911 100644 --- a/spring-eai-core/ivy.xml +++ b/spring-eai-core/ivy.xml @@ -17,6 +17,7 @@ + \ No newline at end of file diff --git a/spring-eai-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java b/spring-eai-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java new file mode 100644 index 0000000000..03bfda6b88 --- /dev/null +++ b/spring-eai-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java @@ -0,0 +1,69 @@ +/* + * 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.aop; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.message.MessageMapper; +import org.springframework.integration.message.SimplePayloadMessageMapper; +import org.springframework.util.Assert; + +/** + * Interceptor that publishes a target method's return value to a channel. + * + * @author Mark Fisher + */ +public class MessagePublishingInterceptor implements MethodInterceptor { + + private MessageMapper mapper = new SimplePayloadMessageMapper(); + + private MessageChannel channel; + + + /** + * Create a publishing interceptor for the given channel. + */ + public MessagePublishingInterceptor(MessageChannel channel) { + Assert.notNull(channel, "channel must not be null"); + this.channel = channel; + } + + /** + * Specify the {@link MessageMapper} to use when creating a message from the + * return value Object. The default is a {@link SimplePayloadMessageMapper}. + * + * @param mapper the mapper to use + */ + public void setMessageMapper(MessageMapper mapper) { + Assert.notNull(mapper, "mapper must not be null"); + this.mapper = mapper; + } + + /** + * Invoke the target method and publish its return value. + */ + public Object invoke(MethodInvocation invocation) throws Throwable { + Object retval = invocation.proceed(); + if (retval != null) { + this.channel.send(mapper.toMessage(retval)); + } + return retval; + } + +} diff --git a/spring-eai-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java b/spring-eai-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java new file mode 100644 index 0000000000..0ee18dae1d --- /dev/null +++ b/spring-eai-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java @@ -0,0 +1,98 @@ +/* + * 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.aop; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import org.springframework.aop.framework.ProxyFactory; +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.channel.PointToPointChannel; +import org.springframework.integration.message.Message; + +/** + * @author Mark Fisher + */ +public class MessagePublishingInterceptorTests { + + @Test + public void testNonNullReturnValuePublished() { + MessageChannel channel = new PointToPointChannel(); + MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(channel); + TestService proxy = (TestService) this.createProxy(new TestServiceImpl("hello world"), interceptor); + proxy.messageTest(); + Message message = channel.receive(0); + assertNotNull(message); + assertEquals("hello world", message.getPayload()); + } + + @Test + public void testNullReturnValueNotPublished() { + MessageChannel channel = new PointToPointChannel(); + MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(channel); + TestService proxy = (TestService) this.createProxy(new TestServiceImpl(null), interceptor); + proxy.messageTest(); + Message message = channel.receive(0); + assertNull(message); + } + + @Test + public void testVoidReturnValueNotPublished() { + MessageChannel channel = new PointToPointChannel(); + MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(channel); + TestService proxy = (TestService) this.createProxy(new TestServiceImpl(null), interceptor); + proxy.voidTest(); + Message message = channel.receive(0); + assertNull(message); + } + + + private Object createProxy(Object target, MessagePublishingInterceptor interceptor) { + ProxyFactory factory = new ProxyFactory(target); + factory.addAdvice(interceptor); + return factory.getProxy(); + } + + + private static interface TestService { + String messageTest(); + void voidTest(); + } + + + private static class TestServiceImpl implements TestService { + + private String message; + + public TestServiceImpl(String message) { + this.message = message; + } + + public String messageTest() { + return this.message; + } + + public void voidTest() { + return; + } + + } + +}