View Javadoc

1   // Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
2   // Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
3   // Decompiler options: packimports(3) 
4   // Source File Name:   PureClassLoader.java
5   
6   package salto.tool.sql;
7   
8   import java.io.File;
9   import java.io.FileInputStream;
10  import java.io.FileNotFoundException;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.net.MalformedURLException;
14  import java.net.URL;
15  import java.util.Enumeration;
16  import java.util.Hashtable;
17  import java.util.Vector;
18  import java.util.zip.ZipEntry;
19  import java.util.zip.ZipException;
20  import java.util.zip.ZipFile;
21  
22  public class PureClassLoader extends ClassLoader
23  {
24      private static class ClassCacheEntry
25      {
26  
27          public boolean isSystemClass()
28          {
29              return origin == null;
30          }
31  
32          Class loadedClass;
33          File origin;
34          long lastModified;
35  
36          ClassCacheEntry()
37          {
38          }
39      }
40  
41  
42      public PureClassLoader(Vector vector)
43          throws IllegalArgumentException
44      {
45          cache = new Hashtable();
46          if(vector != null)
47          {
48              for(Enumeration enumeration = vector.elements(); enumeration.hasMoreElements();)
49              {
50                  Object obj = enumeration.nextElement();
51                  File file;
52                  try
53                  {
54                      file = (File)obj;
55                  }
56                  catch(ClassCastException _ex)
57                  {
58                      throw new IllegalArgumentException("Object " + obj + "is not a valid \"File\" instance");
59                  }
60                  if(!file.exists())
61                      throw new IllegalArgumentException("Repository " + file.getAbsolutePath() + " doesn't exist!");
62                  if(!file.canRead())
63                      throw new IllegalArgumentException("Don't have read access for file " + file.getAbsolutePath());
64                  if(!file.isDirectory() && !isZipOrJarArchive(file))
65                      throw new IllegalArgumentException(file.getAbsolutePath() + " is not a directory or zip/jar file" + " or if it's a zip/jar file then it is corrupted.");
66              }
67  
68          }
69          repository = vector;
70          generation = generationCounter++;
71      }
72  
73      public URL getResource(String s)
74      {
75          URL url = ClassLoader.getSystemResource(s);
76          if(url != null)
77              return url;
78          for(Enumeration enumeration = repository.elements(); enumeration.hasMoreElements();)
79          {
80              File file = (File)enumeration.nextElement();
81              if(file.isDirectory())
82              {
83                  String s1 = s.replace('/', File.separatorChar);
84                  File file1 = new File(file, s1);
85                  if(file1.exists())
86                  {
87                      try
88                      {
89                          return new URL("file://" + file1.getAbsolutePath());
90                      }
91                      catch(MalformedURLException malformedurlexception)
92                      {
93                          malformedurlexception.printStackTrace();
94                      }
95                      return null;
96                  }
97              }
98          }
99  
100         return null;
101     }
102 
103     public InputStream getResourceAsStream(String s)
104     {
105         InputStream inputstream = ClassLoader.getSystemResourceAsStream(s);
106         if(inputstream == null)
107         {
108             for(Enumeration enumeration = repository.elements(); enumeration.hasMoreElements();)
109             {
110                 File file = (File)enumeration.nextElement();
111                 if(file.isDirectory())
112                     inputstream = loadResourceFromDirectory(file, s);
113                 else
114                 if(s.endsWith(".initArgs"))
115                 {
116                     File file1 = new File(file.getParent());
117                     inputstream = loadResourceFromDirectory(file1, s);
118                 } else
119                 {
120                     inputstream = loadResourceFromZipfile(file, s);
121                 }
122                 if(inputstream != null)
123                     break;
124             }
125 
126         }
127         return inputstream;
128     }
129 
130     private boolean isZipOrJarArchive(File file)
131     {
132         boolean flag = true;
133         ZipFile zipfile = null;
134         try
135         {
136             zipfile = new ZipFile(file);
137         }
138         catch(ZipException _ex)
139         {
140             flag = false;
141         }
142         catch(IOException _ex)
143         {
144             flag = false;
145         }
146         finally
147         {
148             if(zipfile != null)
149                 try
150                 {
151                     zipfile.close();
152                 }
153                 catch(IOException _ex) { }
154         }
155         return flag;
156     }
157 
158     private byte[] loadBytesFromStream(InputStream inputstream, int i)
159         throws IOException
160     {
161         byte abyte0[] = new byte[i];
162         int k = 0;
163         int j;
164         for(; i > 0 && (j = inputstream.read(abyte0, k, i)) != -1; i -= j)
165             k += j;
166 
167         return abyte0;
168     }
169 
170     protected synchronized Class loadClass(String s, boolean flag)
171         throws ClassNotFoundException
172     {
173         Class class1 = null;
174         ClassCacheEntry classcacheentry = (ClassCacheEntry)cache.get(s);
175         if(classcacheentry != null)
176         {
177             class1 = classcacheentry.loadedClass;
178             if(flag)
179                 resolveClass(class1);
180             return class1;
181         }
182         if(!securityAllowsClass(s))
183             return loadSystemClass(s, flag);
184         Enumeration enumeration = repository.elements();
185         ClassCacheEntry classcacheentry1 = new ClassCacheEntry();
186         while(enumeration.hasMoreElements()) 
187         {
188             File file = (File)enumeration.nextElement();
189             byte abyte0[];
190             try
191             {
192                 if(file.isDirectory())
193                     abyte0 = loadClassFromDirectory(file, s, classcacheentry1);
194                 else
195                     abyte0 = loadClassFromZipfile(file, s, classcacheentry1);
196             }
197             catch(IOException _ex)
198             {
199                 abyte0 = null;
200             }
201             if(abyte0 != null)
202             {
203                 class1 = defineClass(s, abyte0, 0, abyte0.length);
204                 classcacheentry1.loadedClass = class1;
205                 classcacheentry1.lastModified = classcacheentry1.origin.lastModified();
206                 cache.put(s, classcacheentry1);
207                 if(flag)
208                     resolveClass(class1);
209                 return class1;
210             }
211         }
212         try
213         {
214             class1 = loadSystemClass(s, flag);
215             if(class1 != null)
216             {
217                 if(flag)
218                     resolveClass(class1);
219                 return class1;
220             }
221         }
222         catch(Exception _ex)
223         {
224             class1 = null;
225         }
226         throw new ClassNotFoundException(s);
227     }
228 
229     private byte[] loadClassFromDirectory(File file, String s, ClassCacheEntry classcacheentry)
230         throws IOException
231     {
232         String s1 = s.replace('.', File.separatorChar) + ".class";
233         if(!Character.isJavaIdentifierStart(s1.charAt(0)))
234         {
235             int i;
236             for(i = 1; !Character.isJavaIdentifierStart(s1.charAt(i++)););
237             s1 = s1.substring(i);
238         }
239         File file1 = new File(file, s1);
240         if(file1.exists())
241         {
242             classcacheentry.origin = file1;
243             FileInputStream fileinputstream = new FileInputStream(file1);
244             try
245             {
246                 byte abyte0[] = loadBytesFromStream(fileinputstream, (int)file1.length());
247                 return abyte0;
248             }
249             finally
250             {
251                 fileinputstream.close();
252             }
253         } else
254         {
255             return null;
256         }
257     }
258 
259     private byte[] loadClassFromZipfile(File file, String s, ClassCacheEntry classcacheentry)
260         throws IOException
261     {
262         String s1 = s.replace('.', '/') + ".class";
263         ZipFile zipfile = new ZipFile(file);
264         try
265         {
266             ZipEntry zipentry = zipfile.getEntry(s1);
267             if(zipentry != null)
268             {
269                 classcacheentry.origin = file;
270                 byte abyte0[] = loadBytesFromStream(zipfile.getInputStream(zipentry), (int)zipentry.getSize());
271                 return abyte0;
272             }
273             byte abyte1[] = null;
274             return abyte1;
275         }
276         finally
277         {
278             zipfile.close();
279         }
280     }
281 
282     private InputStream loadResourceFromDirectory(File file, String s)
283     {
284         String s1 = s.replace('/', File.separatorChar);
285         File file1 = new File(file, s1);
286         if(file1.exists())
287             try
288             {
289                 return new FileInputStream(file1);
290             }
291             catch(FileNotFoundException _ex)
292             {
293                 return null;
294             }
295         else
296             return null;
297     }
298 
299     private InputStream loadResourceFromZipfile(File file, String s)
300     {
301         try
302         {
303             ZipFile zipfile = new ZipFile(file);
304             ZipEntry zipentry = zipfile.getEntry(s);
305             if(zipentry != null)
306                 return zipfile.getInputStream(zipentry);
307             else
308                 return null;
309         }
310         catch(IOException _ex)
311         {
312             return null;
313         }
314     }
315 
316     private Class loadSystemClass(String s, boolean flag)
317         throws NoClassDefFoundError, ClassNotFoundException
318     {
319         Class class1 = findSystemClass(s);
320         ClassCacheEntry classcacheentry = new ClassCacheEntry();
321         classcacheentry.origin = null;
322         classcacheentry.loadedClass = class1;
323         classcacheentry.lastModified = 0x7fffffffffffffffL;
324         cache.put(s, classcacheentry);
325         if(flag)
326             resolveClass(class1);
327         return class1;
328     }
329 
330     public PureClassLoader reinstantiate()
331     {
332         return new PureClassLoader(repository);
333     }
334 
335     private boolean securityAllowsClass(String s)
336     {
337         try
338         {
339             SecurityManager securitymanager = System.getSecurityManager();
340             if(securitymanager == null)
341             {
342                 return true;
343             } else
344             {
345                 int i = s.lastIndexOf(46);
346                 securitymanager.checkPackageDefinition(i <= -1 ? "" : s.substring(0, i));
347                 return true;
348             }
349         }
350         catch(SecurityException _ex)
351         {
352             return false;
353         }
354     }
355 
356     public synchronized boolean shouldReload()
357     {
358         for(Enumeration enumeration = cache.elements(); enumeration.hasMoreElements();)
359         {
360             ClassCacheEntry classcacheentry = (ClassCacheEntry)enumeration.nextElement();
361             if(!classcacheentry.isSystemClass())
362             {
363                 long l = classcacheentry.origin.lastModified();
364                 if(l == 0L)
365                     return true;
366                 if(l != classcacheentry.lastModified)
367                     return true;
368             }
369         }
370 
371         return false;
372     }
373 
374     public synchronized boolean shouldReload(String s)
375     {
376         ClassCacheEntry classcacheentry = (ClassCacheEntry)cache.get(s);
377         if(classcacheentry == null)
378             return false;
379         if(classcacheentry.isSystemClass())
380         {
381             return false;
382         } else
383         {
384             boolean flag = classcacheentry.origin.lastModified() != classcacheentry.lastModified;
385             return flag;
386         }
387     }
388 
389     private static int generationCounter = 0;
390     private int generation;
391     private Hashtable cache;
392     private Vector repository;
393 
394 }