Added namespace support for <splitter/> element (INT-135).

This commit is contained in:
Mark Fisher
2008-05-20 16:23:50 +00:00
parent 5e5e5438be
commit 4c14e21cbe
8 changed files with 226 additions and 0 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.integration.channel.config.DirectChannelParser;
import org.springframework.integration.channel.config.PriorityChannelParser;
import org.springframework.integration.channel.config.QueueChannelParser;
import org.springframework.integration.channel.config.RendezvousChannelParser;
import org.springframework.integration.router.config.SplitterParser;
import org.springframework.util.ClassUtils;
/**
@@ -64,6 +65,7 @@ public class IntegrationNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("target-endpoint", new TargetEndpointParser());
registerBeanDefinitionParser("handler", new HandlerParser());
registerBeanDefinitionParser("handler-chain", new HandlerParser());
registerBeanDefinitionParser("splitter", new SplitterParser());
registerBeanDefinitionParser("aggregator", new AggregatorParser());
Map<String, Class<? extends BeanDefinitionParser>> parserMappings = this.loadAdapterParserMappings();
try {

View File

@@ -279,6 +279,20 @@
<xsd:attribute name="keep-alive" type="xsd:int"/>
</xsd:complexType>
<xsd:element name="splitter">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a Splitter.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:attribute name="ref" type="xsd:string" use="required"/>
<xsd:attribute name="method" type="xsd:string" use="required"/>
<xsd:attribute name="output-channel" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="aggregator">
<xsd:complexType>
<xsd:annotation>

View File

@@ -56,6 +56,12 @@ public class SplitterMessageHandlerAdapter extends AbstractMessageHandlerAdapter
}
}
public SplitterMessageHandlerAdapter(Object object, String methodName, String outputChannelName) {
Assert.hasText(outputChannelName, "output channel name is required");
this.setObject(object);
this.setMethodName(methodName);
this.outputChannelName = outputChannelName;
}
public void setChannelRegistry(ChannelRegistry channelRegistry) {
this.channelRegistry = channelRegistry;

View File

@@ -0,0 +1,53 @@
/*
* 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.router.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.router.SplitterMessageHandlerAdapter;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;splitter/&gt; element.
*
* @author Mark Fisher
*/
public class SplitterParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(Element element) {
return SplitterMessageHandlerAdapter.class;
}
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String ref = element.getAttribute("ref");
String methodName = element.getAttribute("method");
String outputChannelName = element.getAttribute("output-channel");
if (!StringUtils.hasText(ref) || !StringUtils.hasText(methodName) || !StringUtils.hasText(outputChannelName)) {
throw new ConfigurationException(
"The 'ref', 'method', and 'output-channel' attributes are all required.");
}
builder.addConstructorArgReference(ref);
builder.addConstructorArgValue(methodName);
builder.addConstructorArgValue(outputChannelName);
}
}

View File

@@ -164,6 +164,54 @@ public class SplitterMessageHandlerAdapterTests {
adapter.handle(message);
}
@Test
public void testSplitPayloadToStringArrayConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = new SplitterMessageHandlerAdapter(
testBean, "stringToStringArray", "testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitMessageToStringArrayConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = new SplitterMessageHandlerAdapter(
testBean, "messageToStringArray", "testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitStringToMessageListConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = new SplitterMessageHandlerAdapter(
testBean, "stringToMessageList", "testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testHeaderForObjectReturnValues() throws Exception {
StringMessage message = new StringMessage("foo.bar");

View File

@@ -0,0 +1,53 @@
/*
* 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.router.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class SplitterParserTests {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"splitterParserTests.xml", this.getClass());
context.start();
MessageChannel channel1 = (MessageChannel) context.getBean("channel1");
MessageChannel channel2 = (MessageChannel) context.getBean("channel2");
channel1.send(new StringMessage("this.is.a.test"));
Message<?> result1 = channel2.receive(1000);
assertEquals("this", result1.getPayload());
Message<?> result2 = channel2.receive(1000);
assertEquals("is", result2.getPayload());
Message<?> result3 = channel2.receive(1000);
assertEquals("a", result3.getPayload());
Message<?> result4 = channel2.receive(1000);
assertEquals("test", result4.getPayload());
assertNull(channel2.receive(0));
}
}

View File

@@ -0,0 +1,28 @@
/*
* 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.router.config;
/**
* @author Mark Fisher
*/
public class TestSplitter {
public String[] split(String input) {
return input.split("\\.");
}
}

View File

@@ -0,0 +1,22 @@
<?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">
<message-bus/>
<channel id="channel1"/>
<channel id="channel2"/>
<handler-endpoint handler="splitter" input-channel="channel1"/>
<splitter id="splitter" ref="pojo" method="split" output-channel="channel2"/>
<beans:bean id="pojo" class="org.springframework.integration.router.config.TestSplitter"/>
</beans:beans>