I am creating a class file at run time.
I want to replace the existing class file with the updated one in the class loader.
It is similar to hot swapping (e.g. JRebel) which avoids server restart and redeployment.
I found context.xml approach for tomcat for context reloading. But it is not very useful in case of production environment.
Can we register class with ClassLoader at runtime? Please suggest if any alternative is there to reload class at runtime.
I am using following code to retrieve current classLoader.
ClassLoader classLoader = LoggingAspect.class.getClassLoader();
Below is the implementation of load class method.
public class AspectClassLoader extends ClassLoader{
@Override
public synchronized Class<?> loadClass(String name) throws ClassNotFoundException
{
String customLoadClass = "com.log.LoggingAspect";
try
{
if(!customLoadClass.equals(name))
{
return super.loadClass(name);
}
else
{
URL classLoadUrl = new URL(this.reloadClassUrl);
URLConnection connection = classLoadUrl.openConnection();
InputStream input = connection.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int data = input.read();
while(data != -1){
buffer.write(data);
data = input.read();
}
input.close();
byte[] classData = buffer.toByteArray();
return defineClass(name, classData, 0, classData.length);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
To reload class i am using following code.
AspectClassLoader urlClsLoader = new AspectClassLoader(classLoader);
Class aspect = urlClsLoader.reloadClass("com.log.LoggingAspect", true, new String("file:"+className));
My reload method of the classloader is
public synchronized Class<?> reloadClass(String name, boolean resolve, String reloadClassUrl) throws ClassNotFoundException
{
this.reloadClassUrl = reloadClassUrl;
return this.loadClass(name, resolve);
}
After reloading if i create the new instance of LoggingAspect it still gives me the old instance. Please suggest.
Class aspect = urlClsLoader.reloadClass("com.log.LoggingAspect", true, new String("file:"+className));
com.log.aspect.Aspect aspectObj = (com.log.aspect.Aspect)aspect.newInstance();
Still I am getting the old instance.
Please suggest why classloader does not load the modified class?