net.sf.fikin.classmarshaling
Class MarshaledObject

java.lang.Object
  extended by net.sf.fikin.classmarshaling.MarshaledObject
All Implemented Interfaces:
java.io.Serializable

public class MarshaledObject
extends java.lang.Object
implements java.io.Serializable

Wraps an object into a serializable representation.

Unlike straight forward implementation with java.io.Serializable, this object wraps object's class information in addition to the object itself thus allowing the object to be de-serialized in an environment where the class information might be missing.

During serialization, MarshaledObject will serialize the object and all its classes, which ClassSelector.isClassToBeMarshaled(Class) indicates.

During de-serializing, MarshaledObject will resolve the classes either from the default classloader or from the serialization stream.

An example how to serialize an object:

      import net.sf.fikin.classmarshaling.MarshaledObject;
      import net.sf.fikin.classmarshaling.Selector;
      import net.sf.fikin.classmarshaling.util.NamePrefixedClassSelector;
      
      Selector selector = new NamePrefixedClassSelector({ "mypkg", "mycls" });
      // not serialize the "mo" instead of "myObj"
      MarshaledObject mo = new MarshaledObject( myObj, selector );
 
where following: will be serialized and wrapped by "mo".

To de-serialize the object:

      import net.sf.fikin.classmarshaling.MarshaledObject;
      // de-serialize the "mo"
      MarshaledObject mo = ...;
      
      MyCls myObj = (MyCls) mo.get();
 
created on Aug 2, 2005

Since:
Version:
$Revision: 1.2 $
Author:
fiykov
See Also:
Serialized Form

Constructor Summary
MarshaledObject(java.lang.Object obj, ClassSelector selector)
          Wrapper for the object and selected classes of his, represented internally in a serialized form.
 
Method Summary
 java.lang.Object get()
          Returns a new instance of the contained marshaled (serialized) object.
 java.lang.Object get(java.lang.ClassLoader parent)
          Returns a new instance of the contained marshaled (serialized) object.
protected  java.util.Map getClassBytecodes()
          Get a new instance of the serialized classes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MarshaledObject

public MarshaledObject(java.lang.Object obj,
                       ClassSelector selector)
                throws java.io.IOException
Wrapper for the object and selected classes of his, represented internally in a serialized form.

Once wrapped, a new instance of the object can be extracted via get() and get(ClassLoader).

Parameters:
obj - to be marshaled
selector - is selecting which classes are to be serialized together with the object
Throws:
java.io.IOException - in case an internal error occurred during serializing the object or its classes
Method Detail

get

public java.lang.Object get()
                     throws java.io.IOException,
                            java.lang.ClassNotFoundException
Returns a new instance of the contained marshaled (serialized) object.

Context class loader is used to initially resolve classes.
If not found then the list with serialized classes is checked.
If not found there either de-serialization will fail with ClassNotFoundException.

Returns:
new instance of the contained object
Throws:
java.io.IOException - in case an internal error occurred while de-serializing the object or its classes.
java.lang.ClassNotFoundException - when during the object de-serialization, some class was needed but not found by the given classloader, neither in the serialized classes. could not be found

get

public java.lang.Object get(java.lang.ClassLoader parent)
                     throws java.io.IOException,
                            java.lang.ClassNotFoundException
Returns a new instance of the contained marshaled (serialized) object.

Given class loader is used to initially resolve classes.
If not found then the list with serialized classes is checked.
If not found there either de-serialization will fail with ClassNotFoundException.

Parameters:
parent - classloader
Returns:
new instance of the contained object
Throws:
java.io.IOException - in case an internal error occurred while de-serializing the object or its classes.
java.lang.ClassNotFoundException - when during the object de-serialization, some class was needed but not found by the given classloader, neither in the serialized classes. could not be found

getClassBytecodes

protected java.util.Map getClassBytecodes()
                                   throws java.io.IOException,
                                          java.lang.ClassNotFoundException
Get a new instance of the serialized classes.

Returned map is used by MarshaledClassLoader, which is responsible for de-serializing the object.

Returns:
a map with serialized classes. the class name is the key and the class bytecode (byte[]) is the value.
Throws:
java.io.IOException - in case of internal de-serialization error
java.lang.ClassNotFoundException - in case the serialized map with classes cannot be de-serialized because of missing class


Copyright © 2007 Nikolay Fiykov. All Rights Reserved.