From 026b9780b174dc71121cb57831703393eefee518 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 9 Nov 2010 18:37:37 -0500 Subject: [PATCH] formatting --- ...alChannelInterceptorBeanPostProcessor.java | 128 ++++++++---------- 1 file changed, 60 insertions(+), 68 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 8686d796f8..d124715a53 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.channel.interceptor; import java.util.ArrayList; @@ -23,6 +24,7 @@ import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.BeansException; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.InitializingBean; @@ -36,109 +38,99 @@ import org.springframework.util.StringUtils; /** * Will apply global interceptors to channels (<channel-interceptor>). * - * * @author Oleg Zhurakousky + * @author Mark Fisher * @since 2.0 */ -final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcessor, InitializingBean{ - private final static Log logger = LogFactory.getLog(GlobalChannelInterceptorBeanPostProcessor.class); +final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcessor, InitializingBean { + + private static final Log logger = LogFactory.getLog(GlobalChannelInterceptorBeanPostProcessor.class); + + private final OrderComparator comparator = new OrderComparator(); - private List channelInterceptors; - + + private volatile List channelInterceptors; + private final Set positiveOrderInterceptors = new LinkedHashSet(); + private final Set negativeOrderInterceptors = new LinkedHashSet(); - GlobalChannelInterceptorBeanPostProcessor(List channelInterceptors){ + + GlobalChannelInterceptorBeanPostProcessor(List channelInterceptors) { this.channelInterceptors = channelInterceptors; } - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String) - */ - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - return bean; + + + public void afterPropertiesSet() throws Exception { + for (GlobalChannelInterceptorWrapper channelInterceptor : this.channelInterceptors) { + if (channelInterceptor.getOrder() >= 0) { + this.positiveOrderInterceptors.add(channelInterceptor); + } + else { + this.negativeOrderInterceptors.add(channelInterceptor); + } + } } - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String) - */ - public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { - - if (bean instanceof MessageChannel){ - - logger.debug("Applying global interceptors on channel '" + beanName + "'"); - this.addInterceptorsIfExist((MessageChannel) bean, beanName); + + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof MessageChannel) { + if (logger.isDebugEnabled()) { + logger.debug("Applying global interceptors on channel '" + beanName + "'"); + } + this.addMatchingInterceptors((MessageChannel) bean, beanName); } - return bean; } - /* - * - */ - @SuppressWarnings("unchecked") - private List getExistingInterceptors(MessageChannel channel){ - DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel); - 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; + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + return bean; } - /* - * + + /** + * Adds any interceptor whose pattern matches against the channel's name. */ - private void addInterceptorsIfExist(MessageChannel channel, String beanName){ + private void addMatchingInterceptors(MessageChannel channel, String beanName) { List interceptors = this.getExistingInterceptors(channel); - if (interceptors != null){ + if (interceptors != null) { List tempInterceptors = new ArrayList(); - for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : positiveOrderInterceptors) { + for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.positiveOrderInterceptors) { String[] patterns = globalChannelInterceptorWrapper.getPatterns(); patterns = StringUtils.trimArrayElements(patterns); - if (PatternMatchUtils.simpleMatch(patterns, beanName)){ + if (PatternMatchUtils.simpleMatch(patterns, beanName)) { tempInterceptors.add(globalChannelInterceptorWrapper); } } - Collections.sort(tempInterceptors, comparator); + Collections.sort(tempInterceptors, this.comparator); interceptors.addAll(tempInterceptors); - tempInterceptors = new ArrayList(); - for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : negativeOrderInterceptors) { + for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.negativeOrderInterceptors) { String[] patterns = globalChannelInterceptorWrapper.getPatterns(); patterns = StringUtils.trimArrayElements(patterns); - if (PatternMatchUtils.simpleMatch(patterns, beanName)){ + 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"); + else if (logger.isDebugEnabled()) { + logger.debug("Global Channel interceptors will not be applied to Channel: " + beanName); } } - /* - * (non-Javadoc) - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - public void afterPropertiesSet() throws Exception { - for (GlobalChannelInterceptorWrapper channelInterceptor : channelInterceptors) { - if (channelInterceptor.getOrder() >= 0){ - positiveOrderInterceptors.add(channelInterceptor); - } - else { - negativeOrderInterceptors.add(channelInterceptor); + + @SuppressWarnings("unchecked") + private List getExistingInterceptors(MessageChannel channel) { + DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel); + try { + Object interceptorListWrapper = channelAccessor.getPropertyValue("interceptors"); + if (interceptorListWrapper != null) { + return (List) new DirectFieldAccessor(interceptorListWrapper).getPropertyValue("interceptors"); } } + catch (Exception e) { + // interceptors not supported by this channel, will return null + } + return null; } + }