/Users/lyon/j4p/src/classUtils/loaders/DynamicClassLoadingComputeClient.java

1    package classUtils.loaders; 
2     
3    //package net.compute; 
4     
5     
6    import java.io.ObjectInputStream; 
7    import java.io.ObjectOutputStream; 
8    import java.net.Socket; 
9    import java.util.StringTokenizer; 
10    
11   /** 
12    * 1. Use the ComputeClient to create a Reloader 
13    * that gives you bytes codes. 
14    * 2. Get the RemoteClassLoader and send it to the 
15    *   compute server. 
16    * 3. The compute server define and computes the class using 
17    *   the remote class loader. 
18    * 4. It then returns the answer to the compute client. 
19    */ 
20    
21   public class DynamicClassLoadingComputeClient 
22           implements Runnable { 
23       private ByteCodeContainer remoteClassLoader; 
24       private String computeServer = "172.16.11.107"; 
25       private int computeServerPort = 8086; 
26    
27       public void setComputableObject(ComputableObject co) { 
28    
29           //remoteClassLoader = rcl; 
30       } 
31    
32       public String[] getSystemClassPaths() { 
33           String s = System.getProperty("java.class.path"); 
34           StringTokenizer st = new StringTokenizer(s, ";"); 
35           int n = st.countTokens(); 
36           String paths[] = new String[n]; 
37           for (int i = 0; i < n; i++) 
38               paths[i] = st.nextToken(); 
39           return paths; 
40       } 
41    
42       public void setRemoteClassLoader() { 
43           String paths[] = getSystemClassPaths(); 
44    
45           String classPath = paths[0]; 
46           String className1 = "distClasses.ComputeThis"; 
47    
48           byte[] b = null; 
49    
50           try { 
51    
52               Reloader rl = 
53                       new Reloader(classPath); 
54               rl.loadClass(className1); 
55               b = rl.getByteCodes(className1); 
56               //print(b); 
57               remoteClassLoader = new ByteCodeContainer(className1, b); 
58           } catch (ClassNotFoundException e) { 
59               e.printStackTrace(); 
60           } 
61       } 
62    
63       public static void main(String args[]) { 
64           DynamicClassLoadingComputeClient cc = new DynamicClassLoadingComputeClient(); 
65           cc.setRemoteClassLoader(); 
66           //cc.setComputableObject( new ANewComputableObject()); 
67           cc.run(); 
68       } 
69    
70       public void run() { 
71           try { 
72               Socket s 
73                       = new Socket(computeServer, computeServerPort); 
74               ObjectInputStream 
75                       ois = 
76                       new ObjectInputStream( 
77                               s.getInputStream()); 
78               ObjectOutputStream 
79                       oos = 
80                       new ObjectOutputStream( 
81                               s.getOutputStream()); 
82    
83               if (remoteClassLoader == null) { 
84                   System.out.println("The Object is null 1"); 
85                   return; 
86               } 
87    
88               oos.writeObject(remoteClassLoader); 
89               // block the thread of execution 
90               // until the computation is finished by 
91               // the compute server. E.g. 
92               // a = f(b); 
93               Object o = ois.readObject(); 
94               System.out.println(o); 
95               ois.close(); 
96               oos.close(); 
97           } catch (Exception e) { 
98               e.printStackTrace(); 
99           } 
100      } 
101  }