/Users/lyon/j4p/src/classUtils/dumper/ByteCodeContainer.java

1    package classUtils.dumper; 
2     
3    import net.compute.ComputableObject; 
4    import net.compute.ReLoader; 
5     
6    import java.io.ByteArrayOutputStream; 
7    import java.io.FileInputStream; 
8    import java.io.Serializable; 
9     
10   /** 
11    * Send the ByteCodeContainer to a remote JVM. It 
12    * uses the RCL to define a new class and compute 
13    * an answer. Computation servers need these 
14    * things to be sent by the computation clients. 
15    */ 
16    
17   public class ByteCodeContainer 
18           extends ClassLoader 
19           implements Serializable { 
20       String className; 
21       byte byteCodes[]; 
22    
23       public static ByteCodeContainer getLoader( 
24               ComputableObject o) { 
25           return new ByteCodeContainer( 
26                   o.getClass()); 
27       } 
28    
29       public ByteCodeContainer(Class c) { 
30           className = c.getName(); 
31           ReLoader rl = new ReLoader( 
32                   "C:\\lyon\\j4p\\classes"); 
33           byteCodes = rl.getByteCodes(className); 
34       } 
35    
36    
37       public ByteCodeContainer(String cn, 
38                                byte b[]) { 
39           byteCodes = b; 
40           className = cn; 
41       } 
42    
43    
44       public void loadIt() { 
45           loadClass(className, true); 
46       } 
47    
48       // for homework 
49       // write a program 
50       // in Java. That can list 
51       // All of the methods that contain 
52       // a method that is of  the signature 
53       // public static void main(String args[]) 
54       // These methods can be in many classes 
55       // and all must reside in a package that is given. 
56       // For example: 
57       // Method [] getMainMethods(Package p); 
58       // Method m[] = getMainMethods(networkPackage); 
59       // returns a list of methods that are in the networkPackage 
60       public Serializable compute() 
61               throws InstantiationException, 
62                      IllegalAccessException { 
63           Class c = defineClass(className, 
64                                 byteCodes, 
65                                 0, 
66                                 byteCodes.length); 
67           Object o = c.newInstance(); 
68           if (!(o instanceof ComputableObject)) { 
69               System.out.println( 
70                       "this is computable in remoteclass loader"); 
71               return null; 
72           } 
73           return ((ComputableObject) o).compute(); 
74       } 
75    
76       public void reload() { 
77           // force the class to be reloaded, 
78           // in case it has changed. 
79           try { 
80               Class c = defineClass(className, 
81                                     byteCodes, 
82                                     0, 
83                                     byteCodes.length); 
84               System.out.println( 
85                       "resolving:" + className); 
86               resolveClass(c); 
87           } catch (Exception e) { 
88           } 
89       } 
90    
91       protected synchronized Class loadClass( 
92               String s, boolean b) { 
93           Class cl = findLoadedClass(s); 
94           try { 
95               if (cl == null)  // not in cache! 
96                   return findSystemClass(s); 
97           } catch (ClassNotFoundException e) { 
98           } 
99           System.out.println("defining:" + s); 
100          Class c = defineClass(className, 
101                                byteCodes, 
102                                0, 
103                                byteCodes.length); 
104          System.out.println( 
105                  "resolving:" + className); 
106          if (c != null && b) resolveClass(c); 
107          return c; 
108      } 
109   
110      public static ByteCodeContainer getByteCodeContainer() { 
111          return getByteCodeContainer( 
112                  futils.Futil.getFileInputStream( 
113                          "Select a class file")); 
114      } 
115   
116      public static ByteCodeContainer getByteCodeContainer( 
117              FileInputStream fis) { 
118          Main dc = new Main(); 
119   
120          //System.out.println("class Dumper say I have:"+cf.getClassName()); 
121          try { 
122              ClassFile cf = ClassFile.readClassFile(fis); 
123              ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
124              cf.write(baos); 
125              baos.close(); 
126              byte b[] = baos.toByteArray(); 
127              //System.out.println("bytecode found:"+b.length); 
128              ByteCodeContainer bcc = new ByteCodeContainer( 
129                      cf.getClassName(), b); 
130              //System.out.println("got class!"+bcc.getClass()); 
131              return bcc; 
132          } catch (Exception e) { 
133              e.printStackTrace(); 
134          } 
135          return null; 
136      } 
137   
138  }