INT-1401 EvaluationContext variable names are now fixed (#return, #method, #args, and #exception)

This commit is contained in:
Mark Fisher
2010-09-02 14:19:57 +00:00
parent 83c0fe79a2
commit 5c7f97f101
9 changed files with 69 additions and 339 deletions

View File

@@ -1,88 +0,0 @@
/*
* 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.aop;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
/**
* Base class for {@link ExpressionSource} implementations.
*
* @author Mark Fisher
* @since 2.0
*/
public abstract class AbstractExpressionSource implements ExpressionSource {
private volatile String methodNameVariableName = ExpressionSource.DEFAULT_METHOD_NAME_VARIABLE_NAME;
private volatile String argumentMapVariableName = ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME;
private volatile String returnValueVariableName = ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME;
private volatile String exceptionVariableName = ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME;
private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
public void setMethodNameVariableName(String methodNameVariableName) {
this.methodNameVariableName = methodNameVariableName;
}
public String getMethodNameVariableName(Method method) {
return this.methodNameVariableName;
}
public void setArgumentMapVariableName(String argumentMapVariableName) {
this.argumentMapVariableName = argumentMapVariableName;
}
public String getArgumentMapVariableName(Method method) {
return this.argumentMapVariableName;
}
public void setExceptionVariableName(String exceptionVariableName) {
this.exceptionVariableName = exceptionVariableName;
}
public String getExceptionVariableName(Method method) {
return this.exceptionVariableName;
}
public void setReturnValueVariableName(String returnValueVariableName) {
this.returnValueVariableName = returnValueVariableName;
}
public String getReturnValueVariableName(Method method) {
return this.returnValueVariableName;
}
protected String[] discoverMethodParameterNames(Method method) {
return this.parameterNameDiscoverer.getParameterNames(method);
}
public abstract String getPayloadExpression(Method method);
public abstract String[] getArgumentVariableNames(Method method);
public abstract Map<String, String> getHeaderExpressions(Method method);
public abstract String getChannelName(Method method);
}

View File

@@ -1,67 +0,0 @@
/*
* 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.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation that provides the variable names to use when constructing the
* evaluation context for a MessagePublishingInterceptor.
*
* @author Mark Fisher
* @since 2.0
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExpressionBinding {
/**
* Name of the variable in the context that refers to the method name.
* <p>The default is "method".
*/
String methodNameVariableName() default ExpressionSource.DEFAULT_METHOD_NAME_VARIABLE_NAME;
/**
* Names of the arguments as a comma-separated list. If not provided, the
* names will be discovered automatically if enabled by the compiler settings.
* These names will be used as the keys in the argument Map.
*/
String argumentVariableNames() default "";
/**
* Name of the variable in the context that refers to the Map of arguments.
* <p>The default is "args".
*/
String argumentMapVariableName() default ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME;
/**
* Name of the variable in the context that refers to the return value, if any.
* <p>The default is "return".
*/
String returnValueVariableName() default ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME;
/**
* Name of the variable in the context that refers to any exception thrown
* by the method invocation that is being intercepted.
* <p>The default is "exception".
*/
String exceptionVariableName() default ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME;
}

View File

@@ -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.
@@ -28,15 +28,21 @@ import java.util.Map;
*/
interface ExpressionSource {
static final String DEFAULT_METHOD_NAME_VARIABLE_NAME = "method";
static final String METHOD_NAME_VARIABLE_NAME = "method";
static final String DEFAULT_ARGUMENT_MAP_VARIABLE_NAME = "args";
static final String ARGUMENT_MAP_VARIABLE_NAME = "args";
static final String DEFAULT_RETURN_VALUE_VARIABLE_NAME = "return";
static final String RETURN_VALUE_VARIABLE_NAME = "return";
static final String DEFAULT_EXCEPTION_VARIABLE_NAME = "exception";
static final String EXCEPTION_VARIABLE_NAME = "exception";
/**
* Returns the channel name to which Messages should be published
* for this particular method invocation.
*/
String getChannelName(Method method);
/**
* Returns the expression string to be evaluated for creating the Message
* payload.
@@ -50,41 +56,4 @@ interface ExpressionSource {
*/
Map<String, String> getHeaderExpressions(Method method);
/**
* Returns the variable name to be associated with the intercepted
* method's name.
*/
String getMethodNameVariableName(Method method);
/**
* Returns the variable names to be associated with the intercepted method
* invocation's argument array.
*/
String[] getArgumentVariableNames(Method method);
/**
* Returns the variable name to use in the evaluation context for the Map
* of arguments. The keys in this map will be determined by the result of
* the {@link #getArgumentVariableNames(Method)} method.
*/
String getArgumentMapVariableName(Method method);
/**
* Returns the variable name to use in the evaluation context for any
* return value resulting from the method invocation.
*/
String getReturnValueVariableName(Method method);
/**
* Returns the variable name to use in the evaluation context for any
* exception thrown from the method invocation.
*/
String getExceptionVariableName(Method method);
/**
* Returns the channel name to which Messages should be published
* for this particular method invocation.
*/
String getChannelName(Method method);
}

View File

@@ -25,6 +25,8 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.expression.MapAccessor;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
@@ -59,6 +61,8 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
private volatile ChannelResolver channelResolver;
private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
public MessagePublishingInterceptor(ExpressionSource expressionSource) {
Assert.notNull(expressionSource, "expressionSource must not be null");
@@ -85,8 +89,8 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
context.addPropertyAccessor(new MapAccessor());
Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis());
Method method = AopUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
String[] argumentNames = this.expressionSource.getArgumentVariableNames(method);
context.setVariable(this.expressionSource.getMethodNameVariableName(method), method.getName());
String[] argumentNames = this.resolveArgumentNames(method);
context.setVariable(ExpressionSource.METHOD_NAME_VARIABLE_NAME, method.getName());
if (invocation.getArguments().length > 0 && argumentNames != null) {
Map<String, Object> argumentMap = new HashMap<String, Object>();
for (int i = 0; i < argumentNames.length; i++) {
@@ -97,15 +101,15 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
argumentMap.put("" + i, argValue);
argumentMap.put(argumentNames[i], argValue);
}
context.setVariable(this.expressionSource.getArgumentMapVariableName(method), argumentMap);
context.setVariable(ExpressionSource.ARGUMENT_MAP_VARIABLE_NAME, argumentMap);
}
try {
Object returnValue = invocation.proceed();
context.setVariable(this.expressionSource.getReturnValueVariableName(method), returnValue);
context.setVariable(ExpressionSource.RETURN_VALUE_VARIABLE_NAME, returnValue);
return returnValue;
}
catch (Throwable t) {
context.setVariable(this.expressionSource.getExceptionVariableName(method), t);
context.setVariable(ExpressionSource.EXCEPTION_VARIABLE_NAME, t);
throw t;
}
finally {
@@ -113,10 +117,14 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
}
}
private String[] resolveArgumentNames(Method method) {
return this.parameterNameDiscoverer.getParameterNames(method);
}
private void publishMessage(Method method, StandardEvaluationContext context) throws Exception {
String payloadExpressionString = this.expressionSource.getPayloadExpression(method);
if (!StringUtils.hasText(payloadExpressionString)) {
payloadExpressionString = "#" + this.expressionSource.getReturnValueVariableName(method);
payloadExpressionString = "#" + ExpressionSource.RETURN_VALUE_VARIABLE_NAME;
}
Expression expression = this.parser.parseExpression(payloadExpressionString);
Object result = expression.getValue(context);

View File

@@ -62,6 +62,14 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
this.channelAttributeName = channelAttributeName;
}
public String getChannelName(Method method) {
String channelName = this.getAnnotationValue(method, this.channelAttributeName, String.class);
if (channelName == null) {
channelName = this.getAnnotationValue(method.getDeclaringClass(), this.channelAttributeName, String.class);
}
return (StringUtils.hasText(channelName) ? channelName : null);
}
public String getPayloadExpression(Method method) {
String payloadExpression = null;
method.getAnnotation(Payload.class);
@@ -69,7 +77,7 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
if (methodPayloadAnnotation != null) {
payloadExpression = StringUtils.hasText(methodPayloadAnnotation.value())
? methodPayloadAnnotation.value()
: "#" + this.getReturnValueVariableName(method);
: "#" + ExpressionSource.RETURN_VALUE_VARIABLE_NAME;
}
Annotation[][] annotationArray = method.getParameterAnnotations();
for (int i = 0; i < annotationArray.length; i++) {
@@ -80,7 +88,7 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
"@Payload can be used at most once on a @Publisher method, either at method-level or on a single parameter");
Assert.state("".equals(((Payload) currentAnnotation).value()),
"@Payload on a parameter for a @Publisher method may not contain an expression");
payloadExpression = "#" + this.getArgumentMapVariableName(method) + "[" + i + "]";
payloadExpression = "#" + ExpressionSource.ARGUMENT_MAP_VARIABLE_NAME + "[" + i + "]";
}
}
}
@@ -100,64 +108,13 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
if (!StringUtils.hasText(name)) {
name = parameterNames[i];
}
headerExpressions.put(name, "#" + this.getArgumentMapVariableName(method) + "['" + i + "']");
headerExpressions.put(name, "#" + ExpressionSource.ARGUMENT_MAP_VARIABLE_NAME + "['" + i + "']");
}
}
}
return headerExpressions;
}
public String getMethodNameVariableName(Method method) {
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
if (annotation != null) {
return annotation.methodNameVariableName();
}
return ExpressionSource.DEFAULT_METHOD_NAME_VARIABLE_NAME;
}
public String[] getArgumentVariableNames(Method method) {
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
if (annotation != null) {
String argNameList = annotation.argumentVariableNames();
if (StringUtils.hasText(argNameList)) {
return StringUtils.tokenizeToStringArray(argNameList, ",");
}
}
return this.parameterNameDiscoverer.getParameterNames(method);
}
public String getArgumentMapVariableName(Method method) {
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
if (annotation != null) {
return annotation.argumentMapVariableName();
}
return ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME;
}
public String getReturnValueVariableName(Method method) {
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
if (annotation != null) {
return annotation.returnValueVariableName();
}
return ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME;
}
public String getExceptionVariableName(Method method) {
ExpressionBinding annotation = AnnotationUtils.findAnnotation(method, ExpressionBinding.class);
if (annotation != null) {
return annotation.exceptionVariableName();
}
return ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME;
}
public String getChannelName(Method method) {
String channelName = this.getAnnotationValue(method, this.channelAttributeName, String.class);
if (channelName == null) {
channelName = this.getAnnotationValue(method.getDeclaringClass(), this.channelAttributeName, String.class);
}
return (StringUtils.hasText(channelName) ? channelName : null);
}
private <T> T getAnnotationValue(Method method, String attributeName, Class<T> expectedType) {
T value = null;
for (Class<? extends Annotation> annotationType : this.annotationTypes) {

View File

@@ -27,7 +27,7 @@ import org.springframework.util.PatternMatchUtils;
* @author Mark Fisher
* @since 2.0
*/
public class MethodNameMappingExpressionSource extends AbstractExpressionSource {
public class MethodNameMappingExpressionSource implements ExpressionSource {
private final Map<String, String> payloadExpressionMap;
@@ -35,17 +35,12 @@ public class MethodNameMappingExpressionSource extends AbstractExpressionSource
private volatile Map<String, String> channelMap = Collections.emptyMap();
private volatile Map<String, String[]> argumentVariableNameMap;
public MethodNameMappingExpressionSource(Map<String, String> payloadExpressionMap) {
Assert.notEmpty(payloadExpressionMap, "payloadExpressionMap must not be empty");
this.payloadExpressionMap = payloadExpressionMap;
}
public void setArgumentVariableNameMap(Map<String, String[]> argumentVariableNameMap) {
this.argumentVariableNameMap = argumentVariableNameMap;
}
public void setHeaderExpressionMap(Map<String, Map<String, String>> headerExpressionMap) {
this.headerExpressionMap = headerExpressionMap;
@@ -55,17 +50,6 @@ public class MethodNameMappingExpressionSource extends AbstractExpressionSource
this.channelMap = channelMap;
}
public String[] getArgumentVariableNames(Method method) {
if (this.argumentVariableNameMap != null) {
for (Map.Entry<String, String[]> entry : this.argumentVariableNameMap.entrySet()) {
if (PatternMatchUtils.simpleMatch(entry.getKey(), method.getName())) {
return entry.getValue();
}
}
}
return this.discoverMethodParameterNames(method);
}
public String getPayloadExpression(Method method) {
for (Map.Entry<String, String> entry : this.payloadExpressionMap.entrySet()) {
if (PatternMatchUtils.simpleMatch(entry.getKey(), method.getName())) {

View File

@@ -27,7 +27,7 @@ import java.util.Map;
* @author Mark Fisher
* @since 2.0
*/
public class SimpleExpressionSource extends AbstractExpressionSource {
public class SimpleExpressionSource implements ExpressionSource {
private volatile String channelName;
@@ -40,7 +40,6 @@ public class SimpleExpressionSource extends AbstractExpressionSource {
this.channelName = channelName;
}
@Override
public String getChannelName(Method method) {
return this.channelName;
}
@@ -49,7 +48,6 @@ public class SimpleExpressionSource extends AbstractExpressionSource {
this.payloadExpression = payloadExpression;
}
@Override
public String getPayloadExpression(Method method) {
return this.payloadExpression;
}
@@ -58,14 +56,8 @@ public class SimpleExpressionSource extends AbstractExpressionSource {
this.headerExpressions = headerExpressions;
}
@Override
public Map<String, String> getHeaderExpressions(Method method) {
return this.headerExpressions;
}
@Override
public String[] getArgumentVariableNames(Method method) {
return this.discoverMethodParameterNames(method);
}
}

View File

@@ -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.
@@ -61,7 +61,7 @@ public class MessagePublishingInterceptorTests {
proxy.test();
Message<?> message = testChannel.receive(0);
assertNotNull(message);
assertEquals("foo", message.getPayload());
assertEquals("test-foo", message.getPayload());
}
@Test
public void demoMethodNameMappingExpressionSource() {
@@ -111,28 +111,8 @@ public class MessagePublishingInterceptorTests {
private static class TestExpressionSource implements ExpressionSource {
public String getMethodNameVariableName(Method method) {
return "m";
}
public String getArgumentMapVariableName(Method method) {
return "map";
}
public String[] getArgumentVariableNames(Method method) {
return new String[] { "a1", "a2"};
}
public String getReturnValueVariableName(Method method) {
return "r";
}
public String getExceptionVariableName(Method method) {
return "x";
}
public String getPayloadExpression(Method method) {
return "#r";
return "'test-' + #return";
}
public Map<String, String> getHeaderExpressions(Method method) {

View File

@@ -24,6 +24,7 @@ import java.util.Map;
import org.junit.Test;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Payload;
/**
@@ -34,40 +35,36 @@ public class MethodAnnotationExpressionSourceTests {
private final MethodAnnotationExpressionSource source = new MethodAnnotationExpressionSource();
@Test
public void defaultBindings() {
Method method = getMethod("methodWithExpressionAnnotationOnly", String.class, int.class);
String expressionString = source.getPayloadExpression(method);
assertEquals("testExpression1", expressionString);
assertEquals(2, source.getArgumentVariableNames(method).length);
assertEquals("arg1", source.getArgumentVariableNames(method)[0]);
assertEquals("arg2", source.getArgumentVariableNames(method)[1]);
Map<String, String> headerMap = source.getHeaderExpressions(method);
assertNotNull(headerMap);
assertEquals(0, headerMap.size());
assertEquals(ExpressionSource.DEFAULT_ARGUMENT_MAP_VARIABLE_NAME, source.getArgumentMapVariableName(method));
assertEquals(ExpressionSource.DEFAULT_EXCEPTION_VARIABLE_NAME, source.getExceptionVariableName(method));
assertEquals(ExpressionSource.DEFAULT_RETURN_VALUE_VARIABLE_NAME, source.getReturnValueVariableName(method));
}
@Test
public void annotationBindings() {
Method method = getMethod("methodWithExpressionBinding", String.class, int.class);
String expressionString = source.getPayloadExpression(method);
assertEquals("testExpression2", expressionString);
assertEquals(2, source.getArgumentVariableNames(method).length);
assertEquals("s", source.getArgumentVariableNames(method)[0]);
assertEquals("i", source.getArgumentVariableNames(method)[1]);
assertEquals("argz", source.getArgumentMapVariableName(method));
assertEquals("x", source.getExceptionVariableName(method));
assertEquals("result", source.getReturnValueVariableName(method));
}
@Test
public void channelName() {
Method method = getMethod("methodWithChannelAndReturnAsPayload");
String channelName = source.getChannelName(method);
String payloadExpression = source.getPayloadExpression(method);
assertEquals("foo", channelName);
assertEquals("#method", payloadExpression);
}
@Test
public void payloadButNoHeaders() {
Method method = getMethod("methodWithPayloadAnnotation", String.class, int.class);
String expressionString = source.getPayloadExpression(method);
assertEquals("testExpression1", expressionString);
Map<String, String> headerMap = source.getHeaderExpressions(method);
assertNotNull(headerMap);
assertEquals(0, headerMap.size());
}
@Test
public void payloadAndHeaders() {
Method method = getMethod("methodWithHeaderAnnotations", String.class, String.class, String.class);
String expressionString = source.getPayloadExpression(method);
assertEquals("testExpression2", expressionString);
Map<String, String> headerMap = source.getHeaderExpressions(method);
assertNotNull(headerMap);
assertEquals(2, headerMap.size());
assertEquals("#args['1']", headerMap.get("foo"));
assertEquals("#args['2']", headerMap.get("bar"));
}
@@ -83,19 +80,17 @@ public class MethodAnnotationExpressionSourceTests {
@Publisher
@Payload("testExpression1")
public void methodWithExpressionAnnotationOnly(String arg1, int arg2) {
public void methodWithPayloadAnnotation(String arg1, int arg2) {
}
@Publisher(channel="foo")
@Payload
@Payload("#method")
public void methodWithChannelAndReturnAsPayload() {
}
@Publisher
@Payload("testExpression2")
@ExpressionBinding(argumentVariableNames="s, i", argumentMapVariableName="argz",
exceptionVariableName="x", returnValueVariableName="result")
public void methodWithExpressionBinding(String arg1, int arg2) {
public void methodWithHeaderAnnotations(String arg1, @Header("foo") String h1, @Header("bar") String h2) {
}
}