tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Lynn, James" <jl...@e-medsoft.com>
Subject RE: Servlet reloading?
Date Sat, 12 Feb 2000 23:46:04 GMT
I really don't understand the need to serialize and then deserialize
everything. If your class loaded caches classes into a Hashtable, and this
Hashtable is passed to the next instance of the ClassLoader, then the class
is the same. The second class loader can then invalidate classes in the
cache that are older than the existing class files. In this way, session
data can be preserved across ClassLoaders. I've included an example
ClassLoader that demonstrates what I mean. The output is
New
0.620997906643284
Found
0.620997906643284


Have I misunderstood something?

// --- begin file CL.java -----

import java.util.Hashtable;
class CL extends ClassLoader
{
 Hashtable loaded;
 CL() { loaded=new Hashtable(); }
 CL(Hashtable previouslyLoaded) { loaded=previouslyLoaded; }

 public Class loadClass(String name) throws ClassNotFoundException
 {
 Class c;

 // if class file is newer load
 //  {
 //  }
 // else
 //  {
   c=(Class)loaded.get(name);
 //  }
 if (c!=null)
  {
  System.out.println("Found");
  return c;
  }
 c=super.loadClass(name);
 loaded.put(name,c);
 return c;
 }
 public static void main(String[] s) throws Exception
 {
 CL cl=new CL();
 Object foo=cl.loadClass("Foo").newInstance();
 
 cl=new CL(cl.loaded);
 Object foo2=cl.loadClass("Foo").newInstance();
 }
}

class Foo
{
  static Hashtable session=new Hashtable();
  Foo()
  {
  Object o=session.get("data");
  if (o==null)
   {
   System.out.println("New");
   session.put("data",o=""+Math.random());
   }
  System.out.println(o);
  }
}

Mime
View raw message