This commit is contained in:
Oleg Zhurakousky
2010-06-17 22:54:26 +00:00
parent 04c592ba8b
commit 2bb02b13ae
12 changed files with 313 additions and 489 deletions

View File

@@ -18,8 +18,10 @@ 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;
@@ -32,36 +34,10 @@ 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.CollectionUtils;
/**
* Will apply global interceptors to channels (<channel-interceptor-chain>). Since global interceptors
* could be Ordered or un-Ordered they will be sorted before merged with other interceptors in the channel.
* Sorting will only be done within the given interceptor chain which itself defines 'order' attribute
* essentially creating a group of ordered interceptors which are ordered internally and then these chain
* groups are also ordered. For example:
* <pre>
* channel-interceptor-chain channel-name-pattern="foo" order="5" - positive order value means AFTER local channel interceptors
* Ordered-global interceptor (4)
* Ordered-global interceptor (1)
* channel-interceptor-chain
* channel-interceptor-chain channel-name-pattern="foo" order="-1" - negative order value means AFTER local channel interceptors
* Ordered-global interceptor (3)
* Ordered-global interceptor (10)
* channel-interceptor-chain
* Will apply global interceptors to channels (&lt;channel-interceptor&gt;).
*
* channel id="foo"
* Ordered-in-channel interceptor (1)
* channel
*
* will result in channel with the following interceptors
* Channel "foo"
* Ordered-global interceptor (3)
* Ordered-global interceptor (10)
* Ordered-in-channel interceptor (1)
* Ordered-global interceptor (1)
* Ordered-global interceptor (4)
* </pre>
*
* @author Oleg Zhurakousky
* @since 2.0
@@ -69,18 +45,17 @@ import org.springframework.util.CollectionUtils;
final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcessor, InitializingBean{
private final static Log logger = LogFactory.getLog(GlobalChannelInterceptorBeanPostProcessor.class);
private final OrderComparator comparator = new OrderComparator();
private List<String> allAvailablePatters;
private List<GlobalChannelInterceptorChain> globalInterceptors;
private List<GlobalChannelInterceptorWrapper> channelInterceptors;
private final Map<String, Pattern> compiledPatterns = new HashMap<String, Pattern>();
private List<GlobalChannelInterceptorChain> positiveOrderChains = new ArrayList<GlobalChannelInterceptorChain>();
private List<GlobalChannelInterceptorChain> negativeOrderChains = new ArrayList<GlobalChannelInterceptorChain>();
private final Set<GlobalChannelInterceptorWrapper> positiveOrderInterceptors = new LinkedHashSet<GlobalChannelInterceptorWrapper>();
private final Set<GlobalChannelInterceptorWrapper> negativeOrderInterceptors = new LinkedHashSet<GlobalChannelInterceptorWrapper>();
/**
*
*
* @param globalInterceptors
*/
GlobalChannelInterceptorBeanPostProcessor(List<GlobalChannelInterceptorChain> globalInterceptors){
this.globalInterceptors = globalInterceptors;
GlobalChannelInterceptorBeanPostProcessor(List<GlobalChannelInterceptorWrapper> channelInterceptors){
this.channelInterceptors = channelInterceptors;
}
/*
* (non-Javadoc)
@@ -96,71 +71,16 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess
*/
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (channelPatternMatches(beanName)){
if (bean instanceof AbstractMessageChannel){
logger.debug("Applying global interceptors on channel '" + beanName + "'");
this.mergeInterceptorsToChannel((AbstractMessageChannel) bean, beanName);
} else {
logger.warn("Attempt to add channel interceptors is unsuccessfull. Global channel interceptors " +
"can only be added to AbstractMessageChannel. Current implementation is: " + bean.getClass() +
" This might happen becouse you specified a single wild-card '*' in 'channel-name-pattern'");
}
}
if (bean instanceof AbstractMessageChannel){
logger.debug("Applying global interceptors on channel '" + beanName + "'");
this.addInterceptorsIfExist((AbstractMessageChannel) bean, beanName);
}
return bean;
}
/**
*
* @param channel
* @param channelName
*/
private void mergeInterceptorsToChannel(AbstractMessageChannel channel, String channelName){
List<ChannelInterceptor> tInt = null;
List<ChannelInterceptor> interceptors = this.getExistingInterceptors(channel);
// POSITIVE
List<GlobalChannelInterceptorChain> tempPositiveInterceptorChains = new ArrayList<GlobalChannelInterceptorChain>();
for (GlobalChannelInterceptorChain positiveOrderChain : positiveOrderChains) {
if (channelPatternMatches(channelName, positiveOrderChain.getPatterns())){
tempPositiveInterceptorChains.add(positiveOrderChain);
}
}
// sort chain
Collections.sort(tempPositiveInterceptorChains, comparator);
for (GlobalChannelInterceptorChain globalChannelInterceptorChain : tempPositiveInterceptorChains) {
tInt = globalChannelInterceptorChain.getInterceptors();
// sort within the chain
Collections.sort(tInt, comparator);
interceptors.addAll(tInt);
}
// NEGATIVE
List<GlobalChannelInterceptorChain> tempNegativeInterceptorChains = new ArrayList<GlobalChannelInterceptorChain>();
for (GlobalChannelInterceptorChain negativeOrderChain : negativeOrderChains) {
if (channelPatternMatches(channelName, negativeOrderChain.getPatterns())){
tempNegativeInterceptorChains.add(negativeOrderChain);
}
}
// sort chains
Collections.sort(tempNegativeInterceptorChains, comparator);
for (GlobalChannelInterceptorChain globalChannelInterceptorChain : tempNegativeInterceptorChains) {
tInt = globalChannelInterceptorChain.getInterceptors();
// sort within the chain
Collections.sort(tInt, comparator);
interceptors.addAll(0, tInt);
}
}
/*
*
*/
private void filterPositiveNegativeOrderChains(){
for (GlobalChannelInterceptorChain globalInterceptorChain : globalInterceptors) {
if (globalInterceptorChain.getOrder() < 0){
negativeOrderChains.add(globalInterceptorChain);
} else {
positiveOrderChains.add(globalInterceptorChain);
}
}
}
/*
*
*/
@@ -175,38 +95,65 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess
/*
*
*/
private boolean channelPatternMatches(String beanName, String... patternsToMatch){
String[] patterns = null;
if (patternsToMatch.length > 0){
patterns = patternsToMatch;
} else {
patterns = allAvailablePatters.toArray(new String[]{});
}
for (String channelPattern : patterns) {
channelPattern = channelPattern.trim();
if (channelPattern.trim().equals("*")){
return true;
}
Pattern p = compiledPatterns.get(channelPattern);
if (p == null){
p = Pattern.compile(channelPattern);
compiledPatterns.put(channelPattern, p);
}
Matcher m = p.matcher(beanName);
if (m.find()){
return true;
private void addInterceptorsIfExist(AbstractMessageChannel channel, String beanName){
List<ChannelInterceptor> interceptors = this.getExistingInterceptors(channel);
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);
}
}
}
}
return false;
Collections.sort(tempInterceptors, comparator);
interceptors.addAll(tempInterceptors);
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);
}
}
}
Collections.sort(tempInterceptors, comparator);
interceptors.addAll(0, tempInterceptors);
}
@SuppressWarnings("unchecked")
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
allAvailablePatters = new ArrayList<String>();
for (GlobalChannelInterceptorChain globalInterceptorchain : globalInterceptors) {
allAvailablePatters.addAll(CollectionUtils.arrayToList(globalInterceptorchain.getPatterns()));
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 {
negativeOrderInterceptors.add(channelInterceptor);
}
}
}
this.filterPositiveNegativeOrderChains();
logger.info("Initialized: '" + this.getClass().getSimpleName() + "' to apply global channel interceptors");
}
}

View File

@@ -1,54 +0,0 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.channel.interceptor;
import java.util.List;
import org.springframework.core.Ordered;
import org.springframework.integration.channel.ChannelInterceptor;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
final class GlobalChannelInterceptorChain implements Ordered{
private List<ChannelInterceptor> interceptors;
private String[] patterns;
private int order;
public GlobalChannelInterceptorChain(List<ChannelInterceptor> interceptors, String[] patterns, int order){
this.interceptors = interceptors;
this.patterns = patterns;
this.order = order;
}
List<ChannelInterceptor> getInterceptors(){
return interceptors;
}
String[] getPatterns() {
return patterns;
}
public String toString(){
return interceptors.toString();
}
public int getOrder() {
return order;
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.channel.interceptor;
import org.springframework.core.Ordered;
import org.springframework.integration.channel.ChannelInterceptor;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public class GlobalChannelInterceptorWrapper implements ChannelInterceptor, Ordered{
private ChannelInterceptor channelInterceptor;
private String[] patterns;
private int order;
public GlobalChannelInterceptorWrapper(ChannelInterceptor channelInterceptor){
this.channelInterceptor = channelInterceptor;
// will set initial order for this interceptor wrapper to be the same as the
// underlying interceptor. Could be overridden with setOrder() method
if (channelInterceptor instanceof Ordered){
order = ((Ordered)channelInterceptor).getOrder();
}
}
public int getOrder() {
return order;
}
/**
*
* @param order
*/
public void setOrder(int order) {
this.order = order;
}
public ChannelInterceptor getChannelInterceptor() {
return channelInterceptor;
}
public String[] getPatterns() {
return patterns;
}
public void setPatterns(String[] patterns) {
this.patterns = patterns;
}
public Message<?> postReceive(Message<?> message, MessageChannel channel) {
return channelInterceptor.postReceive(message, channel);
}
public void postSend(Message<?> message, MessageChannel channel,
boolean sent) {
channelInterceptor.postSend(message, channel, sent);
}
public boolean preReceive(MessageChannel channel) {
return channelInterceptor.preReceive(channel);
}
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return channelInterceptor.preSend(message, channel);
}
public String toString(){
return channelInterceptor.toString();
}
}

View File

@@ -15,10 +15,7 @@
*/
package org.springframework.integration.config.xml;
import java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
@@ -26,10 +23,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
@@ -40,12 +35,10 @@ import org.w3c.dom.Element;
*/
public class GlobalChannelInterceptorParser extends AbstractBeanDefinitionParser {
private static final String CONFIG_PACKAGE = IntegrationNamespaceUtils.BASE_PACKAGE + ".channel.interceptor.";
private final ManagedList<RuntimeBeanReference> globalInterceptorChains = new ManagedList<RuntimeBeanReference>();
private final String GLOBAL_NAME_PATTERN_ATTR = "channel-name-pattern";
private final String REF_ATTR = "ref";
private final String BEAN_ATTR = "bean";
private final String ORDER_ATTR = "order";
private final String INTERCEPTOR_CHAIN_CLASS = "GlobalChannelInterceptorChain";
private final ManagedList<RuntimeBeanReference> globalInterceptors = new ManagedList<RuntimeBeanReference>();
private final static String CHANNELL_NAME_PATTERN_ATTR = "pattern";
private final static String REF_ATTR = "ref";
private final static String ORDER_ATTR = "order";
private final String GLOBAL_POST_PROCESSOR_CLASS = "GlobalChannelInterceptorBeanPostProcessor";
private boolean postProcessorCreated;
@@ -56,35 +49,71 @@ public class GlobalChannelInterceptorParser extends AbstractBeanDefinitionParser
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
this.createAndRegisterGlobalPostProcessorIfNeeded(parserContext);
List<Element> interceptorElements = DomUtils.getChildElementsByTagName(element, new String[]{REF_ATTR, BEAN_ATTR});
int order = this.getOrderAttribute(element);
String channelPattern = element.getAttribute(CHANNELL_NAME_PATTERN_ATTR);
BeanDefinitionBuilder globalChannelInterceptorBuilder =
BeanDefinitionBuilder.genericBeanDefinition(CONFIG_PACKAGE + INTERCEPTOR_CHAIN_CLASS);
String[] channelPatterns = element.getAttribute(GLOBAL_NAME_PATTERN_ATTR).split(",");
int order = this.getOrderAttribute(element);
ManagedList<RuntimeBeanReference> adviceChain = new ManagedList<RuntimeBeanReference>();
for (Element interceptorElement : interceptorElements) {
if (interceptorElement.getNodeName().equals(BEAN_ATTR)){
BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
BeanDefinitionHolder holder = delegate.parseBeanDefinitionElement(interceptorElement);
// needed for p: namespace
holder = delegate.decorateBeanDefinitionIfRequired(interceptorElement, holder);
parserContext.registerBeanComponent(new BeanComponentDefinition(holder));
adviceChain.add(new RuntimeBeanReference(holder.getBeanName()));
} else if (interceptorElement.getNodeName().equals(REF_ATTR)) {
String ref = interceptorElement.getAttribute(BEAN_ATTR);
adviceChain.add(new RuntimeBeanReference(ref));
}
BeanDefinitionBuilder.genericBeanDefinition(CONFIG_PACKAGE + "GlobalChannelInterceptorWrapper");
BeanComponentDefinition interceptorBeanDefinition = IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
if (interceptorBeanDefinition != null){
globalChannelInterceptorBuilder.addConstructorArgValue(interceptorBeanDefinition);
} else {
String beanName = element.getAttribute(REF_ATTR);
globalChannelInterceptorBuilder.addConstructorArgValue(new RuntimeBeanReference(beanName));
}
globalChannelInterceptorBuilder.addConstructorArgValue(adviceChain);
globalChannelInterceptorBuilder.addConstructorArgValue(channelPatterns);
globalChannelInterceptorBuilder.addConstructorArgValue(order);
AbstractBeanDefinition interceptorChainDef = globalChannelInterceptorBuilder.getBeanDefinition();
String interceptorChainName =
BeanDefinitionReaderUtils.generateBeanName(interceptorChainDef, parserContext.getRegistry());
parserContext.registerBeanComponent(new BeanComponentDefinition(interceptorChainDef, interceptorChainName));
globalInterceptorChains.add(new RuntimeBeanReference(interceptorChainName));
globalChannelInterceptorBuilder.addPropertyValue("order", order);
String[] patterns = null;
if (StringUtils.hasText(channelPattern)){
patterns = StringUtils.commaDelimitedListToStringArray(channelPattern);
} else {
patterns = new String[]{"*"};
}
globalChannelInterceptorBuilder.addPropertyValue("patterns", patterns);
String beanName =
BeanDefinitionReaderUtils.generateBeanName(globalChannelInterceptorBuilder.getBeanDefinition(), parserContext.getRegistry());
parserContext.registerBeanComponent(new BeanComponentDefinition(globalChannelInterceptorBuilder.getBeanDefinition(), beanName));
globalInterceptors.add(new RuntimeBeanReference(beanName));
//
//
// *
//
//
//
//
//
// List<Element> interceptorElements = DomUtils.getChildElementsByTagName(element, new String[]{REF_ATTR, BEAN_ATTR});
//
// BeanDefinitionBuilder globalChannelInterceptorBuilder =
// BeanDefinitionBuilder.genericBeanDefinition(CONFIG_PACKAGE + INTERCEPTOR_CHAIN_CLASS);
// String[] channelPatterns = element.getAttribute(CHANNELL_NAME_PATTERN_ATTR).split(",");
// int order = this.getOrderAttribute(element);
//
// ManagedList<RuntimeBeanReference> adviceChain = new ManagedList<RuntimeBeanReference>();
// for (Element interceptorElement : interceptorElements) {
// if (interceptorElement.getNodeName().equals(BEAN_ATTR)){
// BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
// BeanDefinitionHolder holder = delegate.parseBeanDefinitionElement(interceptorElement);
// // needed for p: namespace
// holder = delegate.decorateBeanDefinitionIfRequired(interceptorElement, holder);
// parserContext.registerBeanComponent(new BeanComponentDefinition(holder));
// adviceChain.add(new RuntimeBeanReference(holder.getBeanName()));
// } else if (interceptorElement.getNodeName().equals(REF_ATTR)) {
// String ref = interceptorElement.getAttribute(BEAN_ATTR);
// adviceChain.add(new RuntimeBeanReference(ref));
// }
// }
// globalChannelInterceptorBuilder.addConstructorArgValue(adviceChain);
// globalChannelInterceptorBuilder.addConstructorArgValue(channelPatterns);
// globalChannelInterceptorBuilder.addConstructorArgValue(order);
// AbstractBeanDefinition interceptorChainDef = globalChannelInterceptorBuilder.getBeanDefinition();
// String interceptorChainName =
// BeanDefinitionReaderUtils.generateBeanName(interceptorChainDef, parserContext.getRegistry());
// parserContext.registerBeanComponent(new BeanComponentDefinition(interceptorChainDef, interceptorChainName));
// globalInterceptorChains.add(new RuntimeBeanReference(interceptorChainName));
return null;
}
/*
@@ -105,7 +134,7 @@ public class GlobalChannelInterceptorParser extends AbstractBeanDefinitionParser
BeanDefinitionBuilder postProcessorBuilder =
BeanDefinitionBuilder.genericBeanDefinition(CONFIG_PACKAGE + GLOBAL_POST_PROCESSOR_CLASS);
BeanDefinition beanDef = postProcessorBuilder.getBeanDefinition();
postProcessorBuilder.addConstructorArgValue(globalInterceptorChains);
postProcessorBuilder.addConstructorArgValue(globalInterceptors);
String beanName =
BeanDefinitionReaderUtils.generateBeanName(beanDef, parserContext.getRegistry());
parserContext.registerBeanComponent(new BeanComponentDefinition(beanDef, beanName));

View File

@@ -58,7 +58,7 @@ public class IntegrationNamespaceHandler extends AbstractIntegrationNamespaceHan
registerBeanDefinitionParser("annotation-config", new AnnotationConfigParser());
registerBeanDefinitionParser("application-event-multicaster", new ApplicationEventMulticasterParser());
registerBeanDefinitionParser("publisher", new PublisherParser());
registerBeanDefinitionParser("channel-interceptor-chain", new GlobalChannelInterceptorParser());
registerBeanDefinitionParser("channel-interceptor", new GlobalChannelInterceptorParser());
}
}

View File

@@ -2172,7 +2172,7 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="channel-interceptor-chain">
<xsd:element name="channel-interceptor">
<xsd:annotation>
<xsd:documentation>
Allows you to define channel interceptors to be applied globally.
@@ -2180,13 +2180,11 @@
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="beans:ref" />
<xsd:choice minOccurs="0" maxOccurs="1">
<xsd:element ref="beans:bean" />
</xsd:choice>
</xsd:sequence>
<xsd:attribute name="channel-name-pattern" type="xsd:string"
use="required">
<xsd:attribute name="pattern" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>
[REQUIRED] Channel name(s) or patterns. To specify more than one channel use
@@ -2198,12 +2196,19 @@
<xsd:attribute name="order" type="xsd:integer" use="optional">
<xsd:annotation>
<xsd:documentation>
[OPTIONAL] Specifies the order in which these interceptors will be
[OPTIONAL] Specifies the order in which this interceptor will be
added to the existing channel interceptors (if any).
Negative value (e.g., -2) will signify AFTER, but BEFORE the
the chain that might specify -1 (if any). Positive value (e.g., 2)
will signify BEFORE, but AFTER the chain that might specify 1 (if
any).
Negative value (e.g., -2) will signify BEFORE existing iinterceptors (if any). Positive value (e.g., 2)
will signify AFTER existing interceptors (if any)
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="ref" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
[OPTIONAL] points to a bean reference which implements this interceptor. Could also be defined as inner &lt;bean&gt; element.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>

View File

@@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel id="inputA">
<int:interceptors>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="eight"/>
</int:interceptors>
</int:channel>
<int:channel id="inputB"/>
<int:channel id="inputC"/>
<int:channel-interceptor-chain channel-name-pattern="*" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
</int:channel-interceptor-chain>
</beans>

View File

@@ -15,7 +15,7 @@
<int:channel id="inputB">
<int:queue capacity="1"/>
</int:channel>
</int:channel>
<int:publish-subscribe-channel id="foo"/>
@@ -28,27 +28,36 @@
<int:channel id="baz"/>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, fooA" order="1">
<int:channel-interceptor pattern="input*, foo" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="one"/>
<ref bean="channelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="two"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="three"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="-1">
</int:channel-interceptor>
<int:channel-interceptor pattern="input*, foo, object" order="1">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="four"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="-5">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="five"/>
<ref bean="channelInterceptor"/>
</int:channel-interceptor-chain>
</int:channel-interceptor>
<int:channel-interceptor pattern="inputA, foo">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="five"/>
</int:channel-interceptor>
<int:channel-interceptor pattern="inputA">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
</int:channel-interceptor>
<int:channel-interceptor>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="ten"/>
</int:channel-interceptor>
<int:channel-interceptor pattern="*" ref="eleven"/>
<bean id="eleven" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="eleven"/>
<int:channel-interceptor pattern="input*, foo" order="-3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="two"/>
</int:channel-interceptor>
<int:channel-interceptor pattern="input*" order="-4">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="three"/>
</int:channel-interceptor>
<bean id="channelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="seven"/>
<!-- <bean id="object" class="java.lang.Object"/>-->
</beans>

View File

@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
</int:channel-interceptor-chain>
<bean id="object" class="java.lang.Object"/>
</beans>

View File

@@ -1,40 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel id="inputA">
<int:interceptors>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleOrderedInterceptor"
p:testIdentifier="eight"
p:order="5"/>
<ref bean="unorderedChannelInterceptor"/>
<ref bean="orderedChannelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="five"/>
</int:interceptors>
</int:channel>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="3">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="six"/>
<ref bean="unorderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*" order="1">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor" p:testIdentifier="one"/>
<ref bean="orderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object" order="-1">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleOrderedInterceptor"
p:testIdentifier="four"
p:order="7"/>
<ref bean="orderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<bean id="unorderedChannelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="seven"/>
<bean id="orderedChannelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleOrderedInterceptor"
p:testIdentifier="ten"
p:order="4"/>
</beans>

View File

@@ -1,34 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:p="http://www.springframework.org/schema/p">
<int:channel id="inputA">
<int:interceptors>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="eight"/>
<ref bean="unorderedChannelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="five"/>
</int:interceptors>
</int:channel>
<int:channel-interceptor-chain channel-name-pattern="input*, foo, object">
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="six"/>
<ref bean="unorderedChannelInterceptor"/>
</int:channel-interceptor-chain>
<int:channel-interceptor-chain channel-name-pattern="input*">
<ref bean="unorderedChannelInterceptor"/>
<bean class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="one"/>
</int:channel-interceptor-chain>
<bean id="unorderedChannelInterceptor" class="org.springframework.integration.channel.interceptor.GlobalChannelInterceptorTests$SampleInterceptor"
p:testIdentifier="seven"/>
</beans>

View File

@@ -33,8 +33,9 @@ import org.springframework.integration.core.MessageChannel;
* @author Oleg Zhurakousky
* @since 2.0
*/
@SuppressWarnings("all")
public class GlobalChannelInterceptorTests {
@SuppressWarnings("unchecked")
@Test
public void validateGlobalInterceptor(){
ApplicationContext applicationContext =
@@ -46,149 +47,61 @@ public class GlobalChannelInterceptorTests {
Object iList = cAccessor.getPropertyValue("interceptors");
DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList);
List<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
if (channelName.equals("inputA")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 13);
Assert.assertEquals("four", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("seven", inter[3].getTestIdentifier());
Assert.assertEquals("eight", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
Assert.assertEquals("one", inter[6].getTestIdentifier());
Assert.assertEquals("seven", inter[7].getTestIdentifier());
Assert.assertEquals("two", inter[8].getTestIdentifier());
Assert.assertEquals("three", inter[9].getTestIdentifier());
Assert.assertEquals("seven", inter[10].getTestIdentifier());
Assert.assertEquals("six", inter[11].getTestIdentifier());
Assert.assertEquals("seven", inter[12].getTestIdentifier());
if (channelName.equals("inputA")){ // 328741
ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{});
Assert.assertTrue(inter.length ==10);
Assert.assertEquals("interceptor-three", inter[0].toString());
Assert.assertEquals("interceptor-two", inter[1].toString());
Assert.assertEquals("interceptor-eight", inter[2].toString());
Assert.assertEquals("interceptor-seven", inter[3].toString());
Assert.assertEquals("interceptor-five", inter[4].toString());
Assert.assertEquals("interceptor-six", inter[5].toString());
Assert.assertEquals("interceptor-ten", inter[6].toString());
Assert.assertEquals("interceptor-eleven", inter[7].toString());
Assert.assertEquals("interceptor-four", inter[8].toString());
Assert.assertEquals("interceptor-one", inter[9].toString());
}
else
if (channelName.equals("inputB")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 11);
Assert.assertEquals("four", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("seven", inter[3].getTestIdentifier());
Assert.assertEquals("one", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
Assert.assertEquals("two", inter[6].getTestIdentifier());
Assert.assertEquals("three", inter[7].getTestIdentifier());
Assert.assertEquals("seven", inter[8].getTestIdentifier());
Assert.assertEquals("six", inter[9].getTestIdentifier());
Assert.assertEquals("seven", inter[10].getTestIdentifier());
ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{});
Assert.assertTrue(inter.length == 6);
Assert.assertEquals("interceptor-three", inter[0].toString());
Assert.assertEquals("interceptor-two", inter[1].toString());
Assert.assertEquals("interceptor-ten", inter[2].toString());
Assert.assertEquals("interceptor-eleven", inter[3].toString());
Assert.assertEquals("interceptor-four", inter[4].toString());
Assert.assertEquals("interceptor-one", inter[5].toString());
}
else
if (channelName.equals("foo")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{});
Assert.assertTrue(inter.length == 6);
Assert.assertEquals("four", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("seven", inter[3].getTestIdentifier());
Assert.assertEquals("six", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
Assert.assertEquals("interceptor-two", inter[0].toString());
Assert.assertEquals("interceptor-five", inter[1].toString());
Assert.assertEquals("interceptor-ten", inter[2].toString());
Assert.assertEquals("interceptor-eleven", inter[3].toString());
Assert.assertEquals("interceptor-four", inter[4].toString());
Assert.assertEquals("interceptor-one", inter[5].toString());
}
else
if (channelName.equals("bar")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 2);
Assert.assertEquals("eight", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{});
Assert.assertTrue(inter.length == 4);
Assert.assertEquals("interceptor-eight", inter[0].toString());
Assert.assertEquals("interceptor-seven", inter[1].toString());
Assert.assertEquals("interceptor-ten", inter[2].toString());
Assert.assertEquals("interceptor-eleven", inter[3].toString());
}
else
if (channelName.equals("baz")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 0);
}
}
}
/**
* Will test mix of Ordered and un-Ordered ChannelInterceptors
* Individual interceptors will only be sorted within groups they are defined.
* For example: interceptors defined inside of channels will be sorted according to Ordered implementation
* If global interceptors were added BEFORE (negative order) or AFTER (ppositive order) the global stack will be sorted
* and added before/after the existing stack
*/
@SuppressWarnings("unchecked")
@Test
public void validateGlobalInterceptorsOrdered(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-ordered-context.xml", GlobalChannelInterceptorTests.class);
Map<String, AbstractMessageChannel> channels = applicationContext.getBeansOfType(AbstractMessageChannel.class);
for (String channelName : channels.keySet()) {
AbstractMessageChannel channel = channels.get(channelName);
DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel);
Object iList = cAccessor.getPropertyValue("interceptors");
DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList);
List<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
if (channelName.equals("inputA")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 10);
Assert.assertEquals("ten", inter[0].getTestIdentifier());
Assert.assertEquals("four", inter[1].getTestIdentifier());
Assert.assertEquals("ten", inter[2].getTestIdentifier());
Assert.assertEquals("eight", inter[3].getTestIdentifier());
Assert.assertEquals("seven", inter[4].getTestIdentifier());
Assert.assertEquals("five", inter[5].getTestIdentifier());
Assert.assertEquals("ten", inter[6].getTestIdentifier());
Assert.assertEquals("one", inter[7].getTestIdentifier());
Assert.assertEquals("six", inter[8].getTestIdentifier());
Assert.assertEquals("seven", inter[9].getTestIdentifier());
}
}
}
@SuppressWarnings("unchecked")
@Test
public void validateGlobalInterceptorsUnOrdered(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-unordered-context.xml", GlobalChannelInterceptorTests.class);
Map<String, AbstractMessageChannel> channels = applicationContext.getBeansOfType(AbstractMessageChannel.class);
for (String channelName : channels.keySet()) {
AbstractMessageChannel channel = channels.get(channelName);
DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel);
Object iList = cAccessor.getPropertyValue("interceptors");
DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList);
List<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
if (channelName.equals("inputA")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 7);
Assert.assertEquals("eight", inter[0].getTestIdentifier());
Assert.assertEquals("seven", inter[1].getTestIdentifier());
Assert.assertEquals("five", inter[2].getTestIdentifier());
Assert.assertEquals("six", inter[3].getTestIdentifier());
Assert.assertEquals("seven", inter[4].getTestIdentifier());
Assert.assertEquals("seven", inter[5].getTestIdentifier());
Assert.assertEquals("one", inter[6].getTestIdentifier());
}
}
}
@SuppressWarnings("unchecked")
@Test
public void validateGlobalInterceptorsAllPattern(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-all-context.xml", GlobalChannelInterceptorTests.class);
Map<String, AbstractMessageChannel> channels = applicationContext.getBeansOfType(AbstractMessageChannel.class);
for (String channelName : channels.keySet()) {
AbstractMessageChannel channel = channels.get(channelName);
DirectFieldAccessor cAccessor = new DirectFieldAccessor(channel);
Object iList = cAccessor.getPropertyValue("interceptors");
DirectFieldAccessor iAccessor = new DirectFieldAccessor(iList);
List<SampleInterceptor> interceptoList = (List<SampleInterceptor>) iAccessor.getPropertyValue("interceptors");
if (channelName.equals("inputA")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
ChannelInterceptor[] inter = interceptoList.toArray(new ChannelInterceptor[]{});
Assert.assertTrue(inter.length == 2);
} else if (channelName.equals("inputB")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 1);
} else if (channelName.equals("inputC")){
SampleInterceptor[] inter = interceptoList.toArray(new SampleInterceptor[]{});
Assert.assertTrue(inter.length == 1);
Assert.assertEquals("interceptor-ten", inter[0].toString());
Assert.assertEquals("interceptor-eleven", inter[1].toString());
}
}
}
public static class SampleInterceptor implements ChannelInterceptor {
private String testIdentifier;
@@ -210,6 +123,10 @@ public class GlobalChannelInterceptorTests {
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return null;
}
public String toString(){
return "interceptor-" + testIdentifier;
}
}
public static class SampleOrderedInterceptor extends SampleInterceptor implements Ordered {
private int order;