INT-1401 renamed ExpressionSource to PublisherMetadataSource

This commit is contained in:
Mark Fisher
2010-09-02 15:37:23 +00:00
parent 8285e6b645
commit a55ab3af05
10 changed files with 47 additions and 47 deletions

View File

@@ -46,7 +46,7 @@ import org.springframework.util.StringUtils;
* A {@link MethodInterceptor} that publishes Messages to a channel. The
* payload of the published Message can be derived from arguments or any return
* value or exception resulting from the method invocation. That mapping is the
* responsibility of the EL expression provided by the ExpressionSource.
* responsibility of the EL expression provided by the {@link PublisherMetadataSource}.
*
* @author Mark Fisher
* @since 2.0
@@ -55,7 +55,7 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
private volatile ExpressionSource expressionSource;
private volatile PublisherMetadataSource metadataSource;
private final ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
@@ -64,15 +64,15 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
public MessagePublishingInterceptor(ExpressionSource expressionSource) {
Assert.notNull(expressionSource, "expressionSource must not be null");
this.expressionSource = expressionSource;
public MessagePublishingInterceptor(PublisherMetadataSource metadataSource) {
Assert.notNull(metadataSource, "metadataSource must not be null");
this.metadataSource = metadataSource;
}
public void setExpressionSource(ExpressionSource expressionSource) {
Assert.notNull(expressionSource, "expressionSource must not be null");
this.expressionSource = expressionSource;
public void setPublisherMetadataSource(PublisherMetadataSource metadataSource) {
Assert.notNull(metadataSource, "metadataSource must not be null");
this.metadataSource = metadataSource;
}
public void setDefaultChannel(MessageChannel defaultChannel) {
@@ -84,13 +84,13 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
}
public final Object invoke(final MethodInvocation invocation) throws Throwable {
Assert.notNull(this.expressionSource, "ExpressionSource is required.");
Assert.notNull(this.metadataSource, "PublisherMetadataSource is required.");
final StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new MapAccessor());
Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis());
Method method = AopUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
String[] argumentNames = this.resolveArgumentNames(method);
context.setVariable(ExpressionSource.METHOD_NAME_VARIABLE_NAME, method.getName());
context.setVariable(PublisherMetadataSource.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++) {
@@ -101,15 +101,15 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
argumentMap.put("" + i, argValue);
argumentMap.put(argumentNames[i], argValue);
}
context.setVariable(ExpressionSource.ARGUMENT_MAP_VARIABLE_NAME, argumentMap);
context.setVariable(PublisherMetadataSource.ARGUMENT_MAP_VARIABLE_NAME, argumentMap);
}
try {
Object returnValue = invocation.proceed();
context.setVariable(ExpressionSource.RETURN_VALUE_VARIABLE_NAME, returnValue);
context.setVariable(PublisherMetadataSource.RETURN_VALUE_VARIABLE_NAME, returnValue);
return returnValue;
}
catch (Throwable t) {
context.setVariable(ExpressionSource.EXCEPTION_VARIABLE_NAME, t);
context.setVariable(PublisherMetadataSource.EXCEPTION_VARIABLE_NAME, t);
throw t;
}
finally {
@@ -122,9 +122,9 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
}
private void publishMessage(Method method, StandardEvaluationContext context) throws Exception {
String payloadExpressionString = this.expressionSource.getPayloadExpression(method);
String payloadExpressionString = this.metadataSource.getPayloadExpression(method);
if (!StringUtils.hasText(payloadExpressionString)) {
payloadExpressionString = "#" + ExpressionSource.RETURN_VALUE_VARIABLE_NAME;
payloadExpressionString = "#" + PublisherMetadataSource.RETURN_VALUE_VARIABLE_NAME;
}
Expression expression = this.parser.parseExpression(payloadExpressionString);
Object result = expression.getValue(context);
@@ -137,7 +137,7 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
builder.copyHeaders(headers);
}
Message<?> message = builder.build();
String channelName = this.expressionSource.getChannelName(method);
String channelName = this.metadataSource.getChannelName(method);
MessageChannel channel = null;
if (channelName != null) {
Assert.state(this.channelResolver != null, "ChannelResolver is required to resolve channel names.");
@@ -155,7 +155,7 @@ public class MessagePublishingInterceptor implements MethodInterceptor {
private Map<String, Object> evaluateHeaders(Method method, StandardEvaluationContext context)
throws ParseException, EvaluationException {
Map<String, String> headerExpressionMap = this.expressionSource.getHeaderExpressions(method);
Map<String, String> headerExpressionMap = this.metadataSource.getHeaderExpressions(method);
if (headerExpressionMap != null) {
Map<String, Object> headers = new HashMap<String, Object>();
for (Map.Entry<String, String> headerExpressionEntry : headerExpressionMap.entrySet()) {

View File

@@ -32,13 +32,13 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* An {@link ExpressionSource} implementation that retrieves the expression
* string and evaluation context variable names from an annotation.
* An {@link PublisherMetadataSource} implementation that retrieves the channel
* name and expression strings from an annotation.
*
* @author Mark Fisher
* @since 2.0
*/
public class MethodAnnotationExpressionSource implements ExpressionSource {
public class MethodAnnotationPublisherMetadataSource implements PublisherMetadataSource {
private final Set<Class<? extends Annotation>> annotationTypes;
@@ -47,11 +47,11 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
public MethodAnnotationExpressionSource() {
public MethodAnnotationPublisherMetadataSource() {
this(Collections.<Class<? extends Annotation>>singleton(Publisher.class));
}
public MethodAnnotationExpressionSource(Set<Class<? extends Annotation>> annotationTypes) {
public MethodAnnotationPublisherMetadataSource(Set<Class<? extends Annotation>> annotationTypes) {
Assert.notEmpty(annotationTypes, "annotationTypes must not be empty");
this.annotationTypes = annotationTypes;
}
@@ -77,9 +77,9 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
if (methodPayloadAnnotation != null) {
payloadExpression = StringUtils.hasText(methodPayloadAnnotation.value())
? methodPayloadAnnotation.value()
: "#" + ExpressionSource.RETURN_VALUE_VARIABLE_NAME;
: "#" + PublisherMetadataSource.RETURN_VALUE_VARIABLE_NAME;
}
if (payloadExpression == null || payloadExpression.contains("#" + ExpressionSource.RETURN_VALUE_VARIABLE_NAME)) {
if (payloadExpression == null || payloadExpression.contains("#" + PublisherMetadataSource.RETURN_VALUE_VARIABLE_NAME)) {
Assert.isTrue(!void.class.equals(method.getReturnType()),
"When defining @Publisher on a void-returning method, an explicit payload " +
"expression that does not rely upon a #return value is required.");
@@ -93,7 +93,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 = "#" + ExpressionSource.ARGUMENT_MAP_VARIABLE_NAME + "[" + i + "]";
payloadExpression = "#" + PublisherMetadataSource.ARGUMENT_MAP_VARIABLE_NAME + "[" + i + "]";
}
}
}
@@ -113,7 +113,7 @@ public class MethodAnnotationExpressionSource implements ExpressionSource {
if (!StringUtils.hasText(name)) {
name = parameterNames[i];
}
headerExpressions.put(name, "#" + ExpressionSource.ARGUMENT_MAP_VARIABLE_NAME + "['" + i + "']");
headerExpressions.put(name, "#" + PublisherMetadataSource.ARGUMENT_MAP_VARIABLE_NAME + "['" + i + "']");
}
}
}

View File

@@ -27,7 +27,7 @@ import org.springframework.util.PatternMatchUtils;
* @author Mark Fisher
* @since 2.0
*/
public class MethodNameMappingExpressionSource implements ExpressionSource {
public class MethodNameMappingPublisherMetadataSource implements PublisherMetadataSource {
private final Map<String, String> payloadExpressionMap;
@@ -36,7 +36,7 @@ public class MethodNameMappingExpressionSource implements ExpressionSource {
private volatile Map<String, String> channelMap = Collections.emptyMap();
public MethodNameMappingExpressionSource(Map<String, String> payloadExpressionMap) {
public MethodNameMappingPublisherMetadataSource(Map<String, String> payloadExpressionMap) {
Assert.notEmpty(payloadExpressionMap, "payloadExpressionMap must not be empty");
this.payloadExpressionMap = payloadExpressionMap;
}

View File

@@ -57,8 +57,8 @@ public class PublisherAnnotationAdvisor extends AbstractPointcutAdvisor implemen
public PublisherAnnotationAdvisor(Class<? extends Annotation> ... publisherAnnotationTypes) {
this.publisherAnnotationTypes = new HashSet<Class<? extends Annotation>>(Arrays.asList(publisherAnnotationTypes));
ExpressionSource source = new MethodAnnotationExpressionSource(this.publisherAnnotationTypes);
this.interceptor = new MessagePublishingInterceptor(source);
PublisherMetadataSource metadataSource = new MethodAnnotationPublisherMetadataSource(this.publisherAnnotationTypes);
this.interceptor = new MessagePublishingInterceptor(metadataSource);
}
@SuppressWarnings("unchecked")

View File

@@ -26,7 +26,7 @@ import java.util.Map;
* @author Mark Fisher
* @since 2.0
*/
interface ExpressionSource {
interface PublisherMetadataSource {
static final String METHOD_NAME_VARIABLE_NAME = "method";

View File

@@ -20,14 +20,14 @@ import java.lang.reflect.Method;
import java.util.Map;
/**
* Simple implementation of {@link ExpressionSource} that allows for
* Simple implementation of {@link PublisherMetadataSource} that allows for
* configuration of a single channel name, payload expression, and
* array of header key=value expressions.
*
* @author Mark Fisher
* @since 2.0
*/
public class SimpleExpressionSource implements ExpressionSource {
public class SimplePublisherMetadataSource implements PublisherMetadataSource {
private volatile String channelName;

View File

@@ -29,7 +29,7 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.aop.MethodNameMappingExpressionSource;
import org.springframework.integration.aop.MethodNameMappingPublisherMetadataSource;
import org.springframework.integration.channel.MapBasedChannelResolver;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.util.StringUtils;
@@ -47,7 +47,7 @@ public class PublishingInterceptorParser extends AbstractBeanDefinitionParser {
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
BeanDefinitionBuilder rootBuilder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".aop.MessagePublishingInterceptor");
BeanDefinitionBuilder spelSourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodNameMappingExpressionSource.class.getName());
BeanDefinitionBuilder spelSourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodNameMappingPublisherMetadataSource.class.getName());
Map<String, Map<?,?>> mappings = this.getMappings(element, element.getAttribute("default-channel"), parserContext);
spelSourceBuilder.addConstructorArgValue(mappings.get("payload"));
if (mappings.get("headers") != null) {

View File

@@ -39,8 +39,6 @@ import org.springframework.integration.channel.QueueChannel;
*/
public class MessagePublishingInterceptorTests {
private final ExpressionSource source = new TestExpressionSource();
private final MapBasedChannelResolver channelResolver = new MapBasedChannelResolver();
private final QueueChannel testChannel = new QueueChannel();
@@ -53,7 +51,8 @@ public class MessagePublishingInterceptorTests {
@Test
public void returnValue() {
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(source);
PublisherMetadataSource metadataSource = new TestPublisherMetadataSource();
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(metadataSource);
interceptor.setChannelResolver(channelResolver);
ProxyFactory pf = new ProxyFactory(new TestBeanImpl());
pf.addAdvice(interceptor);
@@ -63,23 +62,24 @@ public class MessagePublishingInterceptorTests {
assertNotNull(message);
assertEquals("test-foo", message.getPayload());
}
@Test
public void demoMethodNameMappingExpressionSource() {
Map<String, String> expressionMap = new HashMap<String, String>();
expressionMap.put("test", "#return");
MethodNameMappingExpressionSource source = new MethodNameMappingExpressionSource(expressionMap);
MethodNameMappingPublisherMetadataSource metadataSource = new MethodNameMappingPublisherMetadataSource(expressionMap);
Map<String, String> channelMap = new HashMap<String, String>();
channelMap.put("test", "c");
source.setChannelMap(channelMap);
metadataSource.setChannelMap(channelMap);
Map<String, Map<String, String>> headerExpressionMap = new HashMap<String, Map<String, String>>();
Map<String, String> headerExpressions = new HashMap<String, String>();
headerExpressions.put("bar", "#return");
headerExpressions.put("name", "'oleg'");
headerExpressionMap.put("test", headerExpressions);
source.setHeaderExpressionMap(headerExpressionMap);
metadataSource.setHeaderExpressionMap(headerExpressionMap);
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(source);
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(metadataSource);
interceptor.setChannelResolver(channelResolver);
ProxyFactory pf = new ProxyFactory(new TestBeanImpl());
pf.addAdvice(interceptor);
@@ -109,7 +109,7 @@ public class MessagePublishingInterceptorTests {
}
private static class TestExpressionSource implements ExpressionSource {
private static class TestPublisherMetadataSource implements PublisherMetadataSource {
public String getPayloadExpression(Method method) {
return "'test-' + #return";

View File

@@ -22,7 +22,7 @@
class="org.springframework.integration.aop.MessagePublishingInterceptor">
<constructor-arg>
<bean
class="org.springframework.integration.aop.MethodNameMappingExpressionSource">
class="org.springframework.integration.aop.MethodNameMappingPublisherMetadataSource">
<constructor-arg>
<map>
<entry key="setName" value="#return" />

View File

@@ -31,9 +31,9 @@ import org.springframework.integration.annotation.Payload;
* @author Mark Fisher
* @since 2.0
*/
public class MethodAnnotationExpressionSourceTests {
public class MethodAnnotationPublisherMetadataSourceTests {
private final MethodAnnotationExpressionSource source = new MethodAnnotationExpressionSource();
private final MethodAnnotationPublisherMetadataSource source = new MethodAnnotationPublisherMetadataSource();
@Test
@@ -94,7 +94,7 @@ public class MethodAnnotationExpressionSourceTests {
private static Method getMethod(String name, Class<?> ... params) {
try {
return MethodAnnotationExpressionSourceTests.class.getMethod(name, params);
return MethodAnnotationPublisherMetadataSourceTests.class.getMethod(name, params);
}
catch (Exception e) {
throw new RuntimeException("failed to resolve method", e);