1 package com.quadcap.io.dir; 2 3 40 41 import java.util.Enumeration ; 42 import java.util.Hashtable ; 43 import java.util.Vector ; 44 45 import java.io.File ; 46 import java.io.FileOutputStream ; 47 import java.io.InputStream ; 48 import java.io.IOException ; 49 50 import java.net.URL ; 51 52 import com.quadcap.io.IO; 53 54 55 60 public class ClassLoader extends java.lang.ClassLoader { 61 Directory root; 62 Vector jars = new Vector (); 63 64 public ClassLoader(Directory root) { 65 super(); 66 this.root = root; 67 try { 68 init(); 69 } catch (IOException ex) { 70 } 71 } 72 73 74 public Class loadClass(String name) throws ClassNotFoundException { 75 return loadClass(name, true); 76 } 77 78 public synchronized Class loadClass(String name, boolean resolve) 80 throws ClassNotFoundException 81 { 82 Class c = findLoadedClass(name); 84 if (c == null) { 85 try { 86 c = findClass(name); 87 } catch (ClassNotFoundException e) { 88 c = super.loadClass(name, resolve); 89 } 90 } 91 if (resolve) { 92 resolveClass(c); 93 } 94 return c; 95 } 96 97 public Class findClass(String name) throws ClassNotFoundException { 98 byte[] b = loadClassData(name); 99 return defineClass(name, b, 0, b.length, 100 this.getClass().getProtectionDomain()); 101 } 102 103 public URL findResource(String name) { 104 URL url = null; 105 try { 106 String fileName = "META-INF" + name; 107 for (int i = 0; url == null && i < jars.size(); i++) { 108 Directory d = (Directory)jars.elementAt(i); 109 url = d.getURL(name); 110 } 111 } catch (Throwable t) { 112 url = null; 113 } 114 return url; 115 } 116 117 private byte[] loadClassData(String name) throws ClassNotFoundException { 118 try { 119 Entry classFile = locateClassFile(name); 120 byte[] b = new byte[(int)(classFile.getSize())]; 121 InputStream f = classFile.getInputStream(); 122 IO.readFully(f, b); 123 f.close(); 124 return b; 125 } catch (IOException e) { 126 throw new ClassNotFoundException ("error loading class: " + 127 e.toString()); 128 } 129 } 130 131 final Entry locateClassFile(String name) throws ClassNotFoundException { 132 String className = name.replace('.', '/') + ".class"; 133 Entry classFile = null; 134 for (int i = 0; classFile == null && i < jars.size(); i++) { 135 Directory d = (Directory)jars.elementAt(i); 136 classFile = d.getEntry(className); 137 } 138 if (classFile == null) { 139 throw new ClassNotFoundException ("not found: " + name); 140 } 141 return classFile; 142 } 143 144 static int tmpCount = 0; 145 146 public String getClassPath() { 147 StringBuffer sb = new StringBuffer (); 148 String delim = System.getProperty("path.separator"); 149 for (int i = 0; i < jars.size(); i++) { 150 Directory d = (Directory)jars.elementAt(i); 151 if (i > 0) sb.append(delim); 152 sb.append(d.getRootPath()); 153 } 154 return sb.toString(); 155 } 156 157 final void init() throws IOException { 158 Enumeration e = root.entries(); 159 Directory classes = null; 160 File tmpClasses = null; 161 while (e.hasMoreElements()) { 162 String path = e.nextElement().toString(); 163 if (path.endsWith(".jar")) { 164 Directory d = null; 165 String realPath = root.getRealPath(path); 166 if (realPath == null) { 167 throw new IOException ("No Path: " + path); 168 } 169 d = Directory.getDirectory(new File (realPath)); 170 jars.addElement(d); 171 } 172 } 173 } 174 } 175 176 | Popular Tags |