From 465010a31e3b89d54629b0773a74862dda251c54 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Fri, 6 May 2011 16:29:38 -0400 Subject: [PATCH] INT-1896 added support for handling proxied channels for GlobalChannelInterceptor --- ...alChannelInterceptorBeanPostProcessor.java | 10 +++++-- .../GlobalChannelInterceptorTests-context.xml | 17 +++++++++--- .../GlobalChannelInterceptorTests.java | 27 ++++++++++++++++--- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java index bd6ab8db4c..737ce0b05c 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java @@ -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 getExistingInterceptors(MessageChannel channel) { - DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel); + private List 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) new DirectFieldAccessor(interceptorListWrapper).getPropertyValue("interceptors"); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-context.xml index 450d93856a..06a1244bb0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-context.xml @@ -1,10 +1,12 @@ + 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"> @@ -62,4 +64,13 @@ + + + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java index b0ad8a1123..122a1379b8 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java @@ -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 channels = applicationContext.getBeansOfType(AbstractMessageChannel.class); + Map 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(); + } + + } }