From 7a72fbc9e70589c2ec2890680a72105a9cf4c9a0 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Sat, 27 Mar 2010 01:32:02 +0000 Subject: [PATCH] INT-789, Fixed the schema file, added one more test --- ...alChannelInterceptorBeanPostProcessor.java | 15 ++++++--- .../config/xml/spring-integration-2.0.xsd | 11 +++++-- ...balChannelInterceptorTests-all-context.xml | 23 ++++++++++++++ .../GlobalChannelInterceptorTests.java | 31 ++++++++++++++----- 4 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-all-context.xml diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java b/org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java index a0146556e7..b623e6a310 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorBeanPostProcessor.java @@ -95,10 +95,14 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (channelPatternMatches(beanName)){ - Assert.isTrue(bean instanceof AbstractMessageChannel, "channel interceptors can only be added to " + - "AbstractMessageChannel. Current implementation is: " + bean.getClass()); - logger.debug("Applying global interceptors on channel '" + beanName + "'"); - this.mergeInterceptorsToChannel((AbstractMessageChannel) bean, beanName); + if (bean instanceof AbstractMessageChannel){ + logger.debug("Applying global interceptors on channel '" + beanName + "'"); + this.mergeInterceptorsToChannel((AbstractMessageChannel) bean, beanName); + } else { + logger.warn("Attempt to add channel interceptors is unsuccessfull. Global channel interceptors " + + "can only be added to AbstractMessageChannel. Current implementation is: " + bean.getClass() + + " This might happen becouse you specified a single wild-card '*' in 'channel-name-pattern'"); + } } return bean; } @@ -177,6 +181,9 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess patterns = allAvailablePatters.toArray(new String[]{}); } for (String channelPattern : patterns) { + if (channelPattern.trim().equals("*")){ + return true; + } Pattern p = Pattern.compile(channelPattern.trim()); Matcher m = p.matcher(beanName); if (m.find()){ diff --git a/org.springframework.integration/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd b/org.springframework.integration/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd index b2a5fdde04..9a57e5b117 100644 --- a/org.springframework.integration/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd +++ b/org.springframework.integration/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd @@ -1,6 +1,7 @@ @@ -1655,12 +1656,16 @@ + + + Allows you to define channel interceptors to be applied globally + + - - + diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-all-context.xml b/org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-all-context.xml new file mode 100644 index 0000000000..d9dbfd8a39 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests-all-context.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java index 5333db8de5..ccca40367b 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/channel/interceptor/GlobalChannelInterceptorTests.java @@ -15,7 +15,6 @@ */ package org.springframework.integration.channel.interceptor; -import java.util.Collection; import java.util.List; import java.util.Map; @@ -167,13 +166,29 @@ public class GlobalChannelInterceptorTests { } } } - /** - * Will test failure if 'channel-name-pattern' filter points to a valid - * bean which is not an AbstractMessageChannel - */ - @Test(expected=BeanCreationException.class) - public void failGlobalInterceptorConfig(){ - new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-failed-context.xml", GlobalChannelInterceptorTests.class); + @SuppressWarnings("unchecked") + @Test + public void validateGlobalInterceptorsAllPattern(){ + ApplicationContext applicationContext = + new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-all-context.xml", GlobalChannelInterceptorTests.class); + Map channels = applicationContext.getBeansOfType(AbstractMessageChannel.class); + for (String channelName : channels.keySet()) { + AbstractMessageChannel channel = channels.get(channelName); + DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel); + Object iList = cAccessor.getPropertyValue("interceptors"); + DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList); + List interceptoList = (List) iAccessor.getPropertyValue("interceptors"); + if (channelName.equals("inputA")){ + SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{}); + Assert.assertTrue(inter.length == 2); + } else if (channelName.equals("inputB")){ + SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{}); + Assert.assertTrue(inter.length == 1); + } else if (channelName.equals("inputC")){ + SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{}); + Assert.assertTrue(inter.length == 1); + } + } } public static class SampleInterceptor implements ChannelInterceptor {