INT-814, INT-839 Added PublisherAnnotationBeanPostProcessor, added tests for documenting usage of @Publisher annotation and AOP-based MessagePublisherInterceptor
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
|
||||
/**
|
||||
* Will post process beans that contain @{@link Publisher} annotation.
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class PublisherAnnotationBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware, InitializingBean {
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
private MessageChannel defaultChannel;
|
||||
private PublisherAnnotationAdvisor advisor;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public PublisherAnnotationBeanPostProcessor(){}
|
||||
/**
|
||||
*
|
||||
* @param defaultChannel
|
||||
*/
|
||||
public PublisherAnnotationBeanPostProcessor(MessageChannel defaultChannel){
|
||||
this.defaultChannel = defaultChannel;
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (this.containsPublisherAnnotations(bean)){
|
||||
ProxyFactory pf = new ProxyFactory(bean);
|
||||
pf.addAdvisor(advisor);
|
||||
bean = pf.getProxy();
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public BeanFactory getBeanFactory() {
|
||||
return beanFactory;
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void afterPropertiesSet(){
|
||||
advisor = new PublisherAnnotationAdvisor();
|
||||
advisor.setBeanFactory(beanFactory);
|
||||
advisor.setDefaultChannel(defaultChannel);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
private boolean containsPublisherAnnotations(Object bean){
|
||||
Method[] methods = bean.getClass().getMethods();
|
||||
for (Method method : methods) {
|
||||
Annotation publisher = AnnotationUtils.findAnnotation(method, Publisher.class);
|
||||
if (publisher != null){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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:si="http://www.springframework.org/schema/integration">
|
||||
|
||||
<bean id="testBean"
|
||||
class="org.springframework.integration.aop.MessagePublishingAnnotationUsageTest$TestBean" />
|
||||
|
||||
<si:channel id="testChannel">
|
||||
<si:queue />
|
||||
</si:channel>
|
||||
|
||||
<bean class="org.springframework.integration.aop.PublisherAnnotationBeanPostProcessor"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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 junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class MessagePublishingAnnotationUsageTest {
|
||||
@Autowired
|
||||
private TestBean testBean;
|
||||
@Autowired
|
||||
private QueueChannel channel;
|
||||
@Test
|
||||
public void demoMessagePublishingInterceptor(){
|
||||
String name = testBean.setName("John", "Doe");
|
||||
Assert.assertNotNull(name);
|
||||
Message<?> message = channel.receive();
|
||||
Assert.assertNotNull(message);
|
||||
Assert.assertEquals("John Doe", message.getPayload());
|
||||
Assert.assertEquals("123", message.getHeaders().get("bar"));
|
||||
}
|
||||
|
||||
|
||||
public static class TestBean{
|
||||
@Publisher(value="#return", channel="testChannel", headers="bar='123'")
|
||||
public String setName(String fname, String lname){
|
||||
return fname + " " + lname;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,8 @@ import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -32,6 +34,7 @@ import org.springframework.integration.core.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class MessagePublishingInterceptorTests {
|
||||
@@ -60,6 +63,32 @@ public class MessagePublishingInterceptorTests {
|
||||
assertNotNull(message);
|
||||
assertEquals("foo", message.getPayload());
|
||||
}
|
||||
@Test
|
||||
public void demoMethodNameMappingExpressionSource() {
|
||||
Map<String, String> expressionMap = new HashMap<String, String>();
|
||||
expressionMap.put("test", "#return");
|
||||
MethodNameMappingExpressionSource source = new MethodNameMappingExpressionSource(expressionMap);
|
||||
Map<String, String> channelMap = new HashMap<String, String>();
|
||||
channelMap.put("test", "c");
|
||||
source.setChannelMap(channelMap);
|
||||
|
||||
Map<String, String[]> headerExpressionMap = new HashMap<String, String[]>();
|
||||
headerExpressionMap.put("test", new String[]{"bar=#return","name='oleg'"});
|
||||
source.setHeaderExpressionMap(headerExpressionMap);
|
||||
|
||||
|
||||
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(source);
|
||||
interceptor.setChannelResolver(channelResolver);
|
||||
ProxyFactory pf = new ProxyFactory(new TestBeanImpl());
|
||||
pf.addAdvice(interceptor);
|
||||
TestBean proxy = (TestBean) pf.getProxy();
|
||||
proxy.test();
|
||||
Message<?> message = testChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertEquals("foo", message.getPayload());
|
||||
assertEquals("foo", message.getHeaders().get("bar"));
|
||||
assertEquals("oleg", message.getHeaders().get("name"));
|
||||
}
|
||||
|
||||
|
||||
static interface TestBean {
|
||||
@@ -77,7 +106,6 @@ public class MessagePublishingInterceptorTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class TestExpressionSource implements ExpressionSource {
|
||||
|
||||
public String getArgumentMapName(Method method) {
|
||||
@@ -85,7 +113,7 @@ public class MessagePublishingInterceptorTests {
|
||||
}
|
||||
|
||||
public String[] getArgumentNames(Method method) {
|
||||
return new String[] { "a1", "a2" };
|
||||
return new String[] { "a1", "a2"};
|
||||
}
|
||||
|
||||
public String getReturnValueName(Method method) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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:si="http://www.springframework.org/schema/integration">
|
||||
|
||||
<bean id="testBean"
|
||||
class="org.springframework.integration.aop.MessagePublishingInterceptorUsageTest$TestBean" />
|
||||
|
||||
<si:channel id="testChannel">
|
||||
<si:queue />
|
||||
</si:channel>
|
||||
|
||||
<aop:config>
|
||||
<aop:advisor advice-ref="interceptor" pointcut="bean(testBean)" />
|
||||
</aop:config>
|
||||
|
||||
<bean id="interceptor"
|
||||
class="org.springframework.integration.aop.MessagePublishingInterceptor">
|
||||
<constructor-arg>
|
||||
<bean
|
||||
class="org.springframework.integration.aop.MethodNameMappingExpressionSource">
|
||||
<constructor-arg>
|
||||
<map>
|
||||
<entry key="setName" value="#return" />
|
||||
</map>
|
||||
</constructor-arg>
|
||||
<property name="headerExpressionMap">
|
||||
<map>
|
||||
<entry key="setName" value="foo='bar'" />
|
||||
</map>
|
||||
</property>
|
||||
<property name="channelMap">
|
||||
<map>
|
||||
<entry key="setName" value="channel" />
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
<property name="channelResolver">
|
||||
<bean
|
||||
class="org.springframework.integration.channel.MapBasedChannelResolver">
|
||||
<property name="channelMap">
|
||||
<map>
|
||||
<entry key="channel" value-ref="testChannel" />
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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 junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class MessagePublishingInterceptorUsageTest {
|
||||
@Autowired
|
||||
private TestBean testBean;
|
||||
@Autowired
|
||||
private QueueChannel channel;
|
||||
@Test
|
||||
public void demoMessagePublishingInterceptor(){
|
||||
String name = testBean.setName("John", "Doe");
|
||||
Assert.assertNotNull(name);
|
||||
Message<?> message = channel.receive();
|
||||
Assert.assertNotNull(message);
|
||||
Assert.assertEquals("John Doe", message.getPayload());
|
||||
Assert.assertEquals("bar", message.getHeaders().get("foo"));
|
||||
}
|
||||
|
||||
|
||||
public static class TestBean{
|
||||
public String setName(String fname, String lname){
|
||||
return fname + " " + lname;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user