1 45 package org.openejb.util; 46 47 import java.io.BufferedInputStream ; 48 import java.io.ByteArrayInputStream ; 49 import java.io.ByteArrayOutputStream ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.net.URL ; 53 import java.util.Enumeration ; 54 import java.util.HashMap ; 55 import java.util.jar.JarEntry ; 56 import java.util.jar.JarFile ; 57 import java.util.jar.JarInputStream ; 58 59 76 public class MemoryClassLoader extends ClassLoader { 77 private final static int BUFFER_SIZE = 1024; 78 private HashMap classes = new HashMap (); 79 private HashMap others = new HashMap (); 80 81 public MemoryClassLoader(ClassLoader parent, JarFile file) { 82 this(parent, new JarFile []{file}); 83 } 84 85 public MemoryClassLoader(ClassLoader parent, JarFile [] file) { 86 super(parent); 87 for(int i=0; i<file.length; i++) { 88 addJar(file[i]); 89 try { 90 file[i].close(); 91 } catch(IOException e) {} 92 } 93 } 94 95 99 public MemoryClassLoader(ClassLoader parent, JarInputStream stream) { 100 this(parent, new JarInputStream []{stream}); 101 } 102 106 public MemoryClassLoader(ClassLoader parent, JarInputStream [] stream) { 107 super(parent); 108 for(int i=0; i<stream.length; i++) { 109 addJar(stream[i]); 110 } 111 } 112 113 114 public InputStream getResourceAsStream(String name) { 115 InputStream stream = getParent().getResourceAsStream(name); 116 if(stream == null) { 117 byte[] buf = (byte[])others.get(name); 118 if(buf != null) { 119 stream = new ByteArrayInputStream (buf); 120 } 121 } 122 return stream; 123 } 124 125 public URL getResource(String name) { 126 throw new Error ("Not Yet Implemented!"); 127 } 132 133 protected Enumeration findResources(String name) throws IOException { 134 throw new Error ("Not Yet Implemented!"); 135 } 137 138 public boolean equals(Object o) { 139 if(o instanceof MemoryClassLoader) { 140 return ((MemoryClassLoader)o).getParent() == getParent(); 141 } 142 return false; 143 } 144 145 public int hashCode() { 146 return getParent().hashCode(); 147 } 148 149 public Class findClass(String name) throws ClassNotFoundException { 150 byte[] data = findClassData(name); 151 if(data != null) { 152 return defineClass(name, data, 0, data.length); 153 } else { 154 throw new ClassNotFoundException (); 155 } 156 } 157 158 159 160 163 public void addJar(JarFile jar) { 164 Enumeration entries = jar.entries(); 165 while(entries.hasMoreElements()) { 166 JarEntry entry = (JarEntry )entries.nextElement(); 167 if(entry.getName().endsWith(".class")) { 168 try { 169 addClassFile(jar, entry); 170 } catch(IOException e) {e.printStackTrace();} 171 } else { 172 try { 173 addOtherFile(jar, entry); 174 } catch(IOException e) {e.printStackTrace();} 175 } 176 } 177 } 178 179 182 public void addJar(JarInputStream stream) { 183 byte[] buf = new byte[BUFFER_SIZE]; 184 int count; 185 try { 186 while(true) { 187 JarEntry entry = stream.getNextJarEntry(); 188 if(entry == null) 189 break; 190 String name = entry.getName(); 191 int size = (int)entry.getSize(); 192 ByteArrayOutputStream out = 193 size >= 0 ? new ByteArrayOutputStream (size) 194 : new ByteArrayOutputStream (BUFFER_SIZE); 195 while((count = stream.read(buf)) > -1) 196 out.write(buf, 0, count); 197 out.close(); 198 if(name.endsWith(".class")) { 199 classes.put(getClassName(name), out.toByteArray()); 200 } else { 201 others.put(name, out.toByteArray()); 202 } 203 } 204 } catch(IOException e) { 205 e.printStackTrace(); 206 } 207 } 208 209 private byte[] findClassData(String name) { 210 return (byte[])classes.remove(name); 211 } 212 213 private void addClassFile(JarFile jar, JarEntry entry) throws IOException { 214 classes.put(getClassName(entry.getName()), getFileBytes(jar, entry)); 215 } 216 217 private void addOtherFile(JarFile jar, JarEntry entry) throws IOException { 218 others.put(entry.getName(), getFileBytes(jar, entry)); 219 } 220 221 private static String getClassName(String fileName) { 222 return fileName.substring(0, fileName.length()-6).replace('/','.'); 223 } 224 225 private static byte[] getFileBytes(JarFile jar, JarEntry entry) throws IOException { 226 ByteArrayOutputStream stream = new ByteArrayOutputStream ((int)entry.getSize()); 227 byte[] buf = new byte[BUFFER_SIZE]; 228 BufferedInputStream in = new BufferedInputStream (jar.getInputStream(entry)); 229 int count; 230 while((count = in.read(buf)) > -1) 231 stream.write(buf, 0, count); 232 in.close(); 233 stream.close(); 234 235 return stream.toByteArray(); 236 } 237 } | Popular Tags |