Added namespace support for creating a MessageSelectorChain (INT-227).

This commit is contained in:
Mark Fisher
2008-06-24 21:11:30 +00:00
parent bacc093ca0
commit 00436b9071
6 changed files with 181 additions and 1 deletions

View File

@@ -70,6 +70,7 @@ public class IntegrationNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("gateway", new GatewayParser());
registerBeanDefinitionParser("handler", new HandlerParser());
registerBeanDefinitionParser("handler-chain", new HandlerParser());
registerBeanDefinitionParser("selector-chain", new SelectorChainParser());
registerBeanDefinitionParser("router", new RouterParser());
registerBeanDefinitionParser("splitter", new SplitterParser());
registerBeanDefinitionParser("aggregator", new AggregatorParser());

View File

@@ -0,0 +1,54 @@
/*
* 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.config;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.integration.message.selector.MessageSelectorChain;
/**
* Parser for the <selector-chain/> element.
*
* @author Mark Fisher
*/
public class SelectorChainParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(Element element) {
return MessageSelectorChain.class;
}
@SuppressWarnings("unchecked")
public void doParse(Element element, BeanDefinitionBuilder builder) {
ManagedList selectors = new ManagedList();
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE && "selector".equals(child.getLocalName())) {
selectors.add(new RuntimeBeanReference(((Element) child).getAttribute("ref")));
}
}
builder.addPropertyValue("selectors", selectors);
}
}

View File

@@ -255,7 +255,7 @@
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a handler.
Defines a MessageHandler.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:ID"/>
@@ -264,6 +264,31 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="selector-chain">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a MessageSelector chain.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="selector" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="selector">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Provides a MessageSelector reference.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="ref" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="dispatcher-policy">
<xsd:complexType>
<xsd:annotation>

View File

@@ -0,0 +1,50 @@
/*
* 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.config;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.message.selector.MessageSelectorChain;
/**
* @author Mark Fisher
*/
public class SelectorChainParserTests {
@Test
@SuppressWarnings("unchecked")
public void testSelectorChain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"selectorChainParserTests.xml", this.getClass());
MessageSelector selector1 = (MessageSelector) context.getBean("selector1");
MessageSelector selector2 = (MessageSelector) context.getBean("selector2");
MessageSelectorChain chain = (MessageSelectorChain) context.getBean("selectorChain");
DirectFieldAccessor accessor = new DirectFieldAccessor(chain);
List<MessageSelector> selectors = (List<MessageSelector>)
accessor.getPropertyValue("selectors");
assertEquals(selector1, selectors.get(0));
assertEquals(selector2, selectors.get(1));
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.config;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
/**
* @author Mark Fisher
*/
public class StubMessageSelector implements MessageSelector {
public boolean accept(Message<?> message) {
return true;
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<selector-chain id="selectorChain">
<selector ref="selector1"/>
<selector ref="selector2"/>
</selector-chain>
<beans:bean id="selector1" class="org.springframework.integration.config.StubMessageSelector"/>
<beans:bean id="selector2" class="org.springframework.integration.config.StubMessageSelector"/>
</beans:beans>