Class ConfigurablePofContext
- All Implemented Interfaces:
ClassLoaderAware
,PofContext
,Serializer
,XmlConfigurable
- Direct Known Subclasses:
SafeConfigurablePofContext
PofContext
interface using information
provided in a configuration file (or in a passed XML configuration) as well as
classes annotated with PortableType
.
For each user type supported by this POF context, it must be provided with:
- A valid user type ID that is unique within this POF context;
- A Java class name that identifies a Java class or interface that all values of the user type are type-assignable to (and that no values of other user types are type-assignable to); in other words, all values of the user type (and no values of other user types) are instances of the specified class, instances of a sub-class of the specified class, or (if it is an interface) instances of a class that implements the specified interface;
- A Java class name that identifies a non-abstract implementation of the PofSerializer interface.
The format of the configuration XML is as follows:
<pof-config>
<user-type-list>
..
<user-type>
<type-id>53</type-id>
<class-name>com.mycompany.data.Trade</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PortableObjectSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
<user-type>
<type-id>54</type-id>
<class-name>com.mycompany.data.Position</class-name>
</user-type>
..
<include>file:/my-pof-config.xml</include>
..
</user-type-list>
<allow-interfaces>false</allow-interfaces>
<allow-subclasses>false</allow-subclasses>
</pof-config>
For each user type, a user-type element must exist inside the
user-type-list element. The user-type-list element
contains up to three elements, in the following order:
- The user-type element should contain a type-id element whose value specifies the unique integer type ID; if none of the user-type elements contains a type-id element, then the type IDs for the user types will be based on the order in which they appear in the configuration, with the first user type being assigned the type ID 0, the second user type being assigned the type ID 1, and so on. (It is strongly recommended that user types IDs always be specified, in order to support schema versioning and evolution.)
- The class-name element is required, and specifies the fully qualified name of the Java class or interface that all values of the user type are type-assignable to.
- The serializer element is used to specify an implementation of
PofSerializer to use to serialize and deserialize user type values to
and from a POF stream. Within the serializer element, the
class-name element is required, and zero or more constructor
parameters can be defined within an init-params block. If no
serializer is specified, either implement the PortableObject
interface or have a
Portable
annotation. If the former, aPortableObjectSerializer
will be used. If the later, aPofAnnotationSerializer
will be used.
The optional include element allows user-type elements defined in another configuration XML to be added to the user type list. The value of this element is a locator string (either a valid path or URL) that specifies the location of the target PofContext configuration file. The user-type elements of the target file are imported verbatum; therefore, if the included elements contain explicit type identifiers, each identifier must be unique with respect to the the user type identifiers (either explicit or generated) defined within the including file. If the included user types do not contain explicit type identifiers, then the type identifiers will be based on the order in which the user types appear in the composite configuration file. Multiple include elements may be used within a single user-type-list element.
The ConfigurablePofContext is truly ClassLoader-aware. It is conceivable that the ConfigurablePofContext is loaded by the system ClassLoader (or some other relatively global ClassLoader), while the objects deserialized by the PofContext are loaded by an application-specific ClassLoader, such as is typical within an application server. The ConfigurablePofContext is designed to load the configuration, the POF-able object classes and the PofSerializer classes from within a specified ClassLoader context, and to pass the ClassLoader information on to the PofSerializer instances, just in case they are not loaded from within the application's ClassLoader context. In other words, the ConfigurablePofContext, its configuration, the PofSerializer classes and the POF-able classes can all be loaded by the same ClassLoader, or they can all be loaded by different ClassLoaders, so long as the configuration, the POF-able classes and the PofSerializer classes can be loaded by either the specified ClassLoader or by the ClassLoader that loaded the ConfigurablePofContext itself.
In order to be used by the ConfigurablePofContext, a PofSerializer implementation must provide a public constructor that accepts the parameters detailed by the init-params element. The parameter values, as specified by the param-value element, can specify one of the following substitutable values:
- {type-id} - replaced with the Type ID of the User Type;
- {class-name} - replaced with the name of the class for the User Type;
- {class} - replaced with the Class for the User Type;
- {class-loader} - replaced with the ConfigurablePofContext's ContextClassLoader.
- (int nTypeId, Class clz, ClassLoader loader)
- (int nTypeId, Class clz)
- (int nTypeId)
- ()
Once constructed, if the PofSerializer implements the XmlConfigurable
interface, the setConfig
method is
invoked, and it is passed the parameter XML information, transposed as
described by transformInitParams
, and
as described in the coherence-pof-config.xsd file.
Finally, if the PofSerializer implements the ClassLoaderAware interface and
a ClassLoader has been specified, then the
setContextClassLoader
method
is invoked with the reference to the specified ClassLoader.
Conceptually, the identity of a ConfigurablePofContext is a combination of a configuration locator and a ClassLoader. The ClassLoader is used to resolve and load the configuration details whose location is specified by the configuration locator, and to load all of the classes specified by the configuration. To achieve acceptable performance, and to limit the redundant use of resources, the ConfigurablePofContext maintains a WeakHashMap keyed by ClassLoader, whose corresponding values are each a SafeHashMap keyed by configuration locator, whose corresponding values contain the data necessary to efficiently perform the operations prescribed by the PofContext interface.
Note: The configuration for the default
constructor
can be specified using the
tangosol.pof.config
system property.
- Since:
- Coherence 3.2
- Author:
- jh/cp 2006.07.24
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
The information related to the configuration of a particular PofContext for a specific URI and ClassLoader. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final String
Default Jandex index file.static final String
The name of the application resource that contains the default set of wire-format-to-object bindings.static final String
The name of the system property ("tangosol.pof.config") that can be used to override the location of the default POF configuration file.protected static final Class
Marker serving as the implicit root class for all lambdas. -
Constructor Summary
ConstructorDescriptionDefault constructor.Copy constructor for a ConfigurablePofContext.Create a ConfigurablePofContext that will use the passed configuration information.ConfigurablePofContext
(String sLocator) Create a ConfigurablePofContext that will load configuration information from the specified locator. -
Method Summary
Modifier and TypeMethodDescriptionprotected static void
appendDefaultSerializerToUserTypes
(XmlElement xmlConfig) Process <default-serializer> element from the specified xml configuration and append information about the serializer to each <user-type> element within <user-type-list> unless user type already has a serializer specified.protected void
Verify that the ConfigurablePofContext has not already been initialized.protected ConfigurablePofContext.PofConfig
Create a PofConfig object based on a configuration that was either provided as XML, or can be loaded from the specified (or default) URI using the provided ClassLoader.Deserialize an object from a ReadBuffer by reading its state using the specified BufferInput object.protected void
Fully initialize the ConfigurablePofContext if it has not already been initialized.getClass
(int nTypeId) Determine the class associated with the given user type identifier.getClassName
(int nTypeId) Determine the name of the class associated with the given user type identifier.Determine the current configuration of the object.protected String
Obtain the location of the configuration that the ConfigurablePofContext used to configure itself.Retrieve the context ClassLoader for this object.Return the Jandex index file name to use to discover portable types.protected int
Helper method for determining the user type identifier associated with a given class that does not have a direct configured association.getName()
Return the name of this serializer.protected ConfigurablePofContext.PofConfig
Obtain the PofConfig that represents the initialized state of the ConfigurablePofContext.getPofSerializer
(int nTypeId) Return a PofSerializer that can be used to serialize and deserialize an object of the specified user type to and from a POF stream.int
Determine the user type identifier associated with the given class.int
Determine the user type identifier associated with the given object.int
getUserTypeIdentifier
(String sClass) Determine the user type identifier associated with the given class name.protected int
Determine the user type identifier associated with the given class.protected int
getUserTypeIdentifierInternal
(String sClass) Determine the user type identifier associated with the given class name.protected void
Bind the ConfigurablePofContext to a ClassLoader, resolving all class names, etc.protected PofSerializer
instantiateSerializer
(XmlElement xmlSer, int nTypeId, Class clz) Create aPofSerializer
from the provided XML serializer element.protected PofSerializer
<?> instantiateSerializer
(Class<? extends PofSerializer> clzSer, Integer nTypeId, Class<?> clz) Instantiate specifiedPofSerializer
.protected boolean
Determine if the ConfigurablePofContext has completed its initialization.protected boolean
Determine if the ConfigurablePofContext supports the configuration of user types by specifying an interface (instead of a class) for the Java type.protected boolean
Determine if implicit root lambda class processing is allowed.boolean
Returntrue
ifPofReader.readObject(int)
method should return the appropriate Java 8 date/time type, orfalse
if a legacy date/time types should be returned in order to preserve backwards compatibility.boolean
Determine if Identity/Reference type support is enabled for this ConfigurablePofContext.protected boolean
Determine if the ConfigurablePofContext supports the serialization of an object that is an instance of a sub-class of a configured type, but not actually an instance of a class of a configured type.boolean
isUserType
(Class clz) Determine if the given class is a user type known to this PofContext.boolean
isUserType
(Object o) Determine if the given object is of a user type known to this PofContext.boolean
isUserType
(String sClass) Determine if the class with the given name is a user type known to this PofContext.protected Class
Find the specified class, return a Java Class object for it.static void
mergeIncludes
(String sURI, XmlElement xmlConfig, ClassLoader loader) Merge all included POF configuration files into the given xml configuration.protected void
Assemble and throw an informative exception based on the passed details.void
serialize
(WriteBuffer.BufferOutput out, Object o) Serialize an object to a WriteBuffer by writing its state using the specified BufferOutput object.void
setConfig
(XmlElement xml) Specify the configuration for the object.void
setContextClassLoader
(ClassLoader loader) Specify the context ClassLoader for this object.void
setIndexFileName
(String sIndexFile) Set the Jandex index file name to use to discover portable types.void
setPreferJavaTime
(boolean fPreferJavaTime) Set the flag specifying if Java 8 date/time types (java.time.*) should be preferred over legacy types.void
setReferenceEnabled
(boolean fReferenceEnabled) Set the referenceEnabled flag.toString()
Return a description of this ConfigurablePofContext.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 Details
-
PROPERTY_CONFIG
The name of the system property ("tangosol.pof.config") that can be used to override the location of the default POF configuration file.The value of this property must be the name of a resource that contains an XML document with the structure defined in /coherence-pof-config.xsd (deployed in coherence.jar).
The default value for the "coherence.pof.config" system property is "coherence-pof-config.xml".
- See Also:
-
DEFAULT_RESOURCE
The name of the application resource that contains the default set of wire-format-to-object bindings.The default value for the resource name is "pof-config.xml". The default can be overriden by specifying a value for the
tangosol.pof.config
system property. -
ROOT_LAMBDA_CLASS
Marker serving as the implicit root class for all lambdas. -
DEFAULT_INDEX_FILE_NAME
Default Jandex index file.- See Also:
-
-
Constructor Details
-
ConfigurablePofContext
public ConfigurablePofContext()Default constructor.Create a default ConfigurablePofContext that will load configuration information from the locator specified in
DEFAULT_RESOURCE
. -
ConfigurablePofContext
Create a ConfigurablePofContext that will load configuration information from the specified locator.- Parameters:
sLocator
- the locator that specifies the location of the PofContext configuration file; the locator is either a valid path or a URL
-
ConfigurablePofContext
Create a ConfigurablePofContext that will use the passed configuration information.- Parameters:
xml
- an XmlElement containing information in the format of a configuration file used by ConfigurablePofContext
-
ConfigurablePofContext
Copy constructor for a ConfigurablePofContext.- Parameters:
that
- the ConfigurablePofContext to (shallow) copy from
-
-
Method Details
-
getConfig
Determine the current configuration of the object.- Specified by:
getConfig
in interfaceXmlConfigurable
- Returns:
- the XML configuration or null
-
setConfig
Specify the configuration for the object.Note that the configuration cannot be set after the ConfigurablePofContext is fully initialized.
- Specified by:
setConfig
in interfaceXmlConfigurable
- Parameters:
xml
- the XML configuration for the object- Throws:
IllegalStateException
- if the ConfigurablePofContext is already fully initialized
-
getContextClassLoader
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:
-
setContextClassLoader
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.Note that the ConfigurablePofContext will fully initialize when it is provided a ClassLoader.
- Specified by:
setContextClassLoader
in interfaceClassLoaderAware
- Parameters:
loader
- the context ClassLoader for this object- Throws:
IllegalStateException
- if the ConfigurablePofContext is already fully initialized
-
serialize
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
Deserialize an object from a ReadBuffer 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
- Parameters:
in
- the BufferInput with which to read the object's state- Returns:
- the deserialized user type instance
- Throws:
IOException
- if an I/O error occurs
-
getName
Description copied from interface:Serializer
Return the name of this serializer.- Specified by:
getName
in interfaceSerializer
- Returns:
- the name of this serializer
-
getPofSerializer
Return a PofSerializer that can be used to serialize and deserialize an object of the specified user type to and from a POF stream.- Specified by:
getPofSerializer
in interfacePofContext
- Parameters:
nTypeId
- the type identifier of the user type that can be serialized and deserialized using the returned PofSerializer; must be non-negative- Returns:
- a PofSerializer for the specified user type
-
getUserTypeIdentifier
Determine the user type identifier associated with the given object.- Specified by:
getUserTypeIdentifier
in interfacePofContext
- Parameters:
o
- an instance of a user type; must not be null- Returns:
- the type identifier of the user type associated with the given object
-
getUserTypeIdentifier
Determine the user type identifier associated with the given class.- Specified by:
getUserTypeIdentifier
in interfacePofContext
- Parameters:
clz
- a user type class; must not be null- Returns:
- the type identifier of the user type associated with the given class
-
getUserTypeIdentifier
Determine the user type identifier associated with the given class name.- Specified by:
getUserTypeIdentifier
in interfacePofContext
- Parameters:
sClass
- the name of a user type class; must not be null- Returns:
- the type identifier of the user type associated with the given class name
-
getClassName
Determine the name of the class associated with the given user type identifier.- Specified by:
getClassName
in interfacePofContext
- Parameters:
nTypeId
- the user type identifier; must be non-negative- Returns:
- the name of the class associated with the specified user type identifier
-
getClass
Determine the class associated with the given user type identifier.- Specified by:
getClass
in interfacePofContext
- Parameters:
nTypeId
- the user type identifier; must be non-negative- Returns:
- the class associated with the specified user type identifier
-
isUserType
Determine if the given object is of a user type known to this PofContext.- Specified by:
isUserType
in interfacePofContext
- Parameters:
o
- the object to test; must not be null- Returns:
- true iff the specified object is of a valid user type
-
isUserType
Determine if the given class is a user type known to this PofContext.- Specified by:
isUserType
in interfacePofContext
- Parameters:
clz
- the class to test; must not be null- Returns:
- true iff the specified class is a valid user type
-
isUserType
Determine if the class with the given name is a user type known to this PofContext.- Specified by:
isUserType
in interfacePofContext
- Parameters:
sClass
- the name of the class to test; must not be null- Returns:
- true iff the class with the specified name is a valid user type
-
isPreferJavaTime
public boolean isPreferJavaTime()Returntrue
ifPofReader.readObject(int)
method should return the appropriate Java 8 date/time type, orfalse
if a legacy date/time types should be returned in order to preserve backwards compatibility.- Specified by:
isPreferJavaTime
in interfacePofContext
- Returns:
true
if Java 8 date/time types should be preferred over legacy types
-
getUserTypeIdentifierInternal
Determine the user type identifier associated with the given class.- Parameters:
clz
- a user type class; must not be null- Returns:
- the type identifier of the user type associated with the given class or -1 if the user type is unknown to this PofContext
-
getInheritedUserTypeIdentifier
Helper method for determining the user type identifier associated with a given class that does not have a direct configured association.- Parameters:
clz
- a user type class; must not be null- Returns:
- the type identifier of the user type associated with the given class or -1 if the user type and its superclass(es) and implemented interface(s) are unknown to this PofContext
-
getUserTypeIdentifierInternal
Determine the user type identifier associated with the given class name.- Parameters:
sClass
- the name of a user type class; must not be null- Returns:
- the type identifier of the user type associated with the given class name or -1 if the user type is unknown to this PofContext
-
isInitialized
protected boolean isInitialized()Determine if the ConfigurablePofContext has completed its initialization.- Returns:
- true iff the initialization is complete
-
getConfigLocation
Obtain the location of the configuration that the ConfigurablePofContext used to configure itself.- Returns:
- the location information for the configuration for the ConfigurablePofContext, or null if not yet initialized and no location was specified
-
getPofConfig
Obtain the PofConfig that represents the initialized state of the ConfigurablePofContext.- Returns:
- the PofConfig for the ConfigurablePofContext, or null if not yet initialized
-
isInterfaceAllowed
protected boolean isInterfaceAllowed()Determine if the ConfigurablePofContext supports the configuration of user types by specifying an interface (instead of a class) for the Java type.- Returns:
- true iff an interface name is acceptable in the configuration as the class of a user type
-
isSubclassAllowed
protected boolean isSubclassAllowed()Determine if the ConfigurablePofContext supports the serialization of an object that is an instance of a sub-class of a configured type, but not actually an instance of a class of a configured type.- Returns:
- true iff serialization of sub-classes is explicitly enabled
-
isLambdaAllowed
protected boolean isLambdaAllowed()Determine if implicit root lambda class processing is allowed.- Returns:
- true iff implicit root lambda class processing is allowed.
-
isReferenceEnabled
public boolean isReferenceEnabled()Determine if Identity/Reference type support is enabled for this ConfigurablePofContext.- Returns:
- true if Identity/Reference type support is enabled
-
setReferenceEnabled
public void setReferenceEnabled(boolean fReferenceEnabled) Set the referenceEnabled flag.- Parameters:
fReferenceEnabled
- the referenceEnabled flag to set
-
setPreferJavaTime
public void setPreferJavaTime(boolean fPreferJavaTime) Set the flag specifying if Java 8 date/time types (java.time.*) should be preferred over legacy types.- Parameters:
fPreferJavaTime
- whether Java 8 data/time types
-
setIndexFileName
Set the Jandex index file name to use to discover portable types.- Parameters:
sIndexFile
- Jandex index file name to use to discover portable types
-
getIndexFileName
Return the Jandex index file name to use to discover portable types.- Returns:
- the Jandex index file name to use to discover portable types
-
toString
Return a description of this ConfigurablePofContext. -
checkNotInitialized
protected void checkNotInitialized()Verify that the ConfigurablePofContext has not already been initialized.- Throws:
IllegalStateException
- if the ConfigurablePofContext is already fully initialized
-
ensureInitialized
protected void ensureInitialized()Fully initialize the ConfigurablePofContext if it has not already been initialized. -
initialize
protected void initialize()Bind the ConfigurablePofContext to a ClassLoader, resolving all class names, etc. -
createPofConfig
Create a PofConfig object based on a configuration that was either provided as XML, or can be loaded from the specified (or default) URI using the provided ClassLoader.- Returns:
- a PofConfig for this ConfigurablePofContext
-
instantiateSerializer
Create aPofSerializer
from the provided XML serializer element.- Parameters:
xmlSer
- xml defining the serializer to createnTypeId
- the user type id this class is registered withclz
- the class of the user type- Returns:
- a PofSerializer implementation capable of (de)serializing clz
-
instantiateSerializer
protected PofSerializer<?> instantiateSerializer(Class<? extends PofSerializer> clzSer, Integer nTypeId, Class<?> clz) Instantiate specifiedPofSerializer
.- Parameters:
clzSer
- the class of the serializer to createnTypeId
- the user type id this class is registered withclz
- the class of the user type- Returns:
- a PofSerializer implementation capable of (de)serializing clz
-
loadClass
Find the specified class, return a Java Class object for it.- Parameters:
sClass
- the fully qualified class name- Returns:
- the Class object for the specified class name, never null
- Throws:
RuntimeException
- a RuntimeException (or a subclass thereof) is thrown if the specified Class could not be loaded
-
report
Assemble and throw an informative exception based on the passed details.- Parameters:
sURI
- the URI of the configurationnTypeId
- the type ID (if applicable and if known)sClass
- the user type class name (if applicable and if known)e
- the underlying exception, if anysText
- the detailed description of the problem- Throws:
IllegalStateException
- always thrown
-
mergeIncludes
Merge all included POF configuration files into the given xml configuration.- Parameters:
sURI
- the URI of the POF configuration filexmlConfig
- the base POF configurationloader
- theClassLoader
used to find the included POF configuration resources
-
appendDefaultSerializerToUserTypes
Process <default-serializer> element from the specified xml configuration and append information about the serializer to each <user-type> element within <user-type-list> unless user type already has a serializer specified.This method could be overridden to add new custom configuration elements to pof-config.
- Parameters:
xmlConfig
- the XmlElement containing pof configuration
-