Dynamic loading of .class files and deserializing of object
We have such scenario:
There is a client and server nodes working together. Server node
transfers a .class (or .jar) file to the client with the serialized
version of object of the corresponding class. Client receives these
files and stores them in some folder. Now, we need to load that .class
(or .jar) file and deserialize object. Server also transfers full
qualifying class name as string for class loader to load class. The
problem rises when we try to deserialize the class. We tried to create
the instance of URLClassLoader with .class (or .jar) file and then we
tried to set that class loader as default for thread, but we received
error when doing deserialization. Then we tried to create our own
class loader (ExtendableClassLoader), set it as default and then do
the deserialization. Error was still there. :)
The sample code goes below:
/**
* Load file method is called by server using RMI
* @param file Bytes of .class or .jar file
* @param classTask Serialized object
* @param className Full-qualifying class name
* @param fileName File name of the .class or .jar file
*/
public void loadFile(byte[] file, byte[] classTask, String className,
String fileName)
throws RemoteException {
...
File jarFile = new File("./lib/" + fileName);
try {
// Saving .class or .jar file to fileName from bytes
FileOutputStream fos = new FileOutputStream(jarFile);
fos.write(file);
fos.close();
ExtendableClassLoader.getInstance().addClassPath(jarFile.getAbsolutePath());
Object object = null;
try {
Class workClass1 =
ExtendableClassLoader.getInstance().findClass("java.io.ByteArrayInputStream");
Constructor constr = null;
Class[] parameterTypes = new Class[]
{ byte[].class };
constr =
workClass1.getConstructor(parameterTypes);
ByteArrayInputStream bais =
(ByteArrayInputStream) constr.newInstance(classTask);
Class workClass2 =
ExtendableClassLoader.getInstance().findClass("java.io.ObjectInputStream");
Constructor constrOis = null;
Class[] parameterTypesOis = new Class[]
{ InputStream.class };
constrOis =
workClass2.getConstructor(parameterTypesOis);
ObjectInputStream ois = (ObjectInputStream)
constrOis.newInstance(bais);
object = ois.readObject();
} catch (Exception e) {
// We've got here when the readObject method is
called
}
} catch (IOException e) {
e.printStackTrace();
}
}