1 23 package com.sun.enterprise.util; 24 25 import java.io.IOException ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.util.Hashtable ; 29 import com.sun.enterprise.util.LocalStringManagerImpl; 30 31 public class FileClassLoader extends ClassLoader { 32 String codebase; 33 Hashtable cache = new Hashtable (); 34 35 private static LocalStringManagerImpl localStrings = 36 new LocalStringManagerImpl(FileClassLoader.class); 37 38 public FileClassLoader(String codebase) 39 { 40 this.codebase = codebase; 41 } 42 43 private byte[] loadClassData(String name) 44 throws IOException 45 { 46 String sep = System.getProperty("file.separator"); 48 String c = name.replace('.', sep.charAt(0)) + ".class"; 49 File file = new File (codebase + sep + c); 50 if (!file.exists()) { 51 File wf = new File (codebase + sep + "WEB-INF" + sep + "classes" + sep + c); 52 if (wf.exists()) { 53 file = wf; 54 } 55 } 56 FileInputStream fis = new FileInputStream (file); 57 int avail = fis.available(); 58 byte[] buf = new byte[avail]; 59 fis.read(buf); 60 fis.close(); 61 return buf; 62 } 63 64 String getClassName(File f) throws IOException , ClassFormatError { 65 FileInputStream fis = new FileInputStream (f); 66 int avail = fis.available(); 67 byte[] buf = new byte[avail]; 68 fis.read(buf); 69 fis.close(); 70 Class c = super.defineClass(null, buf, 0, avail); 71 return c.getName(); 72 } 73 74 77 public synchronized Class loadClass(String name, boolean resolve) 78 throws ClassNotFoundException 79 { 80 Class c = (Class )cache.get(name); 81 if (c == null) { 82 try { 83 byte data[] = loadClassData(name); 84 c = defineClass(null,data, 0, data.length); 85 if( !name.equals(c.getName()) ) { 86 throw new ClassNotFoundException (localStrings.getLocalString("classloader.wrongpackage", "", new Object [] { name, c.getName() })); 87 } 88 } 89 catch ( Exception ex ) { 90 c = Class.forName(name); 93 } 94 cache.put(name, c); 95 } 96 if (resolve) 97 resolveClass(c); 98 return c; 99 } 100 101 public String toString() 102 { 103 return "FileClassLoader: Codebase = "+codebase+"\n"; 104 } 105 } 106 | Popular Tags |