1 55 56 package org.apache.bsf.util; 57 58 import java.util.Hashtable ; 59 import java.io.*; 60 61 67 class BSFClassLoader extends ClassLoader { 68 Hashtable cache = new Hashtable (); 69 String tempDir = "."; 70 71 BSFClassLoader () { 74 } 75 public synchronized Class loadClass (String name, boolean resolve) 76 throws ClassNotFoundException { 77 Class c = (Class ) cache.get (name); 78 if (c == null) { 79 try { 81 c = findSystemClass (name); 82 cache.put (name, c); 83 return c; 84 } catch (ClassNotFoundException e) { 85 } 87 try { 88 byte[] data = loadClassData (name); 89 c = defineClass (name, data, 0, data.length); 90 cache.put (name, c); 91 } catch (Exception e) { 92 e.printStackTrace (); 93 throw new ClassNotFoundException ("unable to resolve class '" + 94 name + "'"); 95 } 96 } 97 if (resolve) 98 resolveClass (c); 99 return c; 100 } 101 private byte[] loadClassData (String name) throws Exception { 102 String fileName = tempDir + File.separatorChar + name + ".class"; 103 FileInputStream fi = new FileInputStream (fileName); 104 byte[] data = new byte[fi.available ()]; 105 fi.read (data); 106 fi.close(); 107 return data; 108 } 109 public void setTempDir (String tempDir) { 110 this.tempDir = tempDir; 111 } 112 } 113 | Popular Tags |