Class MultiplexingSerializer
- java.lang.Object
-
- com.tangosol.io.MultiplexingSerializer
-
- All Implemented Interfaces:
ClassLoaderAware,Serializer
@Named("multi") public class MultiplexingSerializer extends Object implements Serializer, ClassLoaderAwareASerializerimplementation that multiplexes serialization/deserialization requests across multipleSerializerimplementations.More specifically, for serialization operations, the list of available
Serializers are iterated over. The first serializer that is able to serialize the object without error, will be theSerializerused 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 specifiedSerializerwill be used to deserialize the payload. If the header is no present, anIOExceptionwill be thrown.- Since:
- 20.06
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classMultiplexingSerializer.SerializationHeaderSimple wrapper class to hold decorated info added by the serialization process.
-
Field Summary
Fields Modifier and Type Field Description protected Map<String,Serializer>f_idToSerializerThe mapping ofSerializers keyed by their name.protected ConcurrentMap<String,Serializer>f_mapTypeToSerializerThe mapping of types referenced by their String name and a workingSerializerfor that type.protected Serializer[]f_serializersprotected WeakReference<ClassLoader>m_refLoaderThe optionalClassLoader.protected static StringNULL_TYPESymbolic reference fornull.
-
Constructor Summary
Constructors Constructor Description MultiplexingSerializer(Serializer... serializers)Construct a newSerializerthat will multiplex serialization operations across the provided list ofSerializers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Tdeserialize(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 voiddoSerialization(WriteBuffer.BufferOutput out, Object o, Serializer[] serializers)protected voiddoWrite(WriteBuffer.BufferOutput out, Object o, Serializer serializer)Writes the serialization header and payload.ClassLoadergetContextClassLoader()Retrieve the context ClassLoader for this object.StringgetName()Return the name of this serializer.protected voidlogSerializationErrors(Map<Serializer,Exception> serializerExceptionMap, String typeName)Given the provided mappings between aSerializerand theExceptionit threw, log each mapping for diagnostic purposes.protected MultiplexingSerializer.SerializationHeaderreadSerializationHeader(ReadBuffer.BufferInput in)voidserialize(WriteBuffer.BufferOutput out, Object o)Serialize an object to a WriteBuffer by writing its state using the specified BufferOutput object.voidsetContextClassLoader(ClassLoader loader)Specify the context ClassLoader for this object.StringtoString()protected intwriteSerializationHeader(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 workingSerializerfor that type.
-
f_idToSerializer
protected final Map<String,Serializer> f_idToSerializer
The mapping ofSerializers 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 newSerializerthat will multiplex serialization operations across the provided list ofSerializers. 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 noSerializers are provided, or if the any of the providedSerializers returnnullfromSerializer.getName()
-
-
Method Detail
-
serialize
public void serialize(WriteBuffer.BufferOutput out, Object o) throws IOException
Description copied from interface:SerializerSerialize 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:
serializein 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:SerializerDeserialize 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:
deserializein 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:SerializerReturn the name of this serializer.- Specified by:
getNamein interfaceSerializer- Returns:
- the name of this serializer
-
getContextClassLoader
public ClassLoader getContextClassLoader()
Description copied from interface:ClassLoaderAwareRetrieve 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:
getContextClassLoaderin interfaceClassLoaderAware- Returns:
- the context ClassLoader for this object
- See Also:
Thread.getContextClassLoader()
-
setContextClassLoader
public void setContextClassLoader(ClassLoader loader)
Description copied from interface:ClassLoaderAwareSpecify 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:
setContextClassLoaderin 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.BufferOutputto write too- the object to serializeserializers- theSerializers 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.BufferOutputto write too- the object to serializeserializer- theSerializerto use- Throws:
IOException- if an error occurs during serialization
-
logSerializationErrors
protected void logSerializationErrors(Map<Serializer,Exception> serializerExceptionMap, String typeName)
Given the provided mappings between aSerializerand theExceptionit threw, log each mapping for diagnostic purposes.- Parameters:
serializerExceptionMap- a mapping between aSerializerinstance and theExceptionit 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
Serializeris found, otherwise returnsnull
-
-