1 package xdoclet.junit; 2 3 17 18 import java.io.*; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.StringTokenizer ; 22 import java.util.zip.ZipEntry ; 23 import java.util.zip.ZipFile ; 24 25 30 public class XDocletClassLoader extends ClassLoader 31 { 32 private ArrayList _classPath = new ArrayList (); 33 34 public XDocletClassLoader(String classPath) 35 { 36 super(); 37 setClassPath(classPath); 38 } 39 40 public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException 41 { 42 Class result = null; 43 44 if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("org.") || name.startsWith("sun.")) 45 { 46 try 47 { 48 result = findSystemClass(name); 49 } 50 catch (ClassNotFoundException ex) 51 {} 52 } 53 if (result == null) 54 { 55 result = findLoadedClass(name); 56 } 57 if (result == null) 58 { 59 byte[] data = loadClassData(name); 60 61 if (data != null) 62 { 63 result = defineClass(name, data, 0, data.length); 64 } 65 } 66 if (result == null) 67 { 68 throw new ClassNotFoundException (name); 69 } 70 if (resolve) 71 { 72 resolveClass(result); 73 } 74 return result; 75 } 76 77 private void setClassPath(String classPath) 78 { 79 StringTokenizer tokenizer = new StringTokenizer (classPath, System.getProperty("path.separator")); 80 81 _classPath.clear(); 82 while (tokenizer.hasMoreTokens()) 83 { 84 _classPath.add(tokenizer.nextToken()); 85 } 86 } 87 88 private byte[] loadClassData(String className) throws ClassNotFoundException 89 { 90 byte[] data = null; 91 String path = null; 92 String fileName = className.replace('.', '/') + ".class"; 93 94 for (Iterator it = _classPath.iterator(); it.hasNext();) 95 { 96 path = (String )it.next(); 97 if (path.toLowerCase().endsWith(".jar") || path.toLowerCase().endsWith(".zip")) 98 { 99 data = loadJar(path, fileName); 100 } 101 else 102 { 103 data = loadFile(path, fileName); 104 } 105 if (data != null) 106 { 107 return data; 108 } 109 } 110 throw new ClassNotFoundException (className); 111 } 112 113 private byte[] loadFile(String path, String fileName) 114 { 115 File file = new File (path, fileName); 116 117 if (file.exists()) 118 { 119 try 120 { 121 FileInputStream input = new FileInputStream(file); 122 ByteArrayOutputStream output = new ByteArrayOutputStream(1000); 123 byte[] data = new byte[1000]; 124 int numRead; 125 126 while ((numRead = input.read(data)) != -1) 127 { 128 output.write(data, 0, numRead); 129 } 130 input.close(); 131 output.close(); 132 return output.toByteArray(); 133 } 134 catch (IOException ex) 135 {} 136 } 137 return null; 138 } 139 140 private byte[] loadJar(String path, String fileName) 141 { 142 File file = new File (path); 143 144 if (!file.exists()) 145 { 146 return null; 147 } 148 149 ZipFile zipFile = null; 150 151 try 152 { 153 zipFile = new ZipFile (file); 154 } 155 catch(IOException ex) 156 { 157 return null; 158 } 159 160 ZipEntry entry = zipFile.getEntry(fileName); 161 162 if (entry == null) 163 { 164 return null; 165 } 166 167 InputStream input = null; 168 int size = (int)entry.getSize(); 169 byte[] data = new byte[size]; 170 int numRead; 171 172 try 173 { 174 input = zipFile.getInputStream(entry); 175 176 for (int pos = 0; pos < size; pos += numRead) 177 { 178 numRead = input.read(data, pos, data.length - pos); 179 } 180 zipFile.close(); 181 return data; 182 } 183 catch (IOException ex) 184 {} 185 finally 186 { 187 try 188 { 189 if (input != null) 190 { 191 input.close(); 192 } 193 } 194 catch (IOException ex) 195 {} 196 } 197 return null; 198 } 199 } 200 | Popular Tags |