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 5e252a5747..c4c4eaca2a 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 @@ -17,13 +17,9 @@ package org.springframework.integration.channel.interceptor; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -34,6 +30,8 @@ import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.OrderComparator; import org.springframework.integration.channel.AbstractMessageChannel; import org.springframework.integration.channel.ChannelInterceptor; +import org.springframework.util.PatternMatchUtils; +import org.springframework.util.StringUtils; /** * Will apply global interceptors to channels (<channel-interceptor>). @@ -46,7 +44,7 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess private final static Log logger = LogFactory.getLog(GlobalChannelInterceptorBeanPostProcessor.class); private final OrderComparator comparator = new OrderComparator(); private List channelInterceptors; - private final Map compiledPatterns = new HashMap(); + //private final Map compiledPatterns = new HashMap(); private final Set positiveOrderInterceptors = new LinkedHashSet(); private final Set negativeOrderInterceptors = new LinkedHashSet(); @@ -97,18 +95,9 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess List tempInterceptors = new ArrayList(); for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : positiveOrderInterceptors) { String[] patterns = globalChannelInterceptorWrapper.getPatterns(); - for (String channelPattern : patterns) { - channelPattern = channelPattern.trim(); - if (channelPattern.equals("*")){ - tempInterceptors.add(globalChannelInterceptorWrapper); - } else { - Pattern pattern = compiledPatterns.get(channelPattern); - - Matcher m = pattern.matcher(beanName); - if (m.find()){ - tempInterceptors.add(globalChannelInterceptorWrapper); - } - } + patterns = StringUtils.trimArrayElements(patterns); + if (PatternMatchUtils.simpleMatch(patterns, beanName)){ + tempInterceptors.add(globalChannelInterceptorWrapper); } } Collections.sort(tempInterceptors, comparator); @@ -117,13 +106,9 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess tempInterceptors = new ArrayList(); for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : negativeOrderInterceptors) { String[] patterns = globalChannelInterceptorWrapper.getPatterns(); - for (String channelPattern : patterns) { - channelPattern = channelPattern.trim(); - Pattern pattern = compiledPatterns.get(channelPattern); - Matcher m = pattern.matcher(beanName); - if (m.find()){ - tempInterceptors.add(globalChannelInterceptorWrapper); - } + patterns = StringUtils.trimArrayElements(patterns); + if (PatternMatchUtils.simpleMatch(patterns, beanName)){ + tempInterceptors.add(globalChannelInterceptorWrapper); } } Collections.sort(tempInterceptors, comparator); @@ -137,14 +122,6 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess for (GlobalChannelInterceptorWrapper channelInterceptor : channelInterceptors) { String[] patterns = channelInterceptor.getPatterns(); for (String pattern : patterns) { - pattern = pattern.trim(); - if (!pattern.equals("*" )){ - Pattern p = compiledPatterns.get(pattern); - if (p == null){ - p = Pattern.compile(pattern); - compiledPatterns.put(pattern, p); - } - } if (channelInterceptor.getOrder() >= 0){ positiveOrderInterceptors.add(channelInterceptor); } else { 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 d8ad28913d..450d93856a 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 @@ -16,6 +16,8 @@ + + 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 ce39d33d2e..e2dbe388c2 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 @@ -15,6 +15,7 @@ */ package org.springframework.integration.channel.interceptor; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -31,6 +32,7 @@ import org.springframework.integration.channel.ChannelInterceptor; /** * @author Oleg Zhurakousky + * @author Dave Turanski * @since 2.0 */ @SuppressWarnings("all") @@ -46,9 +48,9 @@ public class GlobalChannelInterceptorTests { DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel); Object iList = cAccessor.getPropertyValue("interceptors"); DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList); - List interceptoList = (List) iAccessor.getPropertyValue("interceptors"); + List interceptorList = (List) iAccessor.getPropertyValue("interceptors"); if (channelName.equals("inputA")){ // 328741 - ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{}); + ChannelInterceptor[] inter = interceptorList.toArray(new ChannelInterceptor[]{}); Assert.assertTrue(inter.length ==10); Assert.assertEquals("interceptor-three", inter[0].toString()); Assert.assertEquals("interceptor-two", inter[1].toString()); @@ -63,7 +65,7 @@ public class GlobalChannelInterceptorTests { } else if (channelName.equals("inputB")){ - ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{}); + ChannelInterceptor[] inter = interceptorList.toArray(new ChannelInterceptor[]{}); Assert.assertTrue(inter.length == 6); Assert.assertEquals("interceptor-three", inter[0].toString()); Assert.assertEquals("interceptor-two", inter[1].toString()); @@ -74,7 +76,7 @@ public class GlobalChannelInterceptorTests { } else if (channelName.equals("foo")){ - ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{}); + ChannelInterceptor[] inter = interceptorList.toArray(new ChannelInterceptor[]{}); Assert.assertTrue(inter.length == 6); Assert.assertEquals("interceptor-two", inter[0].toString()); Assert.assertEquals("interceptor-five", inter[1].toString()); @@ -85,7 +87,7 @@ public class GlobalChannelInterceptorTests { } else if (channelName.equals("bar")){ - ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{}); + ChannelInterceptor[] inter = interceptorList.toArray(new ChannelInterceptor[]{}); Assert.assertTrue(inter.length == 4); Assert.assertEquals("interceptor-eight", inter[0].toString()); Assert.assertEquals("interceptor-seven", inter[1].toString()); @@ -94,13 +96,30 @@ public class GlobalChannelInterceptorTests { } else if (channelName.equals("baz")){ - ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{}); + ChannelInterceptor[] inter = interceptorList.toArray(new ChannelInterceptor[]{}); Assert.assertTrue(inter.length == 2); Assert.assertEquals("interceptor-ten", inter[0].toString()); Assert.assertEquals("interceptor-eleven", inter[1].toString()); } } } + + @Test + public void testWildCardPatternMatch(){ + ApplicationContext applicationContext = + new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-context.xml", GlobalChannelInterceptorTests.class); + AbstractMessageChannel channel = applicationContext.getBean("inpuC",AbstractMessageChannel.class); + DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel); + Object iList = cAccessor.getPropertyValue("interceptors"); + DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList); + List interceptorList = (List) iAccessor.getPropertyValue("interceptors"); + List interceptorNames = new ArrayList(); + for (Object interceptor: interceptorList){ + interceptorNames.add(interceptor.toString()); + } + Assert.assertTrue(interceptorNames.contains("interceptor-ten")); + Assert.assertTrue(interceptorNames.contains("interceptor-eleven")); + } public static class SampleInterceptor implements ChannelInterceptor {