INT-789, Fixed the schema file, added one more test

This commit is contained in:
Oleg Zhurakousky
2010-03-27 01:32:02 +00:00
parent 7381ee28cf
commit 7a72fbc9e7
4 changed files with 65 additions and 15 deletions

View File

@@ -95,10 +95,14 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (channelPatternMatches(beanName)){
Assert.isTrue(bean instanceof AbstractMessageChannel, "channel interceptors can only be added to " +
"AbstractMessageChannel. Current implementation is: " + bean.getClass());
logger.debug("Applying global interceptors on channel '" + beanName + "'");
this.mergeInterceptorsToChannel((AbstractMessageChannel) bean, 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'");
}
}
return bean;
}
@@ -177,6 +181,9 @@ final class GlobalChannelInterceptorBeanPostProcessor implements BeanPostProcess
patterns = allAvailablePatters.toArray(new String[]{});
}
for (String channelPattern : patterns) {
if (channelPattern.trim().equals("*")){
return true;
}
Pattern p = Pattern.compile(channelPattern.trim());
Matcher m = p.matcher(beanName);
if (m.find()){

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration"
elementFormDefault="qualified" attributeFormDefault="unqualified">
@@ -1655,12 +1656,16 @@
</xsd:element>
<xsd:element name="channel-interceptor-chain">
<xsd:annotation>
<xsd:documentation>
Allows you to define channel interceptors to be applied globally
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="beans:ref" minOccurs="0" maxOccurs="unbounded"/>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="beans:bean"/>
<xsd:element ref="beans:ref"/>
<xsd:element ref="beans:bean"/>
</xsd:choice>
</xsd:sequence>
<xsd:attribute name="channel-name-pattern" type="xsd:string" use="required">

View File

@@ -0,0 +1,23 @@
<?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,6 @@
*/
package org.springframework.integration.channel.interceptor;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -167,13 +166,29 @@ public class GlobalChannelInterceptorTests {
}
}
}
/**
* Will test failure if 'channel-name-pattern' filter points to a valid
* bean which is not an AbstractMessageChannel
*/
@Test(expected=BeanCreationException.class)
public void failGlobalInterceptorConfig(){
new ClassPathXmlApplicationContext("GlobalChannelInterceptorTests-failed-context.xml", GlobalChannelInterceptorTests.class);
@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[]{});
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);
}
}
}
public static class SampleInterceptor implements ChannelInterceptor {