INT-1120 Generalize serialization strategy to enable serialization implementations to be usable from transformers and when streaming is required.

This commit is contained in:
Gary Russell
2010-07-26 19:12:20 +00:00
parent eb640e571a
commit 93623538e8
14 changed files with 598 additions and 40 deletions

View File

@@ -0,0 +1,45 @@
/*
* 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.commons.serializer;
/**
* @author Gary Russell
* @since 2.0
*
*/
@SuppressWarnings("serial")
public class DeserializationFailureException extends SerializationException {
/**
* Construct a <code>DeserializationException</code> with the specified detail message.
* @param msg the detail message
*/
public DeserializationFailureException(String msg) {
super(msg);
}
/**
* Construct a <code>DeserializationFailureException</code> with the specified detail message
* and nested exception.
* @param msg the detail message
* @param cause the nested exception
*/
public DeserializationFailureException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.commons.serializer;
import java.io.IOException;
import java.io.InputStream;
/**
* An standard interface to convert from data in an InputStream to
* an Object.
*
* @author Gary Russell
* @since 2.0
*
*/
public interface InputStreamingConverter<T> {
/**
* Read (assemble an object of type T) from an InputStream.
* @param is The InputStream.
* @return The object.
* @throws IOException
*/
public T convert(InputStream inputStream) throws IOException;
}

View File

@@ -0,0 +1,51 @@
/*
* 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.commons.serializer;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.springframework.core.convert.converter.Converter;
/**
* Delegates to a {@link JavaSerializationConverter} to deserialize data
* in a byte[] to an object.
*
* @author Gary Russell
* @since 2.0
*
*/
public class JavaDeserializingConverter implements Converter<byte[], Object> {
private JavaSerializationConverter converter = new JavaSerializationConverter();
public Object convert(byte[] source) {
ByteArrayInputStream byteStream = new ByteArrayInputStream(source);
try {
return converter.convert(byteStream);
}
catch (Exception e) {
try {
byteStream.close();
} catch (IOException e1) { }
throw new DeserializationFailureException(
"Failed to deserialize payload. Is the byte array a result of Object serialization?", e);
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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.commons.serializer;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/**
* Converter that implements both input and output streaming using Java
* Serialization.
*
* @author Gary Russell
* @since 2.0
*
*/
public class JavaSerializationConverter
implements InputStreamingConverter<Object>,
OutputStreamingConverter<Object> {
public Object convert(InputStream inputStream) throws IOException {
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(inputStream);
return objectInputStream.readObject();
} catch (ClassNotFoundException e) {
if (objectInputStream != null) {
objectInputStream.close();
}
throw new IOException(e);
}
}
public void convert(Object object, OutputStream outputStream)
throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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.commons.serializer;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
* Delegates to a {@link JavaSerializationConverter} to serialize an object
* to a byte[]. Source object must implement {@link Serializable}.
*
* @author Gary Russell
* @since 2.0
*
*/
public class JavaSerializingConverter implements Converter<Object, byte[]> {
private JavaSerializationConverter converter = new JavaSerializationConverter();
public byte[] convert(Object source) {
Assert.isTrue(source instanceof Serializable, this.getClass().getName()
+ " requires a Serializable payload, but received [" + source.getClass().getName() + "]");
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
try {
this.converter.convert(source, byteStream);
return byteStream.toByteArray();
} catch (Exception e) {
throw new SerializationFailureException("Failed to serialize", e);
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.commons.serializer;
import java.io.IOException;
import java.io.OutputStream;
/**
* A standard interface to stream an object to an OutputStream.
*
* @author Gary Russell
* @since 2.0
*
*/
public interface OutputStreamingConverter<T> {
/**
* Write an object of type T to the outputSream.
* @param object The object.
* @param outputStream The outputStream.
* @throws IOException
*/
public void convert(T object, OutputStream outputStream) throws IOException;
}

View File

@@ -0,0 +1,47 @@
/*
* 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.commons.serializer;
import org.springframework.core.NestedRuntimeException;
/**
* @author Gary Russell
* @since 2.0
*
*/
@SuppressWarnings("serial")
public abstract class SerializationException extends NestedRuntimeException {
/**
* Construct a <code>SerializationException</code> with the specified detail message.
* @param msg the detail message
*/
public SerializationException(String msg) {
super(msg);
}
/**
* Construct a <code>SerializationException</code> with the specified detail message
* and nested exception.
* @param msg the detail message
* @param cause the nested exception
*/
public SerializationException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.commons.serializer;
/**
* @author Gary Russell
* @since 2.0
*
*/
@SuppressWarnings("serial")
public class SerializationFailureException extends SerializationException {
/**
* Construct a <code>SerializationFailureException</code> with the specified detail message.
* @param msg the detail message
*/
public SerializationFailureException(String msg) {
super(msg);
}
/**
* Construct a <code>SerializationFailureException</code> with the specified detail message
* and nested exception.
* @param msg the detail message
* @param cause the nested exception
*/
public SerializationFailureException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -0,0 +1,8 @@
/**
*
* Root package for Spring commons' serializer interfaces and implementations.
* Provides an abstraction over various serialization techniques.
* Includes exceptions for serialization and deserialization failures.
*/
package org.springframework.commons.serializer;

View File

@@ -16,39 +16,35 @@
package org.springframework.integration.transformer;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import org.springframework.commons.serializer.JavaDeserializingConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
* Transformer that deserializes the inbound byte array payload to an object.
* <p>The byte array payload must be a result of serialization.
* Transformer that deserializes the inbound byte array payload to an object by delegating to a
* Converter&lt;byte[], Object&gt;. Default delegate is a {@link JavaDeserializingConverter}.
* <p>The byte array payload must be a result of equivalent serialization.
*
* @author Mark Fisher
* @author Gary Russell
* @since 1.0.1
*/
public class PayloadDeserializingTransformer extends AbstractPayloadTransformer<byte[], Object> {
public class PayloadDeserializingTransformer extends PayloadTypeConvertingTransformer<byte[], Object> {
public PayloadDeserializingTransformer() {
this.converter = new JavaDeserializingConverter();
}
@Override
protected Object transformPayload(byte[] payload) throws Exception {
ByteArrayInputStream byteStream = new ByteArrayInputStream(payload);
ObjectInputStream objectStream = null;
try {
objectStream = new ObjectInputStream(byteStream);
return objectStream.readObject();
}
catch (ObjectStreamException e) {
throw new IllegalArgumentException(
"Failed to deserialize payload. Is the byte array a result of Object serialization?", e);
}
finally {
try {
objectStream.close();
}
catch (Exception e) {
// ignore
}
}
Assert.notNull(this.converter, this.getClass().getName() + " needs a Converter<byte[], Object>");
return converter.convert(payload);
}
@Override
public void setConverter(Converter<byte[], Object> converter) {
this.converter = converter;
}
}

View File

@@ -16,31 +16,36 @@
package org.springframework.integration.transformer;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.springframework.commons.serializer.JavaSerializingConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
* Transformer that serializes the inbound payload into a byte array.
* <p>The payload instance must be Serializable.
* Transformer that serializes the inbound payload into a byte array by delegating to a
* Converter&lt;Object, byte[]&gt;. Default delegate is a {@link JavaSerializingConverter}.
*
* <p>The payload instance must be Serializable if the default converter is used.
*
* @author Mark Fisher
* @author Gary Russell
* @since 1.0.1
*/
public class PayloadSerializingTransformer extends AbstractPayloadTransformer<Object, byte[]> {
public class PayloadSerializingTransformer extends PayloadTypeConvertingTransformer<Object, byte[]> {
public PayloadSerializingTransformer() {
this.converter = new JavaSerializingConverter();
}
@Override
protected byte[] transformPayload(Object payload) throws Exception {
Assert.isTrue(payload instanceof Serializable, this.getClass().getName()
+ " requires a Serializable payload, but received [" + payload.getClass().getName() + "]");
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
objectStream.writeObject(payload);
objectStream.flush();
objectStream.close();
return byteStream.toByteArray();
Assert.notNull(this.converter, this.getClass().getName() + " needs a Converter<Object, byte[]>");
return converter.convert(payload);
}
@Override
public void setConverter(Converter<Object, byte[]> converter) {
this.converter = converter;
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.transformer;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
* Transformer that converts the inbound payload to an object by delegating to a
* Converter&lt;Object, Object&gt;. A reference to the delegate must be provided.
*
* @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.
*
* @param converter The Converter.
*/
public void setConverter(Converter<T, U> converter) {
this.converter = converter;
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.commons.serializer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.Serializable;
import org.junit.Test;
/**
* @author Gary Russell
* @since 2.0
*
*/
public class JavaSerializationTests {
@Test
public void testGood() {
JavaSerializingConverter toBytes = new JavaSerializingConverter();
byte[] bytes = toBytes.convert("Testing");
JavaDeserializingConverter fromBytes = new JavaDeserializingConverter();
assertEquals("Testing", fromBytes.convert(bytes));
}
@Test
public void testBadSerializeNotSerializable() {
JavaSerializingConverter toBytes = new JavaSerializingConverter();
try {
toBytes.convert(new Object());
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) { }
}
@Test
public void testBadSerializeNotSerializableField() {
JavaSerializingConverter toBytes = new JavaSerializingConverter();
try {
toBytes.convert(new UnSerializable());
fail("Expected SerializationFailureException");
} catch (SerializationFailureException e) { }
}
@Test
public void testBadDeserialize() {
JavaDeserializingConverter fromBytes = new JavaDeserializingConverter();
try {
fromBytes.convert("Junk".getBytes());
fail("Expected DeserializationFailureException");
} catch (DeserializationFailureException e) { }
}
class UnSerializable implements Serializable {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
private Object object;
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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.transformer;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.converter.Converter;
/**
* @author Gary Russell
* @since 2.0
*
*/
public class PayloadTypeConvertingTransformerTests {
/**
* Test method for {@link org.springframework.integration.transformer.PayloadTypeConvertingTransformer#transformPayload(java.lang.Object)}.
*/
@Test
public void testTransformPayloadObject() throws Exception {
PayloadTypeConvertingTransformer<String, String> tx = new PayloadTypeConvertingTransformer<String, String>();
tx.setConverter(new Converter<String, String> () {
public String convert(String source) {
return source.toUpperCase();
}}
);
String in = "abcd";
String out = tx.transformPayload(in);
assertEquals("ABCD", out);
}
}