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