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

1    package net.compute; 
2     
3    /** 
4     * Created by 
5     * User: lyon 
6     * Date: Sep 3, 2003 
7     * Time: 11:17:45 AM 
8     * 
9     */ 
10    
11   import java.io.*; 
12    
13   /** 
14    *  Use the reloader to get the bytecode from 
15    * a file for a class. Then, transmit the class byte 
16    * codes to the computation server. Use the 
17    * Reloader on the computation server to define the 
18    * class. Then compute the computation. 
19    * Send the answer back to the compute client. 
20    * Finally, set the remote reloader to null and 
21    * close the connections. 
22    */ 
23    
24   public class ReLoader extends ClassLoader { 
25    
26       private String classPath; 
27    
28       public ReLoader() { 
29           classPath = null; 
30       } 
31    
32       public ReLoader(String classPath) { 
33    
34           this.classPath = classPath; 
35       } 
36    
37    
38    
39       public Class defineClass(byte b[], String className) { 
40           return super.defineClass(className, b, 0, b.length); 
41       } 
42    
43       public synchronized Class loadClass(String className, 
44                                           boolean resolveIt) 
45               throws ClassNotFoundException { 
46    
47           Class result; 
48           byte classData[]; 
49    
50           // Check the loaded class cache 
51           result = findLoadedClass(className); 
52           if (result != null) { 
53               // Return a cached class 
54               System.out.println("The class has already been loaded"); 
55               return result; 
56           } 
57    
58           // If Spoofed, don't delegate 
59           // result is 0 if strings are equal. 
60           if (className.compareTo("Spoofed") != 0) { 
61    
62               // Check with the system class loader 
63               try { 
64                   return loadWithSystemClassLoader(className); 
65               } catch (ClassNotFoundException e) { 
66                   System.out.println("unable to load with system class loader..."); 
67               } 
68           } 
69    
70           // Don't attempt to load a system file except through 
71           // the primordial class loader 
72           // for example, java.lang.String 
73           if (className.startsWith("java.")) { 
74               throw new ClassNotFoundException(); 
75           } 
76    
77           // Try to load it from the classPath directory. 
78           classData = getByteCodes(className); 
79           System.out.println("classData has " + classData.length + " bytes"); 
80           if (classData == null) { 
81               System.out.println("Reloader - Can't load class: " 
82                       + className); 
83               throw new ClassNotFoundException(); 
84           } 
85    
86           System.out.println("defining class..." + className); 
87           try { 
88               result = defineClass(null, classData, 0, 
89                       classData.length); 
90           } catch (ClassFormatError classFormatError) { 
91               classFormatError.printStackTrace(); 
92               System.out.println("class format error!!"); 
93               System.exit(0); 
94           } 
95           System.out.println("It worked!!! newly defined class:" + result); 
96           if (result == null) { 
97               System.out.println("Reloader - Class format error: " 
98                       + className); 
99               throw new ClassFormatError(); 
100          } 
101   
102          if (resolveIt) { 
103              resolveClass(result); 
104          } 
105   
106          // Return class from classPath directory 
107          return result; 
108      } 
109   
110      private Class loadWithSystemClassLoader(String className) 
111              throws ClassNotFoundException { 
112          return super.findSystemClass(className); 
113      } 
114   
115      public byte[] getByteCodes(String typeName) { 
116   
117          FileInputStream fis; 
118          String fileName = classPath + File.separatorChar 
119                  + typeName.replace('.', File.separatorChar) 
120                  + ".class"; 
121   
122          try { 
123              fis = new FileInputStream(fileName); 
124          } catch (FileNotFoundException e) { 
125              e.printStackTrace(); 
126              return null; 
127          } 
128   
129          BufferedInputStream bis = new BufferedInputStream(fis); 
130   
131          ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
132   
133          try { 
134              int c = bis.read(); 
135              while (c != -1) { 
136                  baos.write(c); 
137                  c = bis.read(); 
138              } 
139          } catch (IOException e) { 
140              e.printStackTrace(); 
141              return null; 
142          } 
143   
144          return baos.toByteArray(); 
145      } 
146   
147      public static void main(String[] args) { 
148   
149          String classPath = "C:\\lyon\\tomcat\\common\\classes"; 
150          String className1 = "net.compute.ReLoader"; 
151   
152          try { 
153   
154              ReLoader rl = 
155                      new ReLoader(classPath); 
156              System.out.println("loading class..."); 
157              rl.loadClass(className1); 
158              byte b[] = rl.getByteCodes(className1); 
159              print(b); 
160          } catch (ClassNotFoundException e) { 
161              e.printStackTrace(); 
162          } 
163   
164   
165      } 
166   
167      public static void print(byte b[]) { 
168          for (int i = 0; i < b.length; i++) { 
169              System.out.print((char) (0xff & b[i])); 
170              if ((i % 10) == 0) System.out.println(); 
171          } 
172      } 
173   
174      public static void reloadClasses(String classPath, String className) 
175              throws ClassNotFoundException { 
176          System.out.println("loading classpath:" + classPath 
177                  + "loading class name:" + className + "------"); 
178          ReLoader rl = 
179                  new ReLoader(classPath); 
180          System.out.println("loading class..."); 
181          Class c = rl.loadClass(className); 
182          System.out.println("class returned:" + c); 
183          //((Runnable) c.newInstance()).run(); 
184      } 
185  } 
186   
187