INT-1178, added <converter> namespace support

This commit is contained in:
Oleg Zhurakousky
2010-07-19 04:09:51 +00:00
parent 63a3eecc43
commit 1d9acb24ea
5 changed files with 336 additions and 1 deletions

View File

@@ -0,0 +1,67 @@
/*
* 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.config.xml;
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;
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.ParserContext;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public class ConverterParser extends AbstractBeanDefinitionParser {
private final ManagedList<Object> converters = new ManagedList<Object>();
private final static String CONVERSION_SERVICE = "integrationConversionService";
/* (non-Javadoc)
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#parseInternal(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext)
*/
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
if (!parserContext.getReaderContext().getRegistry().containsBeanDefinition(CONVERSION_SERVICE)){
this.defineConversionSesrvice(parserContext);
}
BeanComponentDefinition converterDefinition = IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
if (converterDefinition == null){
String beanName = element.getAttribute("ref");
Assert.isTrue(StringUtils.hasText(beanName), "'ref' attribute pointing to a Converter definition or sub-element <bean> converter definition must be provided");
converters.add(new RuntimeBeanReference(beanName));
} else{
converters.add(converterDefinition);
}
return null;
}
/*
*
*/
private void defineConversionSesrvice(ParserContext parserContext){
BeanDefinitionBuilder conversionServiceBuilder = BeanDefinitionBuilder.rootBeanDefinition(ConversionServiceFactoryBean.class);
conversionServiceBuilder.addPropertyValue("converters", converters);
BeanDefinitionHolder bdHolder = new BeanDefinitionHolder(conversionServiceBuilder.getBeanDefinition(), CONVERSION_SERVICE);
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, parserContext.getRegistry());
}
}

View File

@@ -61,6 +61,7 @@ public class IntegrationNamespaceHandler extends AbstractIntegrationNamespaceHan
registerBeanDefinitionParser("application-event-multicaster", new ApplicationEventMulticasterParser());
registerBeanDefinitionParser("publisher", new PublisherParser());
registerBeanDefinitionParser("channel-interceptor", new GlobalChannelInterceptorParser());
registerBeanDefinitionParser("converter", new ConverterParser());
}
}

View File

@@ -1024,7 +1024,7 @@
<xsd:extension base="inputOutputEndpointType">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:any processContents="strict" namespace="##other" minOccurs="0" maxOccurs="unbounded" />
<xsd:element ref="service-activator" />
<xsd:element ref="service-activator"/>
<xsd:element ref="header-enricher" />
<xsd:element ref="header-filter" />
<xsd:element ref="transformer" />
@@ -2326,5 +2326,27 @@
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="converter">
<xsd:complexType>
<xsd:all minOccurs="0" maxOccurs="1">
<xsd:element ref="beans:bean" />
</xsd:all>
<xsd:attribute name="ref" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="java.lang.Object"/>
</tool:annotation>
</xsd:appinfo>
<xsd:documentation>
<![CDATA[
[OPTIONAL] points to a bean reference which implements this Converter. Could also be defined as inner &lt;bean&gt; element.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
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-2.0.xsd">
<int:service-activator input-channel="serviceActivatorChannel" ref="testService" method="test"/>
<int:service-activator input-channel="serviceActivatorChannel3" ref="testService" method="test3"/>
<int:transformer input-channel="transformerChannel" ref="testService" method="test"/>
<int:router input-channel="routerChannel" ref="testService" method="test"/>
<int:splitter input-channel="splitterChannel" ref="testService" method="test"/>
<int:filter input-channel="filterChannel" ref="testService" method="filter"/>
<int:channel id="ROUTER_TARGET_CHANNEL">
<int:queue/>
</int:channel>
<bean id="testService" class="org.springframework.integration.config.xml.ConverterParserTests$TestService"/>
<int:converter ref="sampleConverter"/>
<bean id="sampleConverter" class="org.springframework.integration.config.xml.ConverterParserTests$TestConverter"/>
<int:converter>
<bean class="org.springframework.integration.config.xml.ConverterParserTests$TestConverter3"/>
</int:converter>
</beans>

View File

@@ -0,0 +1,212 @@
/*
* 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.config.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ConverterParserTests {
@Autowired
@Qualifier("serviceActivatorChannel")
private MessageChannel serviceActivatorChannel;
@Autowired
@Qualifier("serviceActivatorChannel3")
private MessageChannel serviceActivatorChannel3;
@Autowired
private MessageChannel transformerChannel;
@Autowired
private MessageChannel splitterChannel;
@Autowired
private MessageChannel filterChannel;
@Autowired
private MessageChannel routerChannel;
@Autowired
@Qualifier("ROUTER_TARGET_CHANNEL")
private PollableChannel routerTargetChannel;
@Test
public void serviceActivator() {
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload(new TestBean1("service-test"))
.setReplyChannel(replyChannel).build();
this.serviceActivatorChannel.send(message);
Message<?> result = replyChannel.receive(0);
assertNotNull(result);
assertNotNull(result.getPayload());
assertEquals(TestBean2.class, result.getPayload().getClass());
assertEquals("SERVICE-TEST", ((TestBean2) result.getPayload()).text);
}
@Test
public void serviceActivatorUsingInnerConverterDefinition() {
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload(new TestBean1("service-test"))
.setReplyChannel(replyChannel).build();
this.serviceActivatorChannel3.send(message);
Message<?> result = replyChannel.receive(0);
assertNotNull(result);
assertNotNull(result.getPayload());
assertEquals(TestBean3.class, result.getPayload().getClass());
assertEquals("SERVICE-TEST", ((TestBean3) result.getPayload()).text);
}
@Test
public void transformer() {
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload(new TestBean1("transformer-test"))
.setReplyChannel(replyChannel).build();
this.transformerChannel.send(message);
Message<?> result = replyChannel.receive(0);
assertNotNull(result);
assertNotNull(result.getPayload());
assertEquals(TestBean2.class, result.getPayload().getClass());
assertEquals("TRANSFORMER-TEST", ((TestBean2) result.getPayload()).text);
}
@Test
public void splitter() {
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload(new TestBean1("splitter-test"))
.setReplyChannel(replyChannel).build();
this.splitterChannel.send(message);
Message<?> result = replyChannel.receive(0);
assertNotNull(result);
assertNotNull(result.getPayload());
assertEquals(TestBean2.class, result.getPayload().getClass());
assertEquals("SPLITTER-TEST", ((TestBean2) result.getPayload()).text);
}
@Test
public void filter() {
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload(new TestBean1("filter-test"))
.setReplyChannel(replyChannel).build();
this.filterChannel.send(message);
Message<?> result = replyChannel.receive(0);
assertNotNull(result);
assertNotNull(result.getPayload());
assertEquals(TestBean1.class, result.getPayload().getClass());
assertEquals("filter-test", ((TestBean1) result.getPayload()).text);
}
@Test
public void router() {
Message<?> message = MessageBuilder.withPayload(new TestBean1("router-test")).build();
this.routerChannel.send(message);
Message<?> result = this.routerTargetChannel.receive(0);
assertNotNull(result);
assertNotNull(result.getPayload());
assertEquals(TestBean1.class, result.getPayload().getClass());
assertEquals("router-test", ((TestBean1) result.getPayload()).text);
}
@SuppressWarnings("unused")
private static class TestService {
public Object test(TestBean2 bean) {
return bean;
}
public Object test3(TestBean3 bean) {
return bean;
}
public boolean filter(TestBean2 bean) {
return true;
}
}
private static class TestBean1 {
private String text;
public TestBean1(String text) {
this.text = text;
}
}
private static class TestBean2 {
private String text;
public TestBean2(String text) {
this.text = text;
}
// called by router for channel name
public String toString() {
return this.text.replace("-TEST", "_TARGET_CHANNEL");
}
}
private static class TestBean3 {
private String text;
public TestBean3(String text) {
this.text = text;
}
// called by router for channel name
public String toString() {
return this.text.replace("-TEST", "_TARGET_CHANNEL");
}
}
@SuppressWarnings("unused")
private static class TestConverter implements Converter<TestBean1, TestBean2> {
public TestBean2 convert(TestBean1 source) {
return new TestBean2(source.text.toUpperCase());
}
}
@SuppressWarnings("unused")
private static class TestConverter3 implements Converter<TestBean1, TestBean3> {
public TestBean3 convert(TestBean1 source) {
return new TestBean3(source.text.toUpperCase());
}
}
}