INT-1243 (refactoring publishing-interceptor) Step 3: renamed <publisher> to <publishing-interceptor>, and renamed PublisherParser to PublishingInterceptorParser
This commit is contained in:
@@ -59,7 +59,7 @@ public class IntegrationNamespaceHandler extends AbstractIntegrationNamespaceHan
|
||||
registerBeanDefinitionParser("poller", new PollerParser());
|
||||
registerBeanDefinitionParser("annotation-config", new AnnotationConfigParser());
|
||||
registerBeanDefinitionParser("application-event-multicaster", new ApplicationEventMulticasterParser());
|
||||
registerBeanDefinitionParser("publisher", new PublisherParser());
|
||||
registerBeanDefinitionParser("publishing-interceptor", new PublishingInterceptorParser());
|
||||
registerBeanDefinitionParser("channel-interceptor", new GlobalChannelInterceptorParser());
|
||||
registerBeanDefinitionParser("converter", new ConverterParser());
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
@@ -32,21 +34,21 @@ import org.springframework.integration.channel.MapBasedChannelResolver;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Parser for the <publishing-interceptor> element.
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class PublisherParser extends AbstractBeanDefinitionParser {
|
||||
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());
|
||||
Map<String, Map<?,?>> mappings = this.getMappings(element, element.getAttribute("default-channel"), parserContext);
|
||||
|
||||
spelSourceBuilder.addConstructorArgValue(mappings.get("payload"));
|
||||
if (mappings.get("headers") != null) {
|
||||
spelSourceBuilder.addPropertyValue("headerExpressionMap", mappings.get("headers"));
|
||||
@@ -60,24 +62,21 @@ public class PublisherParser extends AbstractBeanDefinitionParser {
|
||||
BeanDefinitionReaderUtils.registerWithGeneratedName(chResolverBuilder.getBeanDefinition(), parserContext.getRegistry());
|
||||
String defaultChannel = StringUtils.hasText(element.getAttribute("default-channel")) ?
|
||||
element.getAttribute("default-channel") : IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME;
|
||||
|
||||
rootBuilder.addConstructorArgValue(spelSourceBuilder.getBeanDefinition());
|
||||
rootBuilder.addPropertyReference("channelResolver", chResolverName);
|
||||
rootBuilder.addPropertyReference("defaultChannel", defaultChannel);
|
||||
return rootBuilder.getBeanDefinition();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String,Map<?,?>> getMappings(Element element, String defaultChannel, ParserContext parserContext) {
|
||||
List<Element> mappings = DomUtils.getChildElementsByTagName(element, "method");
|
||||
Map<String, Map<?,?>> interceptorMappings = new HashMap<String, Map<?,?>>();
|
||||
Map<String, String> payloadExpressionMap = new HashMap<String, String>();
|
||||
Map<String, Map<String, String>> headersExpressionMap = new HashMap<String, Map<String, String>>();
|
||||
Map<String, String> channelMap = new HashMap<String, String>();
|
||||
ManagedMap resolvableChannelMap = new ManagedMap();
|
||||
ManagedMap<String, Object> resolvableChannelMap = new ManagedMap<String, Object>();
|
||||
if (mappings != null && mappings.size() > 0) {
|
||||
for (Element mapping : mappings) {
|
||||
|
||||
// set payloadMap
|
||||
String methodPattern = StringUtils.hasText(mapping.getAttribute("pattern")) ?
|
||||
mapping.getAttribute("pattern") : "*";
|
||||
@@ -2246,13 +2246,11 @@ Name of the header whose value to use.
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:element name="publisher">
|
||||
<xsd:element name="publishing-interceptor">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a MessagePublishingInterceptor which allows you to generate
|
||||
messages
|
||||
as a by-product of
|
||||
method invocations on Spring configured components.
|
||||
Defines a MessagePublishingInterceptor which allows you to generate messages
|
||||
as a by-product of method invocations on Spring configured components.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
|
||||
@@ -1,36 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans">
|
||||
|
||||
<beans:bean id="testBean"
|
||||
class="org.springframework.integration.config.xml.MessagePublishingInterceptorParserTests$TestBean" />
|
||||
<beans:bean id="defaultTestBean"
|
||||
class="org.springframework.integration.config.xml.MessagePublishingInterceptorParserTests$DefaultTestBean" />
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<beans:bean id="testBean" class="org.springframework.integration.config.xml.MessagePublishingInterceptorParserTests$TestBean" />
|
||||
|
||||
<beans:bean id="defaultTestBean" class="org.springframework.integration.config.xml.MessagePublishingInterceptorParserTests$DefaultTestBean" />
|
||||
|
||||
<publish-subscribe-channel id="defaultChannel"/>
|
||||
<publish-subscribe-channel id="echoChannel"/>
|
||||
<publish-subscribe-channel id="echoUpperCaseChannel"/>
|
||||
|
||||
|
||||
<aop:config>
|
||||
<aop:advisor advice-ref="interceptor" pointcut="bean(testBean)" />
|
||||
</aop:config>
|
||||
|
||||
<publisher id="interceptor" default-channel="defaultChannel">
|
||||
|
||||
<publishing-interceptor id="interceptor" default-channel="defaultChannel">
|
||||
<method pattern="echo" payload="'Echoing: ' + #return" channel="echoChannel">
|
||||
<header name="foo" value="bar"/>
|
||||
</method>
|
||||
<method pattern="echoDef*" payload="#return"/>
|
||||
</publisher>
|
||||
|
||||
</publishing-interceptor>
|
||||
|
||||
<aop:config>
|
||||
<aop:advisor advice-ref="anotherInterceptor" pointcut="bean(defaultTestBean)" />
|
||||
</aop:config>
|
||||
|
||||
<publisher id="anotherInterceptor"/>
|
||||
|
||||
<publishing-interceptor id="anotherInterceptor"/>
|
||||
|
||||
</beans:beans>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.config.xml;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
@@ -26,8 +27,8 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.channel.SubscribableChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageHandler;
|
||||
@@ -44,70 +45,78 @@ public class MessagePublishingInterceptorParserTests {
|
||||
|
||||
@Autowired
|
||||
private TestBean testBean;
|
||||
|
||||
@Autowired
|
||||
private DefaultTestBean defaultTestBean;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("defaultChannel")
|
||||
private SubscribableChannel defaultChannel;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("echoChannel")
|
||||
private SubscribableChannel echoChannel;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
|
||||
@Test
|
||||
public void validateDefaultChannelPublishing(){
|
||||
public void validateDefaultChannelPublishing() {
|
||||
MessageHandler handler = Mockito.mock(MessageHandler.class);
|
||||
defaultChannel.subscribe(handler);
|
||||
doAnswer(new Answer() {
|
||||
public Object answer(InvocationOnMock invocation) {
|
||||
Message<?> message = (Message<?>) invocation.getArguments()[0];
|
||||
assertEquals("hello",message.getPayload());
|
||||
return null;
|
||||
}})
|
||||
.when(handler).handleMessage((Message<?>) anyObject());
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
public Object answer(InvocationOnMock invocation) {
|
||||
Message<?> message = (Message<?>) invocation.getArguments()[0];
|
||||
assertEquals("hello", message.getPayload());
|
||||
return null;
|
||||
}
|
||||
}).when(handler).handleMessage((Message<?>) anyObject());
|
||||
testBean.echoDefaultChannel("hello");
|
||||
verify(handler, times(1)).handleMessage((Message<?>) anyObject());
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@Test
|
||||
public void validateEchoChannelPublishing(){
|
||||
public void validateEchoChannelPublishing() {
|
||||
MessageHandler handler = Mockito.mock(MessageHandler.class);
|
||||
echoChannel.subscribe(handler);
|
||||
doAnswer(new Answer() {
|
||||
public Object answer(InvocationOnMock invocation) {
|
||||
Message<?> message = (Message<?>) invocation.getArguments()[0];
|
||||
assertEquals("bar", message.getHeaders().get("foo"));
|
||||
assertEquals("Echoing: hello", message.getPayload());
|
||||
return null;
|
||||
}})
|
||||
.when(handler).handleMessage((Message<?>) anyObject());
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
public Object answer(InvocationOnMock invocation) {
|
||||
Message<?> message = (Message<?>) invocation.getArguments()[0];
|
||||
assertEquals("bar", message.getHeaders().get("foo"));
|
||||
assertEquals("Echoing: hello", message.getPayload());
|
||||
return null;
|
||||
}
|
||||
}).when(handler).handleMessage((Message<?>) anyObject());
|
||||
testBean.echo("hello");
|
||||
verify(handler, times(1)).handleMessage((Message<?>) anyObject());
|
||||
}
|
||||
|
||||
/**
|
||||
* Need to set 'debug' level
|
||||
*/
|
||||
@Test
|
||||
public void validateNullChannelPublishing(){
|
||||
public void validateNullChannelPublishing() {
|
||||
defaultTestBean.echo("hello");
|
||||
}
|
||||
|
||||
public static class TestBean{
|
||||
public String echo(String str){
|
||||
|
||||
public static class TestBean {
|
||||
|
||||
public String echo(String str) {
|
||||
return str;
|
||||
}
|
||||
public String echoUpperCase(String str){
|
||||
|
||||
public String echoUpperCase(String str) {
|
||||
return str.toUpperCase();
|
||||
}
|
||||
public String echoDefaultChannel(String str){
|
||||
|
||||
public String echoDefaultChannel(String str) {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
public static class DefaultTestBean{
|
||||
public String echo(String str){
|
||||
|
||||
|
||||
public static class DefaultTestBean {
|
||||
|
||||
public String echo(String str) {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user