1 6 7 package com.memoire.vainstall; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.Hashtable ; 17 import java.util.jar.JarInputStream ; 18 import java.util.zip.ZipEntry ; 19 20 24 25 public class VAClassLoader extends ClassLoader  26 { 27 public final static boolean DEBUG="yes".equals(System.getProperty("DEBUG")); 28 29 32 private Hashtable cache_; 33 34 private long offset_; 35 private File jarfile_; 36 private File dllFile_; 37 38 39 42 private Hashtable classes = new Hashtable (); 43 44 public VAClassLoader(File jarfile, Long offset) 45 { 46 super(); 47 offset_=offset.longValue(); 48 jarfile_=jarfile; 49 JarInputStream jar=null; 50 try { 51 cache_=new Hashtable (); 52 printDebug("VAClassLoader: loading classes from "+jarfile.getName()+" (offset "+offset_+")..."); 53 FileInputStream stream=new FileInputStream (jarfile_); 54 stream.skip(offset_); 55 jar=new JarInputStream (stream); 56 ZipEntry entry=jar.getNextEntry(); 57 byte[] data=null; 58 Class c=null; 59 while( entry!=null ) { 60 String entryName=entry.getName(); 61 if( entryName.endsWith(".class") ) { 62 63 byte[] buffer = new byte[2048]; 64 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 65 while(true) 66 { 67 int read = jar.read(buffer); 68 if(read == -1)break; 69 bos.write(buffer, 0, read); 70 } 71 data = bos.toByteArray(); 72 bos.close(); 73 jar.closeEntry(); 74 String className=entryName.replace('/', '.'); printDebug(" className="+className+" size="+data.length); 76 77 byte[] toCache = new byte[data.length]; 78 System.arraycopy(data,0,toCache,0,data.length); 79 cache_.put(className, data); 80 81 } 82 entry=jar.getNextEntry(); 83 } 84 } catch( Throwable t ) { 85 t.printStackTrace(); 86 } finally { 87 printDebug(" closing jarFile."); 88 if( jar!=null ) try {jar.close();} catch( IOException e ) {System.err.println(e);} 89 } 90 } 91 92 93 public Class loadClass(String className) 94 throws ClassNotFoundException  95 { 96 return (loadClass(className, true)); 97 } 98 99 public synchronized Class loadClass(String className,boolean resolveIt) 100 throws ClassNotFoundException  101 { 102 103 Class result; 104 byte[] classBytes; 105 106 result = (Class )classes.get(className); 108 if (result != null) { 109 return result; 110 } 111 112 try { 114 result = super.findSystemClass(className); 115 return result; 116 } catch (ClassNotFoundException e) { 117 } 118 119 printDebug(className); 122 classBytes = loadClassBytes(className); 123 if (classBytes == null) { 124 throw new ClassNotFoundException (); 125 } 126 127 result = defineClass(className, classBytes, 0, classBytes.length); 129 if (result == null) { 130 throw new ClassFormatError (); 131 } 132 133 if (resolveIt) resolveClass(result); 135 136 classes.put(className, result); 138 return result; 139 } 140 141 142 public synchronized InputStream getResourceAsStream(String name) 143 { 144 InputStream res=null; 145 JarInputStream jar=null; 146 try { 147 printDebug("VAClassLoader: loading resource "+name); 148 FileInputStream stream=new FileInputStream (jarfile_); 149 stream.skip(offset_); 150 jar=new JarInputStream (stream); 151 ZipEntry entry=jar.getNextEntry(); 152 while( (entry!=null)&& 153 (!entry.getName().equals(name)) ) 154 entry=jar.getNextEntry(); 155 if( entry!=null ) { 156 res=jar; 157 } 158 } catch( IOException t ) { 159 System.err.println(t); 160 printDebug(" closing jarFile."); 161 if( jar!=null ) try {jar.close();} catch( IOException e ) {System.err.println(e);} 162 } 163 if( res==null ) printDebug(" not found"); 164 else printDebug(" OK"); 165 return res; 166 } 167 168 public synchronized URL getResource(String name) 169 { 170 URL res=null; 171 try { 172 printDebug("VAClassLoader: loading resource URL "+name); 173 String jarUrl=jarfile_.toURL().toString(); 174 jarUrl="jar:"+jarUrl+"!"+name; 175 res=new URL (jarUrl); 176 } catch( MalformedURLException ex ) { 177 printDebug(" null URL"); 178 res=null; 179 } 180 if( res==null ) printDebug(" not found"); 181 else printDebug(" OK"); 182 return res; 183 } 184 185 protected void finalize() 186 { 187 if( dllFile_.exists() ) dllFile_.delete(); 188 } 189 190 public static void printDebug(String msg) 191 { 192 if(DEBUG) System.err.println(msg); 193 } 194 195 protected byte[] loadClassBytes (String className) 196 { 197 className = formatClassName(className); 199 printDebug("Trying to fetch="+className); 200 return (byte[])cache_.get(className); 202 } 203 204 protected String formatClassName(String className) 205 { 206 return className + ".class"; 207 } 208 209 } 210
| Popular Tags
|