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 c4c4eaca2a..8686d796f8 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 @@ -28,7 +28,7 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.OrderComparator; -import org.springframework.integration.channel.AbstractMessageChannel; +import org.springframework.integration.MessageChannel; import org.springframework.integration.channel.ChannelInterceptor; import org.springframework.util.PatternMatchUtils; import org.springframework.util.StringUtils; @@ -44,8 +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 Set positiveOrderInterceptors = new LinkedHashSet(); private final Set negativeOrderInterceptors = new LinkedHashSet(); @@ -67,10 +66,10 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - if (bean instanceof AbstractMessageChannel){ + if (bean instanceof MessageChannel){ logger.debug("Applying global interceptors on channel '" + beanName + "'"); - this.addInterceptorsIfExist((AbstractMessageChannel) bean, beanName); + this.addInterceptorsIfExist((MessageChannel) bean, beanName); } return bean; @@ -80,39 +79,53 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess * */ @SuppressWarnings("unchecked") - private List getExistingInterceptors(AbstractMessageChannel channel){ + private List getExistingInterceptors(MessageChannel channel){ DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel); - Object iWrapper = channelAccessor.getPropertyValue("interceptors"); - DirectFieldAccessor iWrapperAccessor = new DirectFieldAccessor(iWrapper); - List interceptors = (List) iWrapperAccessor.getPropertyValue("interceptors"); - return interceptors; + try { + Object iWrapper = channelAccessor.getPropertyValue("interceptors"); + if (iWrapper != null){ + DirectFieldAccessor iWrapperAccessor = new DirectFieldAccessor(iWrapper); + List interceptors = (List) iWrapperAccessor.getPropertyValue("interceptors"); + return interceptors; + } + } + catch (Exception e) { + logger.warn("Attempted to apply Global Channel iterceptors on the Channel that does not support interceptors"); + return null; + } + return null; } /* * */ - private void addInterceptorsIfExist(AbstractMessageChannel channel, String beanName){ + private void addInterceptorsIfExist(MessageChannel channel, String beanName){ List interceptors = this.getExistingInterceptors(channel); - List tempInterceptors = new ArrayList(); - for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : positiveOrderInterceptors) { - String[] patterns = globalChannelInterceptorWrapper.getPatterns(); - patterns = StringUtils.trimArrayElements(patterns); - if (PatternMatchUtils.simpleMatch(patterns, beanName)){ - tempInterceptors.add(globalChannelInterceptorWrapper); + if (interceptors != null){ + List tempInterceptors = new ArrayList(); + for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : positiveOrderInterceptors) { + String[] patterns = globalChannelInterceptorWrapper.getPatterns(); + patterns = StringUtils.trimArrayElements(patterns); + if (PatternMatchUtils.simpleMatch(patterns, beanName)){ + tempInterceptors.add(globalChannelInterceptorWrapper); + } } - } - Collections.sort(tempInterceptors, comparator); - interceptors.addAll(tempInterceptors); - - tempInterceptors = new ArrayList(); - for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : negativeOrderInterceptors) { - String[] patterns = globalChannelInterceptorWrapper.getPatterns(); - patterns = StringUtils.trimArrayElements(patterns); - if (PatternMatchUtils.simpleMatch(patterns, beanName)){ - tempInterceptors.add(globalChannelInterceptorWrapper); + Collections.sort(tempInterceptors, comparator); + interceptors.addAll(tempInterceptors); + + tempInterceptors = new ArrayList(); + for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : negativeOrderInterceptors) { + String[] patterns = globalChannelInterceptorWrapper.getPatterns(); + patterns = StringUtils.trimArrayElements(patterns); + if (PatternMatchUtils.simpleMatch(patterns, beanName)){ + tempInterceptors.add(globalChannelInterceptorWrapper); + } } + Collections.sort(tempInterceptors, comparator); + interceptors.addAll(0, tempInterceptors); + } + else { + logger.warn("Attempted to apply Global Channel iterceptors on the Channel that does not support interceptors"); } - Collections.sort(tempInterceptors, comparator); - interceptors.addAll(0, tempInterceptors); } /* * (non-Javadoc) @@ -120,13 +133,11 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess */ public void afterPropertiesSet() throws Exception { for (GlobalChannelInterceptorWrapper channelInterceptor : channelInterceptors) { - String[] patterns = channelInterceptor.getPatterns(); - for (String pattern : patterns) { - if (channelInterceptor.getOrder() >= 0){ - positiveOrderInterceptors.add(channelInterceptor); - } else { - negativeOrderInterceptors.add(channelInterceptor); - } + if (channelInterceptor.getOrder() >= 0){ + positiveOrderInterceptors.add(channelInterceptor); + } + else { + negativeOrderInterceptors.add(channelInterceptor); } } }