KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > VAClassLoader


1 /**
2  * $RCSfile: VAClassLoader.java,v $
3  * @creation 01/02/00
4  * @modification $Date: 2005/03/06 23:04:17 $
5  */

6
7 package com.memoire.vainstall;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.jar.JarInputStream JavaDoc;
18 import java.util.zip.ZipEntry JavaDoc;
19
20 /**
21  * @version $Id: VAClassLoader.java,v 1.4 2005/03/06 23:04:17 deniger Exp $
22  * @author Axel von Arnim
23  */

24
25 public class VAClassLoader extends ClassLoader JavaDoc
26 {
27   public final static boolean DEBUG="yes".equals(System.getProperty("DEBUG"));
28
29   /**
30    * format ex. ("com.ice.util.UserProperties.class",byte[] data)
31    */

32   private Hashtable JavaDoc cache_;
33
34   private long offset_;
35   private File JavaDoc jarfile_;
36   private File JavaDoc dllFile_;
37
38
39   /**
40    * format ex. ("com.ice.util.UserProperties",Class)
41    */

42   private Hashtable JavaDoc classes = new Hashtable JavaDoc();
43
44   public VAClassLoader(File JavaDoc jarfile, Long JavaDoc offset)
45   {
46     super();
47     offset_=offset.longValue();
48     jarfile_=jarfile;
49     JarInputStream JavaDoc jar=null;
50     try {
51       cache_=new Hashtable JavaDoc();
52       printDebug("VAClassLoader: loading classes from "+jarfile.getName()+" (offset "+offset_+")...");
53       FileInputStream JavaDoc stream=new FileInputStream JavaDoc(jarfile_);
54       stream.skip(offset_);
55       jar=new JarInputStream JavaDoc(stream);
56       ZipEntry JavaDoc entry=jar.getNextEntry();
57       byte[] data=null;
58       Class JavaDoc c=null;
59       while( entry!=null ) {
60         String JavaDoc entryName=entry.getName();
61         if( entryName.endsWith(".class") ) {
62
63           byte[] buffer = new byte[2048];
64           ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc className=entryName.replace('/', '.'); // .substring(0, entryName.lastIndexOf('.'));
75
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 JavaDoc t ) {
85       t.printStackTrace();
86     } finally {
87       printDebug(" closing jarFile.");
88       if( jar!=null ) try {jar.close();} catch( IOException JavaDoc e ) {System.err.println(e);}
89     }
90   }
91
92
93 public Class JavaDoc loadClass(String JavaDoc className)
94        throws ClassNotFoundException JavaDoc
95 {
96   return (loadClass(className, true));
97 }
98
99 public synchronized Class JavaDoc loadClass(String JavaDoc className,boolean resolveIt)
100        throws ClassNotFoundException JavaDoc
101 {
102
103     Class JavaDoc result;
104     byte[] classBytes;
105
106     //----- Check our local cache of classes
107
result = (Class JavaDoc)classes.get(className);
108     if (result != null) {
109         return result;
110     }
111
112     //----- Check with the primordial class loader
113
try {
114         result = super.findSystemClass(className);
115         return result;
116     } catch (ClassNotFoundException JavaDoc e) {
117     }
118
119     //----- Try to load it from preferred source
120
// Note loadClassBytes() is an abstract method
121
printDebug(className);
122     classBytes = loadClassBytes(className);
123     if (classBytes == null) {
124         throw new ClassNotFoundException JavaDoc();
125     }
126
127     //----- Define it (parse the class file)
128
result = defineClass(className, classBytes, 0, classBytes.length);
129     if (result == null) {
130         throw new ClassFormatError JavaDoc();
131     }
132
133     //----- Resolve if necessary
134
if (resolveIt) resolveClass(result);
135
136     // Done
137
classes.put(className, result);
138     return result;
139 }
140
141
142   public synchronized InputStream JavaDoc getResourceAsStream(String JavaDoc name)
143   {
144     InputStream JavaDoc res=null;
145     JarInputStream JavaDoc jar=null;
146     try {
147       printDebug("VAClassLoader: loading resource "+name);
148       FileInputStream JavaDoc stream=new FileInputStream JavaDoc(jarfile_);
149       stream.skip(offset_);
150       jar=new JarInputStream JavaDoc(stream);
151       ZipEntry JavaDoc 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 JavaDoc t ) {
159       System.err.println(t);
160       printDebug(" closing jarFile.");
161       if( jar!=null ) try {jar.close();} catch( IOException JavaDoc 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 JavaDoc getResource(String JavaDoc name)
169   {
170     URL JavaDoc res=null;
171     try {
172       printDebug("VAClassLoader: loading resource URL "+name);
173       String JavaDoc jarUrl=jarfile_.toURL().toString();
174       jarUrl="jar:"+jarUrl+"!"+name;
175       res=new URL JavaDoc(jarUrl);
176     } catch( MalformedURLException JavaDoc 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 JavaDoc msg)
191   {
192     if(DEBUG) System.err.println(msg);
193   }
194
195 protected byte[] loadClassBytes (String JavaDoc className)
196 {
197   // Support the MultiClassLoader's class name munging facility.
198
className = formatClassName(className);
199         printDebug("Trying to fetch="+className);
200   // Attempt to get the class data from the JarResource.
201
return (byte[])cache_.get(className);
202 }
203
204 protected String JavaDoc formatClassName(String JavaDoc className)
205 {
206   return className + ".class";
207 }
208
209 }
210
Popular Tags