import java.util.*; import java.net.*; import java.io.*; // Le class loader ! class ProgramLoader extends ClassLoader { // the base directory for loading local classes String basedir; boolean url; // The cache for name binding. Hashtable cache = new Hashtable(); // -------------------- // Private method to read an input strean until the end private int read(InputStream ds, byte zone[]) throws IOException { int n,off=0,len=zone.length; while (len>0) { n = ds.read(zone, off, len); if (n == -1) return off; len -= n; off +=n; } return len; } // -------------------- // Private method that read a class from a local file. private byte loadClassDataFromFile (String name)[] { File class_file; FileInputStream class_stream; long len = 0; class_file = new File(basedir+"/"+name+".class"); if (!class_file.exists()) return null; len = class_file.length(); byte data[] = new byte[(int)len]; try { class_stream = new FileInputStream(class_file); int x = read(class_stream, data); return data; } catch (FileNotFoundException ex) { System.out.println("loadClassDataFromFile: "+ex); return null; } catch (IOException ex) { System.out.println("loadClassDataFromFile: "+ex); return null; } } // -------------------- // Private method that read a class from a web server. // Sorry, I can't do better !!! // I can't get the size of the remote file before I read it ... private byte loadClassDataFromURL (String name)[] { try { //System.out.println("basedir : "+basedir); //System.out.println("name : "+name); URL remloc = new URL(basedir+"/"+name+".class"); DataInputStream dis = new DataInputStream(remloc.openStream()); byte data[] = new byte[50000]; int x = read(dis, data); byte data2[] = new byte[x]; for (int i = 0;i Load Class "+name); // First case: the name is already resolved in the cache. theclass = (Class)cache.get(name); if (theclass != null) { //System.out.println("Class "+name+" found in the cache"); return theclass; } try { theclass = findSystemClass(name); //System.out.println(" Class "+name+" is a system class"); cache.put(name,theclass); return theclass; } catch (ClassNotFoundException ex) { // Try to load it as a class from a file or web server byte data[]; if (url) data = loadClassDataFromURL(name); else data = loadClassDataFromFile(name); if (data != null) { try { //System.out.println(" +++++Class "+name+" is loaded remotly"); theclass = defineClass(name,data,0,data.length); cache.put(name,theclass); return theclass; } catch (ClassFormatError ex1) { System.out.println("loadClass: "+ex1); return null; } } } System.out.println("loadClass: class "+name+" not found"); return null; } }