INT-1896 added support for handling proxied channels for GlobalChannelInterceptor

This commit is contained in:
Oleg Zhurakousky
2011-05-06 16:29:38 -04:00
parent 97fb9f679a
commit 465010a31e
3 changed files with 46 additions and 8 deletions

View File

@@ -25,6 +25,8 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.InitializingBean;
@@ -125,9 +127,13 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess
}
@SuppressWarnings("unchecked")
private List<ChannelInterceptor> getExistingInterceptors(MessageChannel channel) {
DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel);
private List<ChannelInterceptor> getExistingInterceptors(MessageChannel channel) {
try {
MessageChannel targetChannel = channel;
if (AopUtils.isAopProxy(channel)){
targetChannel = (MessageChannel) ((Advised)channel).getTargetSource().getTarget();
}
DirectFieldAccessor channelAccessor = new DirectFieldAccessor(targetChannel);
Object interceptorListWrapper = channelAccessor.getPropertyValue("interceptors");
if (interceptorListWrapper != null) {
return (List<ChannelInterceptor>) new DirectFieldAccessor(interceptorListWrapper).getPropertyValue("interceptors");

View File

@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<int:channel id="inputA">
<int:interceptors>
@@ -62,4 +64,13 @@
<bean id="channelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="seven"/>
<int:channel id="inputWithProxy"/>
<aop:config proxy-target-class="true">
<aop:pointcut id="testPointcut" expression="bean(inputWithProxy)"/>
<aop:advisor advice-ref="testInterceptor" pointcut-ref="testPointcut"/>
</aop:config>
<bean id="testInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests.TestInterceptor"/>
</beans>

View File

@@ -20,9 +20,13 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.Ordered;
@@ -40,12 +44,18 @@ import org.springframework.integration.test.util.TestUtils;
public class GlobalChannelInterceptorTests {
@Test
public void validateGlobalInterceptor() {
public void validateGlobalInterceptor() throws Exception{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"GlobalChannelInterceptorTests-context.xml", GlobalChannelInterceptorTests.class);
Map<String, AbstractMessageChannel> channels = applicationContext.getBeansOfType(AbstractMessageChannel.class);
Map<String, MessageChannel> channels = applicationContext.getBeansOfType(MessageChannel.class);
for (String channelName : channels.keySet()) {
AbstractMessageChannel channel = channels.get(channelName);
MessageChannel channel = channels.get(channelName);
if (channelName.equals("nullChannel")){
continue;
}
if (AopUtils.isAopProxy(channel)){
channel = (MessageChannel) ((Advised)channel).getTargetSource().getTarget();
}
List<?> interceptorList = TestUtils.getPropertyValue(channel, "interceptors.interceptors", List.class);
ChannelInterceptor[] interceptors = interceptorList.toArray(new ChannelInterceptor[] {});
if (channelName.equals("inputA")){ // 328741
@@ -91,6 +101,9 @@ public class GlobalChannelInterceptorTests {
Assert.assertEquals("interceptor-ten", interceptors[0].toString());
Assert.assertEquals("interceptor-eleven", interceptors[1].toString());
}
else if (channelName.equals("inputWithProxy")) {
Assert.assertTrue(interceptors.length == 6);
}
}
}
@@ -154,5 +167,13 @@ public class GlobalChannelInterceptorTests {
this.order = order;
}
}
public static class TestInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable {
return invocation.proceed();
}
}
}