1 package com.quadcap.http.server22; 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.dir.Directory; 53 import com.quadcap.io.dir.Entry; 54 55 import com.quadcap.io.IO; 56 57 import com.quadcap.util.Debug; 58 59 60 65 public class WebClassLoader extends ClassLoader { 66 File tmpDir; 67 Directory root; 68 Vector jars = new Vector (); 69 70 public WebClassLoader(Directory root, File tmpDir) { 71 super(); 72 this.tmpDir = tmpDir; 73 this.root = root; 74 } 75 76 public Class loadClass(String name) throws ClassNotFoundException { 77 return loadClass(name, true); 78 } 79 80 98 public Class findClass(String name) throws ClassNotFoundException { 99 if (Trace.level() > 1) { 101 Debug.println("WebClassLoader[" + root + 102 "].findClass(" + name + ")"); 103 } 104 byte[] b = loadClassData(name); 106 return defineClass(name, b, 0, b.length, 107 this.getClass().getProtectionDomain()); 108 } 109 110 public URL findResource(String name) { 111 URL url = null; 112 try { 113 String fileName = "WEB-INF/classes" + name; 114 url = root.getURL(fileName); 115 if (url == null) { 116 for (int i = 0; url == null && i < jars.size(); i++) { 117 Directory d = (Directory)jars.elementAt(i); 118 url = d.getURL(name); 119 } 120 } 121 } catch (Throwable t) { 122 url = null; 123 } 124 return url; 125 } 126 127 private byte[] loadClassData(String name) throws ClassNotFoundException { 128 try { 129 Entry classFile = locateClassFile(name); 130 byte[] b = new byte[(int)(classFile.getSize())]; 131 InputStream f = classFile.getInputStream(); 132 IO.readFully(f, b); 133 f.close(); 134 return b; 135 } catch (IOException e) { 136 throw new ClassNotFoundException ("error loading class: " + 137 e.toString()); 138 } 139 } 140 141 final Entry locateClassFile(String name) throws ClassNotFoundException { 142 String className = name.replace('.', '/') + ".class"; 143 String fileName = "WEB-INF/classes/" + className; 144 Entry classFile = root.getEntry(fileName); 145 if (classFile == null) { 146 for (int i = 0; classFile == null && i < jars.size(); i++) { 147 Directory d = (Directory)jars.elementAt(i); 148 classFile = d.getEntry(className); 149 } 150 } 151 if (classFile == null) { 152 throw new ClassNotFoundException ("not found: " + name); 153 } 154 return classFile; 155 } 156 157 static int tmpCount = 0; 158 159 public String getClassPath() { 160 StringBuffer sb = new StringBuffer (); 161 String delim = System.getProperty("path.separator"); 162 for (int i = 0; i < jars.size(); i++) { 163 Directory d = (Directory)jars.elementAt(i); 164 if (i > 0) sb.append(delim); 165 sb.append(d.getRootPath()); 166 } 167 return sb.toString(); 168 } 169 170 final void init() throws IOException { 171 Enumeration e = root.entries(); 172 Directory classes = null; 173 File tmpClasses = null; 174 while (e.hasMoreElements()) { 175 String path = e.nextElement().toString(); 176 if (path.startsWith("WEB-INF/lib/") && path.endsWith(".jar")) { 177 Directory d = null; 178 String realPath = root.getRealPath(path); 179 if (realPath == null) { 180 Entry entry = root.getEntry(path); 181 InputStream is = entry.getInputStream(); 182 File out; 183 try { 184 out = new File (tmpDir, 185 "tmp" + (tmpCount++) + ".jar"); 186 FileOutputStream os = new FileOutputStream (out); 187 try { 188 IO.copyStream(is, os); 189 } finally { 190 os.close(); 191 } 192 } finally { 193 is.close(); 194 } 195 d = Directory.getDirectory(out); 196 } else { 197 d = Directory.getDirectory(new File (realPath)); 198 } 199 jars.addElement(d); 200 } else if (path.startsWith("WEB-INF/classes")) { 201 if (classes == null) { 202 String realPath = root.getRealPath("WEB-INF/classes"); 203 if (realPath == null) { 204 tmpClasses = new File (tmpDir, "classes"); 205 tmpClasses.mkdirs(); 206 classes = Directory.getDirectory(tmpClasses); 207 } else { 208 classes = Directory.getDirectory(new File (realPath)); 209 } 210 jars.addElement(classes); 211 } 212 if (tmpClasses != null) { 213 String subPath = 214 path.substring("WEB-INF/classes/".length()); 215 if (subPath.length() > 0) { 216 subPath = subPath.replace('/', File.separatorChar); 217 Entry entry = root.getEntry(path); 218 File out = new File (tmpClasses, subPath); 219 if (entry.isDirectory()) { 220 out.mkdir(); 221 } else { 222 InputStream is = entry.getInputStream(); 223 try { 224 FileOutputStream os = 225 new FileOutputStream (out); 226 try { 227 IO.copyStream(is, os); 228 } finally { 229 os.close(); 230 } 231 } finally { 232 is.close(); 233 } 234 } 235 } 236 } 237 } 238 } 239 } 240 } 241 242 | Popular Tags |