Class MultiplexingSerializer
- java.lang.Object
-
- com.tangosol.io.MultiplexingSerializer
-
- All Implemented Interfaces:
ClassLoaderAware
,Serializer
@Named("multi") public class MultiplexingSerializer extends Object implements Serializer, ClassLoaderAware
ASerializer
implementation that multiplexes serialization/deserialization requests across multipleSerializer
implementations.More specifically, for serialization operations, the list of available
Serializer
s are iterated over. The first serializer that is able to serialize the object without error, will be theSerializer
used for that type going forward. All serialized payloads will be prefixed with the following header to enable deserialization.+--------------+-------------------------+ | Length (4) | Serializer Name (1...) | +--------------+-------------------------+ | Serialized Payload (0...) | +----------------------------------------+
Upon deserialization, the header will be read and the specifiedSerializer
will be used to deserialize the payload. If the header is no present, anIOException
will be thrown.- Since:
- 20.06
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
MultiplexingSerializer.SerializationHeader
Simple wrapper class to hold decorated info added by the serialization process.
-
Field Summary
Fields Modifier and Type Field Description protected Map<String,Serializer>
f_idToSerializer
The mapping ofSerializer
s keyed by their name.protected ConcurrentMap<String,Serializer>
f_mapTypeToSerializer
The mapping of types referenced by their String name and a workingSerializer
for that type.protected Serializer[]
f_serializers
protected WeakReference<ClassLoader>
m_refLoader
The optionalClassLoader
.protected static String
NULL_TYPE
Symbolic reference fornull
.
-
Constructor Summary
Constructors Constructor Description MultiplexingSerializer(Serializer... serializers)
Construct a newSerializer
that will multiplex serialization operations across the provided list ofSerializer
s.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
deserialize(ReadBuffer.BufferInput in, Class<? extends T> clazz)
Deserialize an object as an instance of the specified class by reading its state using the specified BufferInput object.protected void
doSerialization(WriteBuffer.BufferOutput out, Object o, Serializer[] serializers)
protected void
doWrite(WriteBuffer.BufferOutput out, Object o, Serializer serializer)
Writes the serialization header and payload.ClassLoader
getContextClassLoader()
Retrieve the context ClassLoader for this object.String
getName()
Return the name of this serializer.protected void
logSerializationErrors(Map<Serializer,Exception> serializerExceptionMap, String typeName)
Given the provided mappings between aSerializer
and theException
it threw, log each mapping for diagnostic purposes.protected MultiplexingSerializer.SerializationHeader
readSerializationHeader(ReadBuffer.BufferInput in)
void
serialize(WriteBuffer.BufferOutput out, Object o)
Serialize an object to a WriteBuffer by writing its state using the specified BufferOutput object.void
setContextClassLoader(ClassLoader loader)
Specify the context ClassLoader for this object.String
toString()
protected int
writeSerializationHeader(WriteBuffer.BufferOutput out, String serializerName)
Write the multiplex serialization header.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.tangosol.io.Serializer
deserialize
-
-
-
-
Field Detail
-
m_refLoader
protected WeakReference<ClassLoader> m_refLoader
The optionalClassLoader
.
-
f_serializers
protected final Serializer[] f_serializers
-
f_mapTypeToSerializer
protected final ConcurrentMap<String,Serializer> f_mapTypeToSerializer
The mapping of types referenced by their String name and a workingSerializer
for that type.
-
f_idToSerializer
protected final Map<String,Serializer> f_idToSerializer
The mapping ofSerializer
s keyed by their name.
-
NULL_TYPE
protected static final String NULL_TYPE
Symbolic reference fornull
.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MultiplexingSerializer
public MultiplexingSerializer(Serializer... serializers)
Construct a newSerializer
that will multiplex serialization operations across the provided list ofSerializer
s. It's important to keep in mind that the iteration order when performing serialization/deserialization operation is based on the order they are provided. This may have bearing on ordering decisions.- Throws:
IllegalArgumentException
- if noSerializer
s are provided, or if the any of the providedSerializer
s returnnull
fromSerializer.getName()
-
-
Method Detail
-
serialize
public void serialize(WriteBuffer.BufferOutput out, Object o) throws IOException
Description copied from interface:Serializer
Serialize an object to a WriteBuffer by writing its state using the specified BufferOutput object.Note: Starting with Coherence 12.2.1 classes that need to designate an alternative object to be used by the Serializer when writing the object to the buffer should implement the
SerializationSupport.writeReplace()
method.- Specified by:
serialize
in interfaceSerializer
- Parameters:
out
- the BufferOutput with which to write the object's stateo
- the object to serialize- Throws:
IOException
- if an I/O error occurs
-
deserialize
public <T> T deserialize(ReadBuffer.BufferInput in, Class<? extends T> clazz) throws IOException
Description copied from interface:Serializer
Deserialize an object as an instance of the specified class by reading its state using the specified BufferInput object.Note: Starting with Coherence 12.2.1 classes that need to designate an alternative object to be returned by the Serializer after an object is deserialized from the buffer should implement the
SerializationSupport.readResolve()
method.- Specified by:
deserialize
in interfaceSerializer
- Type Parameters:
T
- the class to deserialize to- Parameters:
in
- the BufferInput with which to read the object's stateclazz
- the type of the object to deserialize- Returns:
- the deserialized user type instance
- Throws:
IOException
- if an I/O error occurs
-
getName
public String getName()
Description copied from interface:Serializer
Return the name of this serializer.- Specified by:
getName
in interfaceSerializer
- Returns:
- the name of this serializer
-
getContextClassLoader
public ClassLoader getContextClassLoader()
Description copied from interface:ClassLoaderAware
Retrieve the context ClassLoader for this object. The context ClassLoader is provided by the creator of the object for use by the object when loading classes and resources.- Specified by:
getContextClassLoader
in interfaceClassLoaderAware
- Returns:
- the context ClassLoader for this object
- See Also:
Thread.getContextClassLoader()
-
setContextClassLoader
public void setContextClassLoader(ClassLoader loader)
Description copied from interface:ClassLoaderAware
Specify the context ClassLoader for this object. The context ClassLoader can be set when the object is created, and allows the creator to provide the appropriate class loader to be used by the object when when loading classes and resources.- Specified by:
setContextClassLoader
in interfaceClassLoaderAware
- Parameters:
loader
- the context ClassLoader for this object
-
doSerialization
protected void doSerialization(WriteBuffer.BufferOutput out, Object o, Serializer[] serializers) throws IOException
- Parameters:
out
- theWriteBuffer.BufferOutput
to write too
- the object to serializeserializers
- theSerializer
s to attempt the serialization operation against- Throws:
IOException
- if an error occurs during serialization
-
doWrite
protected void doWrite(WriteBuffer.BufferOutput out, Object o, Serializer serializer) throws IOException
Writes the serialization header and payload.- Parameters:
out
- theWriteBuffer.BufferOutput
to write too
- the object to serializeserializer
- theSerializer
to use- Throws:
IOException
- if an error occurs during serialization
-
logSerializationErrors
protected void logSerializationErrors(Map<Serializer,Exception> serializerExceptionMap, String typeName)
Given the provided mappings between aSerializer
and theException
it threw, log each mapping for diagnostic purposes.- Parameters:
serializerExceptionMap
- a mapping between aSerializer
instance and theException
it threw for any given serialization operationtypeName
- The name of the type that produced the error.
-
writeSerializationHeader
protected int writeSerializationHeader(WriteBuffer.BufferOutput out, String serializerName) throws IOException
Write the multiplex serialization header.+--------------+-------------------------+ | Length (4) | Serializer Name (1...) | +--------------+-------------------------+ | Serialized Payload (0...) | +----------------------------------------+
- Parameters:
out
- the stream to write toserializerName
- the name of theSerializer
- Throws:
IOException
- if an error occurs writing the header or payload- See Also:
Serializer.getName()
-
readSerializationHeader
protected MultiplexingSerializer.SerializationHeader readSerializationHeader(ReadBuffer.BufferInput in)
Read theMultiplexingSerializer.SerializationHeader
.+--------------+-------------------------+ | Length (4) | Serializer Name (1...) | +--------------+-------------------------+ | Serialized Payload (0...) | +----------------------------------------+
- Parameters:
in
- the stream to read from- Returns:
- the SerializationHeader if present and a matching
Serializer
is found, otherwise returnsnull
-
-