users@shoal.java.net

re: Message serialization question

From: Jim Marino <jim.marino_at_gmail.com>
Date: Wed, 1 Oct 2008 09:03:41 -0700

Hi Michael,

Thanks for the tip. Our project (www.fabric3.org) takes a slightly
different approach. We have a custom ObjectOutputStream that annotates
the class with the classloader name which is then read by a custom
ObjectInputStream. In our system, classloaders are named (a URI, which
would be similar to a bundle name in OSGi) and stored in a registry
where they can be referenced during deserialization. Our custom
ObjectOutputStream looks like this:

public class MultiClassLoaderObjectOutputStream extends
ObjectOutputStream {
     //....

    protected void annotateClass(Class<?> cl) throws IOException {
         if (cl.getClassLoader() instanceof MultiParentClassLoader) {
             // write the classloader id
             String id = ((MultiParentClassLoader)
cl.getClassLoader()).getName().toString();
             this.writeByte(id.length());
             writeBytes(id);
         } else {
             // use normal classloader resolution
             this.writeByte(-1);
         }
     }
}


public class MultiClassLoaderObjectInputStream extends
ObjectInputStream {
     private ClassLoaderRegistry registry;

     public MultiClassLoaderObjectInputStream(InputStream in,
ClassLoaderRegistry registry) throws IOException {
         super(in);
         this.registry = registry;
     }

     protected Class<?> resolveClass(ObjectStreamClass desc) throws
IOException, ClassNotFoundException {
         int val = readByte();
         if (val == -1) {
             return super.resolveClass(desc);
         } else {
             byte[] bytes = new byte[val];
             int result = read(bytes);
             if (result == -1) {
                 throw new IOException("Invalid classloader URL");
             }
             String id = new String(bytes);
             URI uri = URI.create(id);
             ClassLoader cl = registry.getClassLoader(uri);
             if (cl == null) {
                 throw new IOException("Classloader not found: " + id);
             }
             return Class.forName(desc.getName(), false, cl);
         }
     }

}

Jim



----------------------------------------------------------------------------
Date: Tue, 30 Sep 2008 19:47:27 +0200
From: Michael Bien <mbien_at_fh-landshut.de>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Subject: [Shoal-Users] Message serialization question


Hi,

i had almost the same problem in FishFarm i just called the solution
distributed-classloader instead of peer-classloader :-).

FishFarm has worker nodes which steal work from overstrained other nodes
executing computational expensive tasks (@see jsr166y). This means that
the overstrained node (currently executing the task) knows the resources
which are required from the idle node anyway (since he was already
executing the task...).

solution is obvious... ask friendly for resources if you need them after
stealing a task.

DistributableObject (ala plain old MarshalledObject, just simpler and
fancier):
https://fishfarm.dev.java.net/source/browse/fishfarm/trunk/FishFarm/src/net/java/fishfarm/DistributeableObject.java?rev=183&view=markup

GridResourceLoader:
https://fishfarm.dev.java.net/source/browse/fishfarm/trunk/FishFarm/src/net/java/fishfarm/GridResourceLoader.java?rev=183&view=markup

GridClassLoader:
https://fishfarm.dev.java.net/source/browse/fishfarm/trunk/FishFarm/src/net/java/fishfarm/lang/ExtendableClassloader.java?rev=183&view=markup

ResourceRequestHandler:
https://fishfarm.dev.java.net/source/browse/fishfarm/trunk/FishFarm/src/net/java/fishfarm/ResourceRequestHandler.java?rev=174&view=markup


usage:
register ResourceRequestHandler
wrap object in DistributableObject, serialize, send byte[],

receive byte[], deserialize
obj.get(gridclassloader).

done

keep in mind FishFarm is GPL+cp ex, i don't want that you get problems
with your employee... (refactor, rename it etc ;-) )

best regards,

michael

btw, fishfarm does not support resource versions with this technique...
simplicity first (since you mentioned OSGI)