1 33 34 package edu.rice.cs.drjava.model.junit; 35 36 import junit.runner.*; 37 import java.util.*; 38 import java.io.*; 39 import java.net.*; 40 import java.util.zip.*; 41 42 import edu.rice.cs.util.FileOps; 43 44 56 57 58 public final class DrJavaTestCaseClassLoader extends TestCaseClassLoader { 59 60 ClassLoader _loader; 61 62 63 private Vector<String > fPathItems; 64 65 66 private String [] defaultExclusions= {"junit.framework.", "junit.extensions.", "junit.runner.", "java."}; 67 68 69 static final String EXCLUDED_FILE= "excluded.properties"; 70 71 72 private Vector<String > fExcluded; 73 74 75 public DrJavaTestCaseClassLoader() { this(System.getProperty("java.class.path")); } 76 77 78 public DrJavaTestCaseClassLoader(String classPath) { 79 _loader = getClass().getClassLoader(); 80 scanPath(classPath); 81 readExcludedPackages(); 82 } 83 84 87 private void scanPath(String classPath) { 88 String separator= System.getProperty("path.separator"); 89 fPathItems= new Vector<String >(10); 90 StringTokenizer st= new StringTokenizer(classPath, separator); 91 String item; 92 while (st.hasMoreTokens()) { 93 item = st.nextToken(); 94 fPathItems.addElement(item); 95 try{ _loader = new DrJavaURLClassLoader(new URL[]{FileOps.toURL(new File(item))}, _loader); } 96 catch(MalformedURLException e) { 97 98 } 99 } 100 } 101 102 103 public URL getResource(String name) { return _loader.getResource(name); } 104 105 106 public InputStream getResourceAsStream(String name) { 107 return _loader.getResourceAsStream(name); 108 } 109 110 113 public boolean isExcluded(String name) { 114 for (int i= 0; i < fExcluded.size(); i++) { 115 if (name.startsWith(fExcluded.elementAt(i))) return true; 116 } 117 return false; 118 } 119 120 123 public synchronized Class <?> loadClass(String name, boolean resolve) 124 throws ClassNotFoundException { 125 Class <?> c= findLoadedClass(name); 126 if (c != null) 127 return c; 128 if (isExcluded(name)) { 133 try { 134 c= findSystemClass(name); 135 return c; 136 } catch (ClassNotFoundException e) { 137 } 139 } 140 141 try{ 142 if (c == null) { 143 byte[] data= lookupClassData(name); 144 if (data == null) { 145 throw new ClassNotFoundException (); 146 } 147 c= defineClass(name, data, 0, data.length); 148 } 149 if (resolve) 150 resolveClass(c); 151 }catch(ClassNotFoundException e) { 152 return findSystemClass(name); 154 } 155 return c; 156 } 157 158 159 private byte[] lookupClassData(String className) throws ClassNotFoundException { 160 byte[] data= null; 161 for (int i= 0; i < fPathItems.size(); i++) { 162 String path= fPathItems.elementAt(i); 163 String fileName= className.replace('.', '/')+".class"; 164 if (isJar(path)) { 165 data= loadJarData(path, fileName); 166 } else { 167 data= loadFileData(path, fileName); 168 } 169 if (data != null) { 170 return data; 171 } 172 } 173 throw new ClassNotFoundException (className); 174 } 175 176 179 private boolean isJar(String pathEntry) { 180 return pathEntry.endsWith(".jar") || pathEntry.endsWith(".zip"); 181 } 182 183 186 private byte[] loadFileData(String path, String fileName) { 187 File file= new File(path, fileName); 188 if (file.exists()) { 189 return getClassData(file); 190 } 191 return null; 192 } 193 194 197 private byte[] getClassData(File f) { 198 try { 199 FileInputStream stream= new FileInputStream(f); 200 ByteArrayOutputStream out= new ByteArrayOutputStream(1000); 201 byte[] b= new byte[1000]; 202 int n; 203 while ((n= stream.read(b)) != -1) 204 out.write(b, 0, n); 205 stream.close(); 206 out.close(); 207 return out.toByteArray(); 208 209 } catch (IOException e) { 210 } 211 return null; 212 } 213 214 218 private byte[] loadJarData(String path, String fileName) { 219 ZipFile zipFile= null; 220 InputStream stream= null; 221 File archive= new File(path); 222 if (!archive.exists()) 223 return null; 224 try { 225 zipFile= new ZipFile(archive); 226 } catch(IOException io) { 227 return null; 228 } 229 ZipEntry entry= zipFile.getEntry(fileName); 230 if (entry == null) 231 return null; 232 int size= (int) entry.getSize(); 233 try { 234 stream= zipFile.getInputStream(entry); 235 byte[] data= new byte[size]; 236 int pos= 0; 237 while (pos < size) { 238 int n= stream.read(data, pos, data.length - pos); 239 pos += n; 240 } 241 zipFile.close(); 242 return data; 243 } catch (IOException e) { 244 } finally { 245 try { 246 if (stream != null) 247 stream.close(); 248 } catch (IOException e) { 249 } 250 } 251 return null; 252 } 253 254 257 private void readExcludedPackages() { 258 fExcluded = new Vector<String >(10); 259 for (String de: defaultExclusions) fExcluded.addElement(de); 260 261 InputStream is = getClass().getResourceAsStream(EXCLUDED_FILE); 262 if (is == null) return; 263 Properties p = new Properties(); 264 try { p.load(is); } 265 catch (IOException e) { return; } 266 finally { 267 try { is.close(); } 268 catch (IOException e) { 269 270 } 271 } 272 273 Enumeration pnames = p.propertyNames(); 275 276 while (pnames.hasMoreElements()) { 277 String key = (String ) pnames.nextElement(); 278 if (key.startsWith("excluded.")) { 279 String path = p.getProperty(key); 280 path = path.trim(); 281 if (path.endsWith("*")) path= path.substring(0, path.length()-1); 282 if (path.length() > 0) fExcluded.addElement(path); 283 } 284 } 285 } 286 287 290 private static class DrJavaURLClassLoader extends URLClassLoader{ 291 292 public DrJavaURLClassLoader(URL[] urls, ClassLoader c) { super(urls, c); } 293 294 public URL getResource(String name) { 295 URL ret = getParent().getResource(name); 296 if (ret == null) ret = super.getResource(name); 297 return ret; 298 } 299 } 300 } | Popular Tags |