INT-1025 Added namespace support for both object-to-map-transformer and map-to-object-transformer

This commit is contained in:
Oleg Zhurakousky
2010-07-13 01:52:10 +00:00
parent 33d96b1fc1
commit 7ec2138ae8
10 changed files with 695 additions and 3 deletions

View File

@@ -41,6 +41,8 @@ public class IntegrationNamespaceHandler extends AbstractIntegrationNamespaceHan
registerBeanDefinitionParser("header-enricher", new StandardHeaderEnricherParser());
registerBeanDefinitionParser("header-filter", new HeaderFilterParser());
registerBeanDefinitionParser("object-to-string-transformer", new ObjectToStringTransformerParser());
registerBeanDefinitionParser("object-to-map-transformer", new ObjectToMapTransformerParser());
registerBeanDefinitionParser("map-to-object-transformer", new MapToObjectTransformerParser());
registerBeanDefinitionParser("payload-serializing-transformer", new PayloadSerializingTransformerParser());
registerBeanDefinitionParser("payload-deserializing-transformer", new PayloadDeserializingTransformerParser());
registerBeanDefinitionParser("claim-check-in", new ClaimCheckInParser());

View File

@@ -0,0 +1,54 @@
/*
* 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.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.transformer.MapToObjectTransformer;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public class MapToObjectTransformerParser extends AbstractTransformerParser {
@Override
protected String getTransformerClassName() {
return MapToObjectTransformer.class.getName();
}
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String ref = element.getAttribute("ref");
String type = element.getAttribute("type");
Assert.isTrue(!(StringUtils.hasText(ref) && StringUtils.hasText(type)),
"'type' and 'ref' attributes are mutually-exclusive, but both have valid values; type: " + type + "; ref:");
if (StringUtils.hasText(ref)){
builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(ref, "java.lang.String");
} else if (StringUtils.hasText(type)){
ClassLoader classLoader = parserContext.getReaderContext().getBeanClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
Class<?> clazz = ClassUtils.resolveClassName(type, classLoader);
builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(clazz, "java.lang.Class");
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.transformer.ObjectToMapTransformer;
import org.w3c.dom.Element;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public class ObjectToMapTransformerParser extends AbstractTransformerParser {
@Override
protected String getTransformerClassName() {
return ObjectToMapTransformer.class.getName();
}
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
}
}

View File

@@ -58,7 +58,6 @@ public class MapToObjectTransformer extends AbstractPayloadTransformer<Map<?,?>,
this.targetClass = targetClass;
this.targetBeanName = null;
}
/**
* @param beanName
*/
@@ -67,8 +66,6 @@ public class MapToObjectTransformer extends AbstractPayloadTransformer<Map<?,?>,
this.targetBeanName = beanName;
this.targetClass = null;
}
/*
* (non-Javadoc)
* @see org.springframework.integration.transformer.AbstractPayloadTransformer#transformPayload(java.lang.Object)

View File

@@ -1031,6 +1031,8 @@
<xsd:element ref="payload-serializing-transformer" />
<xsd:element ref="payload-deserializing-transformer" />
<xsd:element ref="object-to-string-transformer" />
<xsd:element ref="object-to-map-transformer"/>
<xsd:element ref="map-to-object-transformer" />
<xsd:element ref="filter" />
<xsd:element ref="splitter" />
<xsd:element ref="aggregator" />
@@ -1498,6 +1500,60 @@
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="object-to-map-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that converts any Object payload to a SpEL Map.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="inputOutputEndpointType">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="map-to-object-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that converts SpEL-based Map to an object.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="inputOutputEndpointType">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
<xsd:attribute name="type" type="xsd:string">
<xsd:annotation>
<xsd:documentation source="java:java.lang.Class"><![CDATA[
Fully qulified name of the java type to be created by this transformer (e.g. foo.bar.Foo)
NOTE: This attribute is mutually-exclusive with 'ref'.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="ref" use="optional">
<xsd:annotation>
<xsd:documentation>
The name of the bean to be produced by this transformer. The bean MUST BE of scope 'prototype'.
NOTE: This attribute is mutually-exclusive with 'type'.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="java.lang.Object" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="claim-check-in" type="claimCheckTransformerType">
<xsd:annotation>

View File

@@ -0,0 +1,20 @@
<?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:channel id="input"/>
<int:channel id="output">
<int:queue/>
</int:channel>
<int:map-to-object-transformer input-channel="input"
output-channel="output"
ref="person"/>
<bean id="person" class="org.springframework.integration.config.xml.MapToObjectTransformerParserTests$Person"/>
</beans>

View File

@@ -0,0 +1,35 @@
<?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:channel id="input"/>
<int:channel id="output">
<int:queue/>
</int:channel>
<int:map-to-object-transformer input-channel="input"
output-channel="output"
type="org.springframework.integration.config.xml.MapToObjectTransformerParserTests$Person"/>
<int:channel id="inputA"/>
<int:channel id="outputA">
<int:queue/>
</int:channel>
<int:map-to-object-transformer input-channel="inputA"
output-channel="outputA"
ref="person"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="org.springframework.integration.config.xml.MapToObjectTransformerParserTests$StringToAddressConverter" />
</list>
</property>
</bean>
<bean id="person" class="org.springframework.integration.config.xml.MapToObjectTransformerParserTests$Person" scope="prototype"/>
</beans>

View File

@@ -0,0 +1,184 @@
/*
* 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 junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.convert.converter.Converter;
import org.springframework.integration.channel.PollableChannel;
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
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class MapToObjectTransformerParserTests {
@Autowired
@Qualifier("input")
private MessageChannel input;
@Autowired
@Qualifier("output")
private PollableChannel output;
@Autowired
@Qualifier("inputA")
private MessageChannel inputA;
@Autowired
@Qualifier("outputA")
private PollableChannel outputA;
@SuppressWarnings("unchecked")
@Test
public void testMapToObjectTransformationWithType(){
Map map = new HashMap();
map.put("fname", "Justin");
map.put("lname", "Case");
Address address = new Address();
address.setStreet("1123 Main st");
map.put("address", address);
Message message = MessageBuilder.withPayload(map).build();
input.send(message);
Message outMessage = output.receive();
Person person = (Person) outMessage.getPayload();
assertNotNull(person);
assertEquals("Justin", person.getFname());
assertEquals("Case", person.getLname());
assertNull(person.getSsn());
assertNotNull(person.getAddress());
assertEquals("1123 Main st", person.getAddress().getStreet());
}
@SuppressWarnings("unchecked")
@Test
public void testMapToObjectTransformationWithRef(){
Map map = new HashMap();
map.put("fname", "Justin");
map.put("lname", "Case");
Address address = new Address();
address.setStreet("1123 Main st");
map.put("address", address);
Message message = MessageBuilder.withPayload(map).build();
inputA.send(message);
Message<?> newMessage = outputA.receive();
Person person = (Person) newMessage.getPayload();
assertNotNull(person);
assertEquals("Justin", person.getFname());
assertEquals("Case", person.getLname());
assertNull(person.getSsn());
assertNotNull(person.getAddress());
assertEquals("1123 Main st", person.getAddress().getStreet());
}
@SuppressWarnings("unchecked")
@Test
public void testMapToObjectTransformationWithConversionService(){
Map map = new HashMap();
map.put("fname", "Justin");
map.put("lname", "Case");
map.put("address", "1123 Main st");
Message message = MessageBuilder.withPayload(map).build();
inputA.send(message);
Message newMessage = outputA.receive();
Person person = (Person) newMessage.getPayload();
assertNotNull(person);
assertEquals("Justin", person.getFname());
assertEquals("Case", person.getLname());
assertNotNull(person.getAddress());
assertEquals("1123 Main st", person.getAddress().getStreet());
}
@Test(expected=BeanCreationException.class)
public void testNonPrototypeFailure(){
new ClassPathXmlApplicationContext("MapToObjectTransformerParserTests-context-fail.xml", MapToObjectTransformerParserTests.class);
}
public static class Person{
private String fname;
private String lname;
private String ssn;
private Address address;
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
public static class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
public static class StringToAddressConverter implements Converter<String, Address>{
public StringToAddressConverter(){}
public Address convert(String source) {
Address address = new Address();
address.setStreet(source);
return address;
}
}
}

View File

@@ -0,0 +1,18 @@
<?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.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.0.xsd">
<channel id="directInput"/>
<channel id="output">
<queue capacity="1"/>
</channel>
<object-to-map-transformer input-channel="directInput" output-channel="output"/>
</beans:beans>

View File

@@ -0,0 +1,289 @@
/*
* 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 junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.transformer.MessageTransformationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ObjectToMapTransformerParserTests {
@Autowired
@Qualifier("directInput")
private MessageChannel directInput;
@Autowired
@Qualifier("output")
private PollableChannel output;
@SuppressWarnings("unchecked")
@Test
public void testObjectToSpelMapTransformer(){
Employee employee = this.buildEmployee();
EvaluationContext context = new StandardEvaluationContext(employee);
ExpressionParser parser = new SpelExpressionParser();
Message<Employee> message = MessageBuilder.withPayload(employee).build();
directInput.send(message);
Message<Map<String, Object>> outputMessage = (Message<Map<String, Object>>) output.receive();
Map<String, Object> transformedMap = outputMessage.getPayload();
assertNotNull(outputMessage.getPayload());
for (String key : transformedMap.keySet()) {
Expression expression = parser.parseExpression(key);
Object valueFromTheMap = transformedMap.get(key);
Object valueFromExpression = expression.getValue(context);
String packageNameOfValueType = valueFromTheMap.getClass().getPackage().getName();
assertTrue(packageNameOfValueType.startsWith("java.lang"));
assertEquals(valueFromTheMap, valueFromExpression);
}
}
@Test(expected=MessageTransformationException.class)
public void testObjectToSpelMapTransformerWithCycle(){
Employee employee = this.buildEmployee();
Child child = new Child();
Person parent = employee.getPerson();
parent.setChild(child);
child.setParent(parent);
Message<Employee> message = MessageBuilder.withPayload(employee).build();
directInput.send(message);
}
@SuppressWarnings("unchecked")
public Employee buildEmployee(){
Address companyAddress = new Address();
companyAddress.setCity("Philadelphia");
companyAddress.setStreet("1123 Main");
companyAddress.setZip("12345");
Map<String, Long[]> coordinates = new HashMap<String, Long[]>();
coordinates.put("latitude", new Long[]{(long)1, (long)5, (long)13});
coordinates.put("longitude", new Long[]{(long)156});
companyAddress.setCoordinates(coordinates);
Employee employee = new Employee();
employee.setCompanyName("ABC Inc.");
employee.setCompanyAddress(companyAddress);
ArrayList departments = new ArrayList();
departments.add("HR");
departments.add("IT");
employee.setDepartments(departments);
Person person = new Person();
person.setFname("Justin");
person.setLname("Case");
person.setAkaNames("Hard", "Use", "Beer");
Address personAddress = new Address();
personAddress.setCity("Philly");
personAddress.setStreet("123 Main");
List<String> listTestData = new ArrayList<String>();
listTestData.add("hello");
listTestData.add("blah");
Map<String, List<String>> mapWithListTestData = new HashMap<String, List<String>>();
mapWithListTestData.put("mapWithListTestData", listTestData);
personAddress.setMapWithListData(mapWithListTestData);
person.setAddress(personAddress);
Map<String, Object> remarksA = new HashMap<String, Object>();
Map<String, Object> remarksB = new HashMap<String, Object>();
remarksA.put("foo", "foo");
remarksA.put("bar", "bar");
remarksB.put("baz", "baz");
List<Map<String, Object>> remarks = new ArrayList<Map<String,Object>>();
remarks.add(remarksA);
remarks.add(remarksB);
person.setRemarks(remarks);
employee.setPerson(person);
Map<String, Map<String, Object>> testMapData = new HashMap<String, Map<String, Object>>();
Map<String, Object> internalMapA = new HashMap<String, Object>();
internalMapA.put("foo", "foo");
internalMapA.put("bar", "bar");
Map<String, Object> internalMapB = new HashMap<String, Object>();
internalMapB.put("baz", "baz");
testMapData.put("internalMapA", internalMapA);
testMapData.put("internalMapB", internalMapB);
employee.setTestMapInMapData(testMapData);
return employee;
}
public static class Employee{
private List<String> departments;
private String companyName;
private Person person;
private Address companyAddress;
private Map<String, Map<String, Object>> testMapInMapData;
public Map<String, Map<String, Object>> getTestMapInMapData() {
return testMapInMapData;
}
public void setTestMapInMapData(
Map<String, Map<String, Object>> testMapInMapData) {
this.testMapInMapData = testMapInMapData;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Address getCompanyAddress() {
return companyAddress;
}
public void setCompanyAddress(Address companyAddress) {
this.companyAddress = companyAddress;
}
public List<String> getDepartments() {
return departments;
}
public void setDepartments(List<String> departments) {
this.departments = departments;
}
}
public static class Person{
private String fname;
private String lname;
private String[] akaNames;
private List<Map<String, Object>> remarks;
private Child child;
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
public List<Map<String, Object>> getRemarks() {
return remarks;
}
public void setRemarks(List<Map<String, Object>> remarks) {
this.remarks = remarks;
}
private Address address;
public String[] getAkaNames() {
return akaNames;
}
public void setAkaNames(String... akaNames) {
this.akaNames = akaNames;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
public static class Address{
private String street;
private String city;
private String zip;
private Map<String, List<String>> mapWithListData;
private Map<String, Long[]> coordinates;
public Map<String, List<String>> getMapWithListData() {
return mapWithListData;
}
public void setMapWithListData(Map<String, List<String>> mapWithListData) {
this.mapWithListData = mapWithListData;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Map<String, Long[]> getCoordinates() {
return coordinates;
}
public void setCoordinates(Map<String, Long[]> coordinates) {
this.coordinates = coordinates;
}
}
public static class Child {
private Person parent;
public Person getParent() {
return parent;
}
public void setParent(Person parent) {
this.parent = parent;
}
}
}