INT-174 added AbstractMessageProcessor with a setter for ConversionService
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.core.convert.ConversionService;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.expression.spel.support.StandardTypeConverter;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class AbstractMessageProcessor implements MessageProcessor {
|
||||
|
||||
private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
||||
|
||||
|
||||
public void setConversionService(ConversionService conversionService) {
|
||||
if (conversionService != null) {
|
||||
this.evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService));
|
||||
}
|
||||
}
|
||||
|
||||
protected StandardEvaluationContext getEvaluationContext() {
|
||||
return this.evaluationContext;
|
||||
}
|
||||
|
||||
protected Object evaluateExpression(Expression expression, Message<?> message) {
|
||||
try {
|
||||
return expression.getValue(this.evaluationContext, message);
|
||||
}
|
||||
catch (EvaluationException e) {
|
||||
throw new MessageHandlingException(message, "Expression evaluation failed.", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -17,15 +17,12 @@
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import org.springframework.context.expression.MapAccessor;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.ParseException;
|
||||
import org.springframework.expression.spel.SpelParserConfiguration;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
|
||||
/**
|
||||
* A {@link MessageProcessor} implementation that evaluates a SpEL expression
|
||||
@@ -34,7 +31,7 @@ import org.springframework.integration.message.MessageHandlingException;
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class ExpressionEvaluatingMessageProcessor implements MessageProcessor {
|
||||
public class ExpressionEvaluatingMessageProcessor extends AbstractMessageProcessor {
|
||||
|
||||
private final ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
|
||||
|
||||
@@ -44,6 +41,7 @@ public class ExpressionEvaluatingMessageProcessor implements MessageProcessor {
|
||||
public ExpressionEvaluatingMessageProcessor(String expression) {
|
||||
try {
|
||||
this.expression = parser.parseExpression(expression);
|
||||
this.getEvaluationContext().addPropertyAccessor(new MapAccessor());
|
||||
}
|
||||
catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Failed to parse expression.", e);
|
||||
@@ -52,14 +50,7 @@ public class ExpressionEvaluatingMessageProcessor implements MessageProcessor {
|
||||
|
||||
|
||||
public Object processMessage(Message<?> message) {
|
||||
StandardEvaluationContext context = new StandardEvaluationContext(message);
|
||||
context.addPropertyAccessor(new MapAccessor());
|
||||
try {
|
||||
return this.expression.getValue(context);
|
||||
}
|
||||
catch (EvaluationException e) {
|
||||
throw new MessageHandlingException(message, "Expression evaluation failed.", e);
|
||||
}
|
||||
return this.evaluateExpression(this.expression, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,14 +38,10 @@ import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.expression.spel.support.StandardTypeConverter;
|
||||
import org.springframework.integration.annotation.Header;
|
||||
import org.springframework.integration.annotation.Headers;
|
||||
import org.springframework.integration.annotation.Payload;
|
||||
@@ -72,7 +68,7 @@ import org.springframework.util.ReflectionUtils.MethodFilter;
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class MethodInvokingMessageProcessor implements MessageProcessor {
|
||||
public class MethodInvokingMessageProcessor extends AbstractMessageProcessor {
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
@@ -84,10 +80,6 @@ public class MethodInvokingMessageProcessor implements MessageProcessor {
|
||||
|
||||
private final Map<Class<?>, HandlerMethod> handlerMethods;
|
||||
|
||||
private volatile ConversionService conversionService;
|
||||
|
||||
private final EvaluationContext evaluationContext;
|
||||
|
||||
|
||||
public MethodInvokingMessageProcessor(Object targetObject, Method method) {
|
||||
this(targetObject, null, method);
|
||||
@@ -116,7 +108,7 @@ public class MethodInvokingMessageProcessor implements MessageProcessor {
|
||||
Assert.notNull(targetObject, "targetObject must not be null");
|
||||
this.targetObject = targetObject;
|
||||
this.handlerMethods = Collections.<Class<?>, HandlerMethod>singletonMap(handlerMethod.getTargetParameterType(), handlerMethod);
|
||||
this.evaluationContext = this.createEvaluationContext(method, annotationType);
|
||||
this.prepareEvaluationContext(this.getEvaluationContext(), method, annotationType);
|
||||
this.setDisplayString(targetObject, method);
|
||||
}
|
||||
|
||||
@@ -129,7 +121,7 @@ public class MethodInvokingMessageProcessor implements MessageProcessor {
|
||||
this.targetObject = targetObject;
|
||||
this.requiresReply = requiresReply;
|
||||
this.handlerMethods = this.findHandlerMethodsForTarget(targetObject, annotationType, methodName, requiresReply);
|
||||
this.evaluationContext = this.createEvaluationContext(methodName, annotationType);
|
||||
this.prepareEvaluationContext(this.getEvaluationContext(), methodName, annotationType);
|
||||
this.setDisplayString(targetObject, methodName);
|
||||
}
|
||||
|
||||
@@ -145,19 +137,7 @@ public class MethodInvokingMessageProcessor implements MessageProcessor {
|
||||
this.displayString = sb.toString() + "]";
|
||||
}
|
||||
|
||||
public void setConversionService(ConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
private ConversionService getRequiredConversionService() {
|
||||
if (this.conversionService == null) {
|
||||
this.conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||
}
|
||||
return this.conversionService;
|
||||
}
|
||||
|
||||
private EvaluationContext createEvaluationContext(Object method, Class<? extends Annotation> annotationType) {
|
||||
StandardEvaluationContext context = new StandardEvaluationContext();
|
||||
private void prepareEvaluationContext(StandardEvaluationContext context, Object method, Class<? extends Annotation> annotationType) {
|
||||
Class<?> targetType = AopUtils.getTargetClass(this.targetObject);
|
||||
if (method instanceof Method) {
|
||||
context.registerMethodFilter(targetType, new FixedHandlerMethodFilter((Method) method));
|
||||
@@ -167,9 +147,7 @@ public class MethodInvokingMessageProcessor implements MessageProcessor {
|
||||
new HandlerMethodFilter(annotationType, (String) method, this.requiresReply));
|
||||
}
|
||||
context.addPropertyAccessor(new MapAccessor());
|
||||
context.setTypeConverter(new StandardTypeConverter(this.getRequiredConversionService()));
|
||||
context.setVariable("target", targetObject);
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
@@ -183,7 +161,7 @@ public class MethodInvokingMessageProcessor implements MessageProcessor {
|
||||
for (HandlerMethod candidate : candidates) {
|
||||
try {
|
||||
Expression expression = candidate.getExpression();
|
||||
Object result = expression.getValue(this.evaluationContext, message);
|
||||
Object result = this.evaluateExpression(expression, message);
|
||||
if (this.requiresReply) {
|
||||
// TODO: remove this if SpEL is modified to throw an EvaluationException instead
|
||||
// e.g. we can invoke getValue(this.evaluationContext, message, candidate.getReturnType);
|
||||
|
||||
Reference in New Issue
Block a user