org.axonframework.serializer
Class AbstractXStreamSerializer

java.lang.Object
  extended by org.axonframework.serializer.AbstractXStreamSerializer
All Implemented Interfaces:
Serializer
Direct Known Subclasses:
DBObjectXStreamSerializer, DBObjectXStreamSerializer, XStreamSerializer

public abstract class AbstractXStreamSerializer
extends Object
implements Serializer

Abstract implementation for XStream based serializers. It provides some helper methods and configuration features independent of the actual format used to marshal to.

Since:
2.0
Author:
Allard Buijze

Constructor Summary
protected AbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream)
          Initialize the serializer using the given charset and xStream instance.
protected AbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver)
          Initialize the serializer using the given charset and xStream instance.
protected AbstractXStreamSerializer(Charset charset, com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver, ConverterFactory converterFactory)
          Initialize the serializer using the given charset, xStream instance, revisionResolver and converterFactory.
protected AbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream)
          Initialize a generic serializer using the UTF-8 character set.
protected AbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream, RevisionResolver revisionResolver)
          Initialize a generic serializer using the UTF-8 character set.
 
Method Summary
 void addAlias(String name, Class type)
          Adds an alias to use instead of the fully qualified class name.
 void addFieldAlias(String alias, Class definedIn, String fieldName)
          Adds an alias to use for a given field in the given class.
 void addPackageAlias(String alias, String pkgName)
          Add an alias for a package.
<T> boolean
canSerializeTo(Class<T> expectedRepresentation)
          Indicates whether this Serializer is capable of serializing to the given expectedRepresentation.
 Class classForType(SerializedType type)
          Returns the class for the given type identifier.
protected
<S,T> T
convert(Class<S> sourceType, Class<T> targetType, S source)
          Convert the given source, of type sourceType to the given targetType.
<S,T> T
deserialize(SerializedObject<S> serializedObject)
          Deserializes the first object read from the given bytes.
protected abstract  Object doDeserialize(SerializedObject serializedObject, com.thoughtworks.xstream.XStream xStream)
          Deserialize the given serializedObject.
protected abstract
<T> T
doSerialize(Object object, Class<T> expectedFormat, com.thoughtworks.xstream.XStream xStream)
          Serialize the given object to the given expectedFormat.
 Charset getCharset()
          Returns the character set used to convert character to bytes and vice versa.
 ConverterFactory getConverterFactory()
          Returns the ConverterFactory used by this serialized.
 com.thoughtworks.xstream.XStream getXStream()
          Returns a reference to the underlying XStream instance, that does the actual serialization.
protected abstract  void registerConverters(ChainingConverterFactory converterFactory)
          Registers any converters that are specific to the type of content written by this serializer.
<T> SerializedObject<T>
serialize(Object object, Class<T> expectedType)
          Serialize the given object into a Serialized Object containing the given expectedRepresentation.
 SerializedType typeForClass(Class type)
          Returns the type identifier for the given class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractXStreamSerializer

protected AbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream)
Initialize a generic serializer using the UTF-8 character set. The provided XStream instance is used to perform the serialization.

An AnnotationRevisionResolver is used to resolve the revision for serialized objects.

Parameters:
xStream - XStream instance to use

AbstractXStreamSerializer

protected AbstractXStreamSerializer(com.thoughtworks.xstream.XStream xStream,
                                    RevisionResolver revisionResolver)
Initialize a generic serializer using the UTF-8 character set. The provided XStream instance is used to perform the serialization.

Parameters:
xStream - XStream instance to use
revisionResolver - The strategy to use to resolve the revision of an object

AbstractXStreamSerializer

protected AbstractXStreamSerializer(Charset charset,
                                    com.thoughtworks.xstream.XStream xStream)
Initialize the serializer using the given charset and xStream instance. The xStream instance is configured with several converters for the most common types in Axon.

An AnnotationRevisionResolver is used to resolve revision for serialized objects.

Parameters:
charset - The character set to use
xStream - The XStream instance to use

AbstractXStreamSerializer

protected AbstractXStreamSerializer(Charset charset,
                                    com.thoughtworks.xstream.XStream xStream,
                                    RevisionResolver revisionResolver)
Initialize the serializer using the given charset and xStream instance. The xStream instance is configured with several converters for the most common types in Axon.

Parameters:
charset - The character set to use
xStream - The XStream instance to use
revisionResolver - The strategy to use to resolve the revision of an object

AbstractXStreamSerializer

protected AbstractXStreamSerializer(Charset charset,
                                    com.thoughtworks.xstream.XStream xStream,
                                    RevisionResolver revisionResolver,
                                    ConverterFactory converterFactory)
Initialize the serializer using the given charset, xStream instance, revisionResolver and converterFactory. The xStream instance is configured with several converters for the most common types in Axon.

Parameters:
charset - The character set to use
xStream - The XStream instance to use
revisionResolver - The strategy to use to resolve the revision of an object
converterFactory - The ConverterFactory providing the necessary content converters
Method Detail

registerConverters

protected abstract void registerConverters(ChainingConverterFactory converterFactory)
Registers any converters that are specific to the type of content written by this serializer.

Parameters:
converterFactory - the ConverterFactory to register the converters with

canSerializeTo

public <T> boolean canSerializeTo(Class<T> expectedRepresentation)
Description copied from interface: Serializer
Indicates whether this Serializer is capable of serializing to the given expectedRepresentation.

When true, this does *not* guarantee that the serialization and (optional) conversion will also succeed when executed. For example, when a serializer produces a byte[] containing JSON, trying to convert to a Dom4J Document will fail, even though this serializer has a converter to convert byte[] to Dom4J instances.

Specified by:
canSerializeTo in interface Serializer
Type Parameters:
T - The type of data a Serialized Object should contain
Parameters:
expectedRepresentation - The type of data a Serialized Object should contain
Returns:
true if the expectedRepresentation is supported, otherwise false.

serialize

public <T> SerializedObject<T> serialize(Object object,
                                         Class<T> expectedType)
Serialize the given object into a Serialized Object containing the given expectedRepresentation.

Use Serializer.canSerializeTo(Class) to detect whether the expectedRepresentation is supported by this serializer.

Specified by:
serialize in interface Serializer
Type Parameters:
T - The expected data type representing the serialized object
Parameters:
object - The object to serialize
expectedType - The expected data type representing the serialized object
Returns:
the instance representing the serialized object.

doSerialize

protected abstract <T> T doSerialize(Object object,
                                     Class<T> expectedFormat,
                                     com.thoughtworks.xstream.XStream xStream)
Serialize the given object to the given expectedFormat. The subclass may use convert(Class, Class, Object) to convert the result of the serialization to the expected type.

Type Parameters:
T - The format in which the serialized object must be returned
Parameters:
object - The object to serialize
expectedFormat - The format in which the serialized object must be returned
xStream - The XStream instance to serialize with
Returns:
The serialized object

doDeserialize

protected abstract Object doDeserialize(SerializedObject serializedObject,
                                        com.thoughtworks.xstream.XStream xStream)
Deserialize the given serializedObject.

Parameters:
serializedObject - The instance containing the serialized format of the object
xStream - The XStream instance to deserialize with
Returns:
the deserialized object

convert

protected <S,T> T convert(Class<S> sourceType,
                          Class<T> targetType,
                          S source)
Convert the given source, of type sourceType to the given targetType.

Type Parameters:
S - The type of data that needs to be converted
T - The target type of the conversion
Parameters:
sourceType - The type of data that needs to be converted. Should be a content type identifier, not necessarily the result of source.getClass().
targetType - The target type of the conversion
source - The object to convert
Returns:
The converted object

deserialize

public <S,T> T deserialize(SerializedObject<S> serializedObject)
Deserializes the first object read from the given bytes. The bytes are not consumed from the array or modified in any way. The resulting object instance is cast to the expected type.

Specified by:
deserialize in interface Serializer
Type Parameters:
S - The data type of the serialized object
T - The expected deserialized type
Parameters:
serializedObject - the instance describing the type of object and the bytes providing the serialized data
Returns:
the serialized object, cast to the expected type

classForType

public Class classForType(SerializedType type)
Returns the class for the given type identifier. The result of this method must guarantee that the deserialized SerializedObject with the given type is an instance of the returned Class.

If a class cannot be resolved (i.e. because the class is not available on this JVM's classpath) this method throws UnknownSerializedTypeException.

Specified by:
classForType in interface Serializer
Parameters:
type - The type identifier of the object
Returns:
the Class representing the type of the serialized Object

typeForClass

public SerializedType typeForClass(Class type)
Description copied from interface: Serializer
Returns the type identifier for the given class. This is the type identifier of the Serialized object as returned by Serializer.serialize(Object, Class).

Specified by:
typeForClass in interface Serializer
Parameters:
type - Class representing the type of the serializable Object.
Returns:
The type identifier of the object

addAlias

public void addAlias(String name,
                     Class type)
Adds an alias to use instead of the fully qualified class name.

Parameters:
name - The alias to use
type - The Class to use the alias for
See Also:
XStream.alias(String, Class)

addPackageAlias

public void addPackageAlias(String alias,
                            String pkgName)
Add an alias for a package. This allows long package names to be shortened considerably. Will also use the alias for sub-packages of the provided package.

E.g. an alias of "axoncore" for the package "org.axonframework.core" will use "axoncore.repository" for the package "org.axonframework.core.repository".

Parameters:
alias - The alias to use.
pkgName - The package to use the alias for
See Also:
XStream.aliasPackage(String, String)

addFieldAlias

public void addFieldAlias(String alias,
                          Class definedIn,
                          String fieldName)
Adds an alias to use for a given field in the given class.

Parameters:
alias - The alias to use instead of the original field name
definedIn - The class that defines the field.
fieldName - The name of the field to use the alias for
See Also:
XStream.aliasField(String, Class, String)

getXStream

public com.thoughtworks.xstream.XStream getXStream()
Returns a reference to the underlying XStream instance, that does the actual serialization.

Returns:
the XStream instance that does the actual (de)serialization.
See Also:
XStream

getCharset

public Charset getCharset()
Returns the character set used to convert character to bytes and vice versa.

Returns:
the character set used to convert character to bytes and vice versa

getConverterFactory

public ConverterFactory getConverterFactory()
Returns the ConverterFactory used by this serialized. The converter factory allows registration of ContentTypeConverters needed by the upcasters.

Specified by:
getConverterFactory in interface Serializer
Returns:
the ConverterFactory used by this serialized


Copyright © 2010-2016. All Rights Reserved.