Added MessagePublishingInterceptor (INT-28).

This commit is contained in:
Mark Fisher
2007-12-03 12:44:20 +00:00
parent ee4524950d
commit 70547052c3
4 changed files with 173 additions and 3 deletions

View File

@@ -3,10 +3,12 @@
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="IVY_CACHE/org.aopalliance/aopalliance/aopalliance-1.0.jar" sourcepath="IVY_CACHE/org.aopalliance/aopalliance/aopalliance-sources-1.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache/commons-logging/commons-logging-1.1.jar" sourcepath="/IVY_CACHE/org.apache/commons-logging/commons-logging-sources-1.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.junit/junit/junit-4.4.jar" sourcepath="/IVY_CACHE/org.junit/junit/junit-sources-4.4.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-beans/spring-beans-2.1-m4.jar" sourcepath="/IVY_CACHE/org.springframework/spring-beans/spring-beans-sources-2.1-m4.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-context/spring-context-2.1-m4.jar" sourcepath="/IVY_CACHE/org.springframework/spring-context/spring-context-sources-2.1-m4.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-core/spring-core-2.1-m4.jar" sourcepath="/IVY_CACHE/org.springframework/spring-core/spring-core-sources-2.1-m4.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-aop/spring-aop-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-context/spring-aop-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-beans/spring-beans-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-context/spring-bean-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-context/spring-context-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-context/spring-context-sources-2.5.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-core/spring-core-2.5.0.jar" sourcepath="/IVY_CACHE/org.springframework/spring-core/spring-core-sources-2.5.0.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -17,6 +17,7 @@
<dependencies>
<dependency org="org.junit" name="junit" rev="4.4" conf="test->default"/>
<dependency org="org.springframework" name="spring-context" rev="2.5.0" conf="compile->default"/>
<dependency org="org.springframework" name="spring-aop" rev="2.5.0" conf="compile->default"/>
</dependencies>
</ivy-module>

View File

@@ -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;
}
}

View File

@@ -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;
}
}
}