GemFire 6.6

com.gemstone.gemfire.pdx
Class ReflectionBasedAutoSerializer

java.lang.Object
  extended by com.gemstone.gemfire.pdx.ReflectionBasedAutoSerializer
All Implemented Interfaces:
Declarable, PdxSerializer

public class ReflectionBasedAutoSerializer
extends Object
implements PdxSerializer, Declarable

This class uses Java reflection in conjunction with PdxSerialzer to perform automatic serialization of domain objects. The implication is that the domain classes do not need to implement the PdxSerializable interface.

This implementation will serialize all relevant fields

For example:

 List<String> domainClasses = new ArrayList<String>();
 domainClasses.add("com.foo.DomainObject");
 Cache c = new CacheFactory().set("cache-xml-file", cacheXmlFileName)
     .setPdxSerializer(new ReflectionBasedAutoSerializer(domainClasses))
     .create();
 

In this example DomainObject would not need to implement PdxSerializable to be serialized.

The equivalent cache.xml entries might be as follows:

 <pdx>
   <pdx-serializer>
     <class-name>
       com.gemstone.gemfire.pdx.ReflectionBasedAutoSerializer
     </class-name>
     <parameter name="classes">
       <string> com.company.domain.DomainObject </string>
     </parameter>
   </pdx-serializer>
 </pdx>
 
See setSerializableClasses for additional details on the format of the parameter string.

Since:
6.6
Note: Early Access. Please consult GemStone technical support for assistance with this functionality.

Constructor Summary
ReflectionBasedAutoSerializer()
          Default constructor primarily used during declarative configuration via the cache.xml file.
ReflectionBasedAutoSerializer(List<String> classes)
          Constructor which takes a list of class name patterns which are to be auto-serialized.
 
Method Summary
 Object fromData(Class clazz, PdxReader reader)
          Method implemented from PdxSerializer which performs object de-serialization.
 Properties getConfig()
          Return a Properties object with a representation of the current config.
 void init(Properties props)
          Used for declarative class initialization from cache.xml.
 void setSerializableClasses(List<String> patterns)
          Method to configure classes to consider for serialization, to set any identity fields and to define any fields to exclude from serialization.
 boolean toData(Object obj, PdxWriter writer)
          Method implemented from PdxSerializer which performs object serialization.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionBasedAutoSerializer

public ReflectionBasedAutoSerializer()
Default constructor primarily used during declarative configuration via the cache.xml file.


ReflectionBasedAutoSerializer

public ReflectionBasedAutoSerializer(List<String> classes)
Constructor which takes a list of class name patterns which are to be auto-serialized.

Parameters:
classes - a list of patterns which are matched against domain class names to determine whether they should be serialized
Method Detail

setSerializableClasses

public final void setSerializableClasses(List<String> patterns)
Method to configure classes to consider for serialization, to set any identity fields and to define any fields to exclude from serialization.

Each string in the list represents a definition in the following form:

   <class pattern>#identity=<identity field pattern>#exclude=<exclude field pattern>
 
The hash (#) characters are separators and are not part of the parameter name. An example would be:
   com.company.DomainObject.*#identity=id.*#exclude=creationDate
 
This would select all classes with a class name beginning with com.company.DomainObject and would select as PDX identity fields any fields beginning with id and would not serialize the field called creationDate.

There is no association between the the identity and exclude options, so the above example could also be expressed as:

   com.company.DomainObject.*#identity=id.*
   com.company.DomainObject.*#exclude=creationDate
 
Note that all defined patterns are used when determining whether a field should be considered as an identity field or should be excluded. Thus the order of the patterns is not relevant.

Parameters:
patterns - the list of definitions to apply

toData

public boolean toData(Object obj,
                      PdxWriter writer)
Method implemented from PdxSerializer which performs object serialization.

Specified by:
toData in interface PdxSerializer
Parameters:
obj - the object to serialize
writer - the PdxWriter to use when serializing this object
Returns:
true if the object was serialized, false otherwise

fromData

public Object fromData(Class clazz,
                       PdxReader reader)
Method implemented from PdxSerializer which performs object de-serialization.

Specified by:
fromData in interface PdxSerializer
Parameters:
clazz - the class of the object to re-create
reader - the PdxReader to use when creating this object
Returns:
the deserialized object if this serializer handles the given class, null otherwise.

init

public void init(Properties props)
Used for declarative class initialization from cache.xml. The following property may be specified:

getConfig

public Properties getConfig()
Return a Properties object with a representation of the current config. Depending on how this ReflectionBasedAutoSerializer was configured, the returned property value will have the correct semantics but may differ from the the original configuration string.

Returns:
a Properties object

GemFire 6.6

Copyright © 1997-2011 VMware, Inc. All rights reserved.