formatting

This commit is contained in:
Mark Fisher
2010-11-09 18:37:37 -05:00
parent 7b1185f2f0
commit 026b9780b1

View File

@@ -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<GlobalChannelInterceptorWrapper> channelInterceptors;
private volatile List<GlobalChannelInterceptorWrapper> channelInterceptors;
private final Set<GlobalChannelInterceptorWrapper> positiveOrderInterceptors = new LinkedHashSet<GlobalChannelInterceptorWrapper>();
private final Set<GlobalChannelInterceptorWrapper> negativeOrderInterceptors = new LinkedHashSet<GlobalChannelInterceptorWrapper>();
GlobalChannelInterceptorBeanPostProcessor(List<GlobalChannelInterceptorWrapper> channelInterceptors){
GlobalChannelInterceptorBeanPostProcessor(List<GlobalChannelInterceptorWrapper> 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<ChannelInterceptor> getExistingInterceptors(MessageChannel channel){
DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel);
try {
Object iWrapper = channelAccessor.getPropertyValue("interceptors");
if (iWrapper != null){
DirectFieldAccessor iWrapperAccessor = new DirectFieldAccessor(iWrapper);
List<ChannelInterceptor> interceptors = (List<ChannelInterceptor>) 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<ChannelInterceptor> interceptors = this.getExistingInterceptors(channel);
if (interceptors != null){
if (interceptors != null) {
List<GlobalChannelInterceptorWrapper> tempInterceptors = new ArrayList<GlobalChannelInterceptorWrapper>();
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<GlobalChannelInterceptorWrapper>();
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<ChannelInterceptor> getExistingInterceptors(MessageChannel channel) {
DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel);
try {
Object interceptorListWrapper = channelAccessor.getPropertyValue("interceptors");
if (interceptorListWrapper != null) {
return (List<ChannelInterceptor>) new DirectFieldAccessor(interceptorListWrapper).getPropertyValue("interceptors");
}
}
catch (Exception e) {
// interceptors not supported by this channel, will return null
}
return null;
}
}