INT-1486 refactored payload-serializing/deserializing-transformes for latest commons-serializer updates

This commit is contained in:
Mark Fisher
2010-09-28 14:52:37 -04:00
parent 28b482c25d
commit aba3b47060
11 changed files with 55 additions and 65 deletions

View File

@@ -35,7 +35,7 @@ public class PayloadDeserializingTransformerParser extends AbstractTransformerPa
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "deserializer");
}
}

View File

@@ -36,7 +36,7 @@ public class PayloadSerializingTransformerParser extends AbstractTransformerPars
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "serializer");
}
}

View File

@@ -16,9 +16,8 @@
package org.springframework.integration.transformer;
import org.springframework.commons.serializer.Deserializer;
import org.springframework.commons.serializer.DeserializingConverter;
import org.springframework.commons.serializer.java.JavaStreamingConverter;
import org.springframework.core.convert.converter.Converter;
/**
* Transformer that deserializes the inbound byte array payload to an object by delegating to a
@@ -33,15 +32,14 @@ import org.springframework.core.convert.converter.Converter;
*/
public class PayloadDeserializingTransformer extends PayloadTypeConvertingTransformer<byte[], Object> {
@Override
public void setConverter(Converter<byte[], Object> converter) {
this.converter = converter;
public void setDeserializer(Deserializer<Object> deserializer) {
this.setConverter(new DeserializingConverter(deserializer));
}
@Override
protected Object transformPayload(byte[] payload) throws Exception {
if (this.converter == null) {
this.converter = new DeserializingConverter(new JavaStreamingConverter());
this.setConverter(new DeserializingConverter());
}
return this.converter.convert(payload);
}

View File

@@ -16,9 +16,8 @@
package org.springframework.integration.transformer;
import org.springframework.commons.serializer.Serializer;
import org.springframework.commons.serializer.SerializingConverter;
import org.springframework.commons.serializer.java.JavaStreamingConverter;
import org.springframework.core.convert.converter.Converter;
/**
* Transformer that serializes the inbound payload into a byte array by delegating to a
@@ -33,15 +32,14 @@ import org.springframework.core.convert.converter.Converter;
*/
public class PayloadSerializingTransformer extends PayloadTypeConvertingTransformer<Object, byte[]> {
@Override
public void setConverter(Converter<Object, byte[]> converter) {
this.converter = converter;
public void setSerializer(Serializer<Object> serializer) {
this.setConverter(new SerializingConverter(serializer));
}
@Override
protected byte[] transformPayload(Object payload) throws Exception {
if (this.converter == null) {
this.converter = new SerializingConverter(new JavaStreamingConverter());
this.setConverter(new SerializingConverter());
}
return this.converter.convert(payload);
}

View File

@@ -25,20 +25,13 @@ import org.springframework.util.Assert;
*
* @author Gary Russell
* @since 2.0
*
*/
public class PayloadTypeConvertingTransformer<T, U> extends AbstractPayloadTransformer<T, U> {
protected Converter<T, U> converter;
@Override
protected U transformPayload(T payload) throws Exception {
Assert.notNull(this.converter, this.getClass().getName() + " needs a Converter<Object, Object>");
return converter.convert(payload);
}
/**
* Sets the converter to be used for Serialization.
* Specify the converter to use.
*
* @param converter The Converter.
*/
@@ -46,5 +39,10 @@ public class PayloadTypeConvertingTransformer<T, U> extends AbstractPayloadTrans
this.converter = converter;
}
@Override
protected U transformPayload(T payload) throws Exception {
Assert.notNull(this.converter, this.getClass().getName() + " requires a Converter<Object, Object>");
return this.converter.convert(payload);
}
}

View File

@@ -1648,15 +1648,15 @@
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
<xsd:attribute name="converter" use="optional">
<xsd:attribute name="serializer" use="optional">
<xsd:annotation>
<xsd:documentation>
Reference to a Converter instance that converts from an object to a byte array.
This is optional. The default Converter will use standard Java serialization.
Reference to a Serializer instance to convert from an object to a byte array.
This is optional. The default will use standard Java serialization.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.core.convert.converter.Converter" />
<tool:expected-type type="org.springframework.commons.serializer.Serializer" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
@@ -1682,15 +1682,15 @@
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
<xsd:attribute name="converter" use="optional">
<xsd:attribute name="deserializer" use="optional">
<xsd:annotation>
<xsd:documentation>
Reference to a Converter instance that converts from a byte array to an object.
This is optional. The default Converter will use standard Java deserialization.
Reference to a Deserializer instance to convert from a byte array to an object.
This is optional. The default will use standard Java deserialization.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.core.convert.converter.Converter" />
<tool:expected-type type="org.springframework.commons.serializer.Deserializer" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>

View File

@@ -13,7 +13,7 @@
<queue capacity="1"/>
</channel>
<channel id="customConverterInput"/>
<channel id="customDeserializerInput"/>
<channel id="output">
<queue capacity="1"/>
@@ -25,8 +25,8 @@
<poller fixed-delay="10000"/>
</payload-deserializing-transformer>
<payload-deserializing-transformer input-channel="customConverterInput" output-channel="output" converter="customConverter"/>
<payload-deserializing-transformer input-channel="customDeserializerInput" output-channel="output" deserializer="customDeserializer"/>
<beans:bean id="customConverter" class="org.springframework.integration.config.xml.PayloadDeserializingTransformerParserTests$TestDeserializingConverter"/>
<beans:bean id="customDeserializer" class="org.springframework.integration.config.xml.PayloadDeserializingTransformerParserTests$TestDeserializer"/>
</beans:beans>

View File

@@ -21,15 +21,17 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.commons.serializer.Deserializer;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
@@ -37,6 +39,7 @@ import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.transformer.MessageTransformationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.FileCopyUtils;
/**
* @author Mark Fisher
@@ -52,7 +55,7 @@ public class PayloadDeserializingTransformerParserTests {
private MessageChannel queueInput;
@Autowired
private MessageChannel customConverterInput;
private MessageChannel customDeserializerInput;
@Autowired
private PollableChannel output;
@@ -105,8 +108,8 @@ public class PayloadDeserializingTransformerParserTests {
}
@Test
public void customConverter() throws Exception {
customConverterInput.send(new GenericMessage<byte[]>("test".getBytes("UTF-8")));
public void customDeserializer() throws Exception {
customDeserializerInput.send(new GenericMessage<byte[]>("test".getBytes("UTF-8")));
Message<?> result = output.receive(3000);
assertNotNull(result);
assertEquals(String.class, result.getPayload().getClass());
@@ -130,15 +133,10 @@ public class PayloadDeserializingTransformerParserTests {
}
public static class TestDeserializingConverter implements Converter<byte[], Object> {
public static class TestDeserializer implements Deserializer<Object> {
public Object convert(byte[] source) {
try {
return new String(source, "UTF-8").toUpperCase();
}
catch (UnsupportedEncodingException e) {
throw new MessageTransformationException("failed to convert payload", e);
}
public Object deserialize(InputStream source) throws IOException {
return FileCopyUtils.copyToString(new InputStreamReader(source, "UTF-8")).toUpperCase();
}
}

View File

@@ -13,7 +13,7 @@
<queue capacity="1"/>
</channel>
<channel id="customConverterInput"/>
<channel id="customSerializerInput"/>
<channel id="output">
<queue capacity="1"/>
@@ -25,8 +25,8 @@
<poller fixed-delay="10000"/>
</payload-serializing-transformer>
<payload-serializing-transformer input-channel="customConverterInput" output-channel="output" converter="customConverter"/>
<payload-serializing-transformer input-channel="customSerializerInput" output-channel="output" serializer="customSerializer"/>
<beans:bean id="customConverter" class="org.springframework.integration.config.xml.PayloadSerializingTransformerParserTests$TestSerializingConverter"/>
<beans:bean id="customSerializer" class="org.springframework.integration.config.xml.PayloadSerializingTransformerParserTests$TestSerializer"/>
</beans:beans>

View File

@@ -21,15 +21,16 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.commons.serializer.Serializer;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
@@ -52,7 +53,7 @@ public class PayloadSerializingTransformerParserTests {
private MessageChannel queueInput;
@Autowired
private MessageChannel customConverterInput;
private MessageChannel customSerializerInput;
@Autowired
private PollableChannel output;
@@ -103,8 +104,8 @@ public class PayloadSerializingTransformerParserTests {
}
@Test
public void customConverter() throws Exception {
customConverterInput.send(new GenericMessage<String>("test"));
public void customSerializer() throws Exception {
customSerializerInput.send(new GenericMessage<String>("test"));
Message<?> result = output.receive(3000);
assertNotNull(result);
assertEquals(byte[].class, result.getPayload().getClass());
@@ -127,15 +128,12 @@ public class PayloadSerializingTransformerParserTests {
}
public static class TestSerializingConverter implements Converter<Object, byte[]> {
public static class TestSerializer implements Serializer<Object> {
public byte[] convert(Object source) {
try {
return source.toString().toUpperCase().getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new MessageTransformationException("failed to convert payload", e);
}
public void serialize(Object source, OutputStream outputStream) throws IOException {
outputStream.write(source.toString().toUpperCase().getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
}
}

View File

@@ -209,7 +209,7 @@
<dependency>
<groupId>org.springframework.commons</groupId>
<artifactId>spring-commons-serializer</artifactId>
<version>1.0.0.M1</version>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>