Jason Greene [
http://community.jboss.org/people/jason.greene] modified the document:
"Modular Serialization"
To view the document, visit:
http://community.jboss.org/docs/DOC-17244
--------------------------------------------------------------
h2. Understanding Class-loading Issues
When an object is serialized using Java serialization, the format consists of a
"class reference" and the field data of every field in all inheriting classes.
The class reference is simply a class name. *+The assumption is that the recieving party
has all of the sender's class definitions readily accessable to the thread's
current context class loader.+* This means very different semantics than those commonly
expected in a normal in-vm invocation accross class loader boundaries. In a modularized
environment, it is not only common, but considered good practice to only share public API
types and yet still pass internal implementation classes that back the API types across
module boundaries. These "opaque" structures don't cause the VM a problem
because the same phsysical, already constructed, object instance is being passed around.
However, with serialization there is no object instance. All fields of all types that make
up an object need to be accessed to recreate the instance. Also since fields themesleves
may be references to other custom types, it is quite common for a large graph referencing
numerous types to be on the wire, and thus have a need for visibility.
h2. Problem A - Subclass Visibility
In this example a common super class is shared between a sender and a receiver, but an
extened subclass used by the sender's implementation is mistakenly not shared. This
case works just fine with local IN-VM invocations, but will fail once serialization is
involved.
http://community.jboss.org/servlet/JiveServlet/showImage/102-17244-7-1706...
http://community.jboss.org/servlet/JiveServlet/downloadImage/102-17244-7-...
h2. Problem B - Reference / Aggregation Visibility
In this example a common class is shared between a sender and a receiver, but it contains
a field which references a sender class that is mistakenly not shared. This case works
just fine with local IN-VM invocations, but will fail once serialization is involved.
http://community.jboss.org/servlet/JiveServlet/showImage/102-17244-7-1706...
http://community.jboss.org/servlet/JiveServlet/downloadImage/102-17244-7-...
h2. Solution 1 - Require Components To Share All Needed Classes
This may require altering the application / component code to always use common shared
classes in the serialization stream. A negative side effect of this is that it puts a
larger burdeon on the application / component developer. A positive benefit is that the
wire format is compatible standard serialization.
h2. Solution 2 - JBoss Marshaling - Modular Serialization
This solution is currently available using the ModularClassResolver like so:
RiverMarshallerFactory factory = new RiverMarshallerFactory();
MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setClassResolver(ModularClassResolver.getInstance(moduleLoader));
This changes the format of the stream such that the owning module identifier is written in
addition to the name of any class referenced in the stream. This allows jboss marshalling
to easily locate the "correct" class loader for deserializing the class. The
advantage of this approach is that application/component code need not be changed, and
that the same practices used in local invocation also apply with remote serialization. The
drawback is that the serialization format must be alteredm potentially introducing
compatiblity problems with older clients. In addition non-modular clients, if they need to
be supported, will not understand the format. Solution 3 is the recommended approach when
compatibility is a concern
h2. Solution 3 - Support both Modular and Standard Serialization
This is the same approach as 2 but adds the ability to handle both styles of streaming.
This is achieved by either having the client ignore the information, or having some kind
of negotiation process (protocol version etc).
--------------------------------------------------------------
Comment by going to Community
[
http://community.jboss.org/docs/DOC-17244]
Create a new document in JBoss AS7 Development at Community
[
http://community.jboss.org/choose-container!input.jspa?contentType=102&am...]