The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Dynamic Extension



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Class Reloading

Posted by Damian Gavalas on June 02, 1999 at 9:52 AM

Hello everybody,

I have a question related to dynamic reloading of Java classes.

I'm building an application where a client application downloads through the network (TCP connection) objects with persistent state. These objects definitions (class files) are located on a local client directory. In case that no dynamic reloading is considered, the object's state may be restored by invoking the readObject() method.

ServerSocket serSock = new ServerSocket(1000, 3);
Socket connection = serSock.accept();
InputStream is = connection.getInputStream();
ObjectInputStream dataIn = new ObjectInputStream(new GZIPInputStream(is));
Object o = dataIn.readObject();

In case that the downloaded object's class file directory belongs in the CLASSPATH, the readObject() restores the object's state normally (I believe because the object's class definition is loaded by the default ClassLoader), otherwise a ClassNotFoundException is thrown.

My problem is that the downloaded objects class definitions are modified in runtime, so they have to be reloaded by the client applications. In order to do that, I followed Bill Venners advice and I create a new instance of a custom ClassLoader (new name space) whenever a class definition is updated. However a came across the problem that I described previously. If I just needed to create a new instance of the updated class, that would be easy, through:

NewClassLoader loader = new NewClassLoader(directory);
Class downloadedClass = loader.loadClass(className);
Object o = downloadedClass.newInstance();

// The downloadedClass is updated
loader = new NewClassLoader(directory);
downloadedClass = loader.loadClass(className);
o = downloadedClass.newInstance();

However the situation is different here, where an existing object's state needs to be reconstructed. Is there any way to force the dataIn.readObject() object to be loaded by a custom ClassLoader (NewClassLoader) ???? Could anybody suggest any way to work around the problem?

Thanks very much in advance,


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us