/Users/lyon/j4p/src/net/compute/LocalClassLoader.java

1    package net.compute; 
2     
3    import classUtils.dumper.ClassFile; 
4     
5    import java.io.File; 
6    import java.io.FileInputStream; 
7    import java.io.FileNotFoundException; 
8    import java.io.IOException; 
9     
10   /** 
11    * Created by 
12    * User: lyon 
13    * Date: Aug 19, 2003 
14    * Time: 9:22:07 AM 
15    * 
16    */ 
17    
18   /** 
19    * Defines a Class Loader that knows how to read a class 
20    * from the local file system. 
21    */ 
22   public abstract class LocalClassLoader extends java.lang.ClassLoader { 
23       private String directory; 
24    
25       public LocalClassLoader(String dir) { 
26           directory = dir; 
27       } 
28    
29       private LocalClassLoader() { 
30       } 
31    
32       public Class loadClassFromFile(String className, boolean resolve) 
33               throws ClassNotFoundException, FileNotFoundException { 
34           File target = 
35                   new File(directory 
36                   + className.replace('.', '/') 
37                   + ".class"); 
38           if (!target.exists()) throw new java.io.FileNotFoundException(); 
39           try { 
40               return 
41                       loadClassFromFile(target, 
42                               className, 
43                               resolve); 
44           } catch (IOException e) { 
45               System.out.println("Aborting read: " 
46                       + e.toString() 
47                       + " in LocalClassLoader."); 
48               throw new ClassNotFoundException(); 
49           } 
50       } 
51    
52       public Class loadClassFromFile(File fileTarget, 
53                                      boolean resolve) 
54               throws IOException { 
55           long bytecount = fileTarget.length(); 
56           byte[] buffer = new byte[(int) bytecount]; 
57           FileInputStream f = new FileInputStream(fileTarget); 
58           int readCount = f.read(buffer); 
59           f.close(); 
60           System.out.println("read in:" + buffer.length + " bytes"); 
61           System.out.println("file name=:" + fileTarget.getName()); 
62           // This is where it dies.... 
63           ClassFile cf = ClassFile.getClassFile(fileTarget); 
64           Class c = defineClass(cf.getClassName(), buffer, 0, (int) bytecount); 
65           System.out.println("This is never reached"); 
66           if (resolve) resolveClass(c); 
67           System.out.println("[Loaded " + c.getName() 
68                   + " from " + fileTarget 
69                   + " (" + bytecount + " bytes)]"); 
70           return c; 
71       } 
72    
73       public Class loadClassFromFile(File fileTarget, String className, 
74                                      boolean resolve) 
75               throws IOException { 
76           long bytecount = fileTarget.length(); 
77           byte[] buffer = new byte[(int) bytecount]; 
78           FileInputStream f = new FileInputStream(fileTarget); 
79           int readCount = f.read(buffer); 
80           f.close(); 
81           Class c = defineClass(className, buffer, 0, (int) bytecount); 
82           if (resolve) resolveClass(c); 
83           System.out.println("[Loaded " + className 
84                   + " from " + fileTarget 
85                   + " (" + bytecount + " bytes)]"); 
86           return c; 
87       } 
88    
89       public static void doIt(String argv[]) { 
90           try { 
91               if (argv.length < 1) { 
92                   System.out.println("Usage: java Test "); 
93                   return; 
94               } 
95               String target = argv[0]; 
96               DelegatingLoader loader = 
97                       new DelegatingLoader("ersatz/"); 
98               Class c = loader.loadClass(target, true); 
99               Object[] arg = {}; 
100              Class[] argClass = {}; 
101              // invoke the main method on the given class 
102              // with the argument list. 
103              c.getMethod("main", argClass).invoke(null, arg); 
104          } catch (Exception e) { 
105              System.out.println("Error " + e.toString() 
106                      + " in Test.doIt."); 
107          } 
108      } 
109  } 
110   
111