Refactored MessageProcessingTransformer to an abstract base class with separate MethodInvokingTransformer and ExpressionEvaluatingTransformer sub-classes (now consistent with splitter, router, etc).

This commit is contained in:
Mark Fisher
2009-11-30 18:29:34 +00:00
parent 94c9c4d4fe
commit 911c48b372
6 changed files with 112 additions and 58 deletions

View File

@@ -16,12 +16,10 @@
package org.springframework.integration.config;
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.message.MessageHandler;
import org.springframework.integration.transformer.ExpressionEvaluatingTransformer;
import org.springframework.integration.transformer.MessageTransformingHandler;
import org.springframework.integration.transformer.MessageProcessingTransformer;
import org.springframework.integration.transformer.MethodInvokingTransformer;
import org.springframework.integration.transformer.Transformer;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -41,21 +39,17 @@ public class TransformerFactoryBean extends AbstractMessageHandlerFactoryBean {
transformer = (Transformer) targetObject;
}
else if (StringUtils.hasText(targetMethodName)) {
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(targetObject, targetMethodName);
transformer = new MessageProcessingTransformer(messageProcessor);
transformer = new MethodInvokingTransformer(targetObject, targetMethodName);
}
else {
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(
targetObject, org.springframework.integration.annotation.Transformer.class);
transformer = new MessageProcessingTransformer(messageProcessor);
transformer = new MethodInvokingTransformer(targetObject);
}
return new MessageTransformingHandler(transformer);
}
@Override
MessageHandler createExpressionEvaluatingHandler(String expression) {
MessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
Transformer transformer = new MessageProcessingTransformer(processor);
Transformer transformer = new ExpressionEvaluatingTransformer(expression);
return new MessageTransformingHandler(transformer);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -20,11 +20,9 @@ import java.lang.reflect.Method;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.message.MessageHandler;
import org.springframework.integration.transformer.MessageProcessingTransformer;
import org.springframework.integration.transformer.MessageTransformingHandler;
import org.springframework.integration.transformer.MethodInvokingTransformer;
import org.springframework.util.StringUtils;
/**
@@ -41,8 +39,7 @@ public class TransformerAnnotationPostProcessor extends AbstractMethodAnnotation
@Override
protected MessageHandler createHandler(Object bean, Method method, Transformer annotation) {
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(bean, method);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(bean, method);
MessageTransformingHandler handler = new MessageTransformingHandler(transformer);
String outputChannelName = annotation.outputChannel();
if (StringUtils.hasText(outputChannelName)) {

View File

@@ -26,20 +26,22 @@ import org.springframework.integration.message.MessageHandlingException;
import org.springframework.util.Assert;
/**
* Base class for Message Transformers that delegate to a {@link MessageProcessor}.
*
* @author Mark Fisher
*/
public class MessageProcessingTransformer implements Transformer {
public abstract class AbstractMessageProcessingTransformer implements Transformer {
private final MessageProcessor messageProcessor;
public MessageProcessingTransformer(MessageProcessor messageProcessor) {
protected AbstractMessageProcessingTransformer(MessageProcessor messageProcessor) {
Assert.notNull(messageProcessor, "messageProcessor must not be null");
this.messageProcessor = messageProcessor;
}
public Message<?> transform(Message<?> message) {
public final Message<?> transform(Message<?> message) {
Object result = this.messageProcessor.processMessage(message);
if (result == null) {
return null;

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2002-2009 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.transformer;
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
/**
* A Message Transformer implementation that evaluates the specified SpEL
* expression. The result of evaluation will typically be considered as the
* payload of a new Message unless it is itself already a Message.
*
* @author Mark Fisher
* @since 2.0
*/
public class ExpressionEvaluatingTransformer extends AbstractMessageProcessingTransformer {
public ExpressionEvaluatingTransformer(String expression) {
super(new ExpressionEvaluatingMessageProcessor(expression));
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2009 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.transformer;
import java.lang.reflect.Method;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
/**
* A Message Transformer implementation that invokes the specified method
* on the given object. The method's return value will be considered as
* the payload of a new Message unless the return value is itself already
* a Message.
*
* @author Mark Fisher
*/
public class MethodInvokingTransformer extends AbstractMessageProcessingTransformer {
public MethodInvokingTransformer(Object object, Method method) {
super(new MethodInvokingMessageProcessor(object, method));
}
public MethodInvokingTransformer(Object object, String methodName) {
super(new MethodInvokingMessageProcessor(object, methodName));
}
public MethodInvokingTransformer(Object object) {
super(new MethodInvokingMessageProcessor(object, Transformer.class));
}
}

View File

@@ -28,8 +28,6 @@ import org.junit.Test;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.core.Message;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandlingException;
@@ -44,8 +42,7 @@ public class MethodInvokingTransformerTests {
public void simplePayloadConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("exclaim", String.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<?> message = new StringMessage("foo");
Message<?> result = transformer.transform(message);
assertEquals("FOO!", result.getPayload());
@@ -54,8 +51,7 @@ public class MethodInvokingTransformerTests {
@Test
public void simplePayloadConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "exclaim");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "exclaim");
Message<?> message = new StringMessage("foo");
Message<?> result = transformer.transform(message);
assertEquals("FOO!", result.getPayload());
@@ -65,8 +61,7 @@ public class MethodInvokingTransformerTests {
public void typeConversionConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("exclaim", String.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<?> message = new GenericMessage<Integer>(123);
Message<?> result = transformer.transform(message);
assertEquals("123!", result.getPayload());
@@ -75,8 +70,7 @@ public class MethodInvokingTransformerTests {
@Test
public void typeConversionConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "exclaim");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "exclaim");
Message<?> message = new GenericMessage<Integer>(123);
Message<?> result = transformer.transform(message);
assertEquals("123!", result.getPayload());
@@ -86,8 +80,7 @@ public class MethodInvokingTransformerTests {
public void typeConversionFailureConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("exclaim", String.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<?> message = new GenericMessage<Date>(new Date());
transformer.transform(message);
}
@@ -95,8 +88,7 @@ public class MethodInvokingTransformerTests {
@Test(expected = IllegalArgumentException.class)
public void typeConversionFailureConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "exclaim");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "exclaim");
Message<?> message = new GenericMessage<Date>(new Date());
transformer.transform(message);
}
@@ -105,8 +97,7 @@ public class MethodInvokingTransformerTests {
public void headerAnnotationConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("headerTest", String.class, Integer.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<String> message = MessageBuilder.withPayload("foo")
.setHeader("number", 123).build();
Message<?> result = transformer.transform(message);
@@ -116,8 +107,7 @@ public class MethodInvokingTransformerTests {
@Test
public void headerAnnotationConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "headerTest");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "headerTest");
Message<String> message = MessageBuilder.withPayload("foo")
.setHeader("number", 123).build();
Message<?> result = transformer.transform(message);
@@ -128,8 +118,7 @@ public class MethodInvokingTransformerTests {
public void headerValueNotProvided() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("headerTest", String.class, Integer.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<String> message = MessageBuilder.withPayload("foo")
.setHeader("wrong", 123).build();
transformer.transform(message);
@@ -139,8 +128,7 @@ public class MethodInvokingTransformerTests {
public void optionalHeaderAnnotation() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("optionalHeaderTest", String.class, Integer.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<String> message = MessageBuilder.withPayload("foo").setHeader("number", 99).build();
Message<?> result = transformer.transform(message);
assertEquals("foo99", result.getPayload());
@@ -150,8 +138,7 @@ public class MethodInvokingTransformerTests {
public void optionalHeaderValueNotProvided() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("optionalHeaderTest", String.class, Integer.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<String> message = MessageBuilder.withPayload("foo").build();
Message<?> result = transformer.transform(message);
assertEquals("foonull", result.getPayload());
@@ -161,8 +148,7 @@ public class MethodInvokingTransformerTests {
public void headerEnricherConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("propertyEnricherTest", String.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<String> message = MessageBuilder.withPayload("test")
.setHeader("prop1", "bad")
.setHeader("prop3", "baz").build();
@@ -176,8 +162,7 @@ public class MethodInvokingTransformerTests {
@Test
public void headerEnricherConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "propertyEnricherTest");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "propertyEnricherTest");
Message<String> message = MessageBuilder.withPayload("test")
.setHeader("prop1", "bad")
.setHeader("prop3", "baz").build();
@@ -192,8 +177,7 @@ public class MethodInvokingTransformerTests {
public void messageReturnValueConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("messageReturnValueTest", Message.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Message<String> message = MessageBuilder.withPayload("test").build();
Message<?> result = transformer.transform(message);
assertEquals("test", result.getPayload());
@@ -202,8 +186,7 @@ public class MethodInvokingTransformerTests {
@Test
public void messageReturnValueConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "messageReturnValueTest");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "messageReturnValueTest");
Message<String> message = MessageBuilder.withPayload("test").build();
Message<?> result = transformer.transform(message);
assertEquals("test", result.getPayload());
@@ -214,8 +197,7 @@ public class MethodInvokingTransformerTests {
public void propertiesPayloadConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("propertyPayloadTest", Properties.class);
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, testMethod);
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, testMethod);
Properties props = new Properties();
props.setProperty("prop1", "bad");
props.setProperty("prop3", "baz");
@@ -235,8 +217,7 @@ public class MethodInvokingTransformerTests {
@SuppressWarnings("unchecked")
public void propertiesPayloadConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "propertyPayloadTest");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "propertyPayloadTest");
Properties props = new Properties();
props.setProperty("prop1", "bad");
props.setProperty("prop3", "baz");
@@ -255,8 +236,7 @@ public class MethodInvokingTransformerTests {
@Test
public void nullReturningMethod() {
TestBean testBean = new TestBean();
MessageProcessor messageProcessor = new MethodInvokingMessageProcessor(testBean, "nullReturnValueTest");
MessageProcessingTransformer transformer = new MessageProcessingTransformer(messageProcessor);
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "nullReturnValueTest");
StringMessage message = new StringMessage("test");
Message<?> result = transformer.transform(message);
assertNull(result);