INT-1421 - changed to use PatternMatchUtils

This commit is contained in:
Oleg Zhurakousky
2010-09-03 16:26:11 +00:00
parent 3e9c6545e0
commit 74c6ef95ec
3 changed files with 36 additions and 38 deletions

View File

@@ -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<GlobalChannelInterceptorWrapper> channelInterceptors;
private final Map<String, Pattern> compiledPatterns = new HashMap<String, Pattern>();
//private final Map<String, Pattern> compiledPatterns = new HashMap<String, Pattern>();
private final Set<GlobalChannelInterceptorWrapper> positiveOrderInterceptors = new LinkedHashSet<GlobalChannelInterceptorWrapper>();
private final Set<GlobalChannelInterceptorWrapper> negativeOrderInterceptors = new LinkedHashSet<GlobalChannelInterceptorWrapper>();
@@ -97,18 +95,9 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess
List<GlobalChannelInterceptorWrapper> tempInterceptors = new ArrayList<GlobalChannelInterceptorWrapper>();
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<GlobalChannelInterceptorWrapper>();
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 {

View File

@@ -16,6 +16,8 @@
<int:channel id="inputB">
<int:queue capacity="1"/>
</int:channel>
<int:channel id="inpuC"/>
<int:publish-subscribe-channel id="foo"/>

View File

@@ -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<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
List<SampleInterceptor> interceptorList = (List<SampleInterceptor>) 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<SampleInterceptor> interceptorList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
List<String> interceptorNames = new ArrayList<String>();
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 {