/Users/lyon/j4p/src/net/rmi/utils/Compile.java

1    package net.rmi.utils; 
2     
3    import classUtils.putils.ClassPathUtils; 
4    import classUtils.reflection.ReflectUtil; 
5    import gui.In; 
6    import utils.SystemUtils; 
7     
8    import java.io.BufferedReader; 
9    import java.io.File; 
10   import java.io.IOException; 
11   import java.io.InputStreamReader; 
12   import java.rmi.Remote; 
13    
14   public class Compile { 
15    
16       public static void main(String args[]) { 
17           rmicTestDos2(); 
18           //startWebStart(); 
19           //WebStartUtils.testGetWebstartLocation(); 
20       } 
21    
22       public static void rmic() { 
23           String 
24                   fn = "rmi.rmiimage.Server"; 
25           rmic(fn); 
26       } 
27    
28       private static void rmicTestDos() { 
29           Runtime rt = Runtime.getRuntime(); 
30           String args1[] = { 
31               "C:\\j2sdk1.4.1_02\\bin\\rmic", 
32               "-v1.2 -classpath", "c:\\lyon\\j4p\\classes", 
33               "–d", "c:\\lyon\\j4p\\classes", 
34               "net.rmi.armi.RemoteHelloImplementation" 
35           }; 
36           String args[] = { 
37               "dir" 
38           }; 
39           try { 
40               Process p = rt.exec(args, 
41                       null, 
42                       new File("c:\\lyon\\j4p\\classes")); 
43               BufferedReader br 
44                       = new BufferedReader( 
45                               new InputStreamReader(p.getErrorStream())); 
46               String s = null; 
47               while ((s = br.readLine()) != null) 
48                   System.out.println("dos:" + s); 
49           } catch (IOException e) { 
50               e.printStackTrace(); 
51           } 
52       } 
53    
54    
55       // For homework, automate the computation of 
56       // the following variables: 
57       /* 
58           File workingDirectory = new File("c:\\lyon\\j4p\\classes"); 
59           String className = "net.rmi.armi.RemoteHelloImplementation"; 
60           String destinationDirectory = "c:\\lyon\\j4p\\classes\\net\\rmi\\armi\\"; 
61           String classPath = "c:\\lyon\\j4p\\classes\\"; 
62       */ 
63       private static void rmicTestDos2() { 
64           Class c = net.rmi.armi.RemoteHelloImplementation.class; 
65           runRmic(c); 
66    
67       } 
68       public static boolean isImplementingRemoteInterface(Class c) { 
69          Class ca[]= ReflectUtil.getAllInterfaces(c); 
70    
71          Class remoteClass = Remote.class; 
72           for (int i=0; i < ca.length; i++){ 
73               System.out.println(ca[i]); 
74               if (ca[i].equals(remoteClass)) return true; 
75           } 
76           return false; 
77       } 
78       /** 
79        * Run rmic 
80        * @param c target of the rmic command 
81        */ 
82       public static void runRmic(Class c) { 
83           System.out.println("running rmic on:"+c.getName()); 
84           if (!isImplementingRemoteInterface(c) ){ 
85               In.message("Class:"+c.getName()+ 
86                       " does not implement the Remote Interface\nProgram dies"); 
87               System.exit(0); 
88           } 
89           File workingDirectory = 
90                   new File(ClassPathUtils.getClassPathInUserDir()); 
91           System.out.println(workingDirectory.toString()); 
92           checkFile(workingDirectory); 
93    
94           File destinationDirectory = new File(workingDirectory.toString()); 
95           System.out.println("output to>"+destinationDirectory.getAbsolutePath()); 
96           String classPath = workingDirectory.toString(); 
97           runRmic(classPath, 
98                   destinationDirectory, 
99                   workingDirectory, 
100                  c); 
101      } 
102   
103      public static void rmicTester() { 
104          File workingDirectory = 
105                  new File(ClassPathUtils.getClassPathInUserDir()); 
106          String classPath = workingDirectory.toString(); 
107          String version = "-v1.2"; 
108          File destinationDirectory = new File(workingDirectory.toString()); 
109   
110          Class clazz = net.rmi.armi.RemoteHelloImplementation.class; 
111          rmic(version, classPath, destinationDirectory, clazz); 
112      } 
113      public static void checkFile(File f) { 
114          if (!f.exists()) { 
115              In.message("could not find: " + 
116                      f + 
117                      "\n Program exits!"); 
118              System.exit(0); 
119          } 
120      } 
121   
122      /** 
123       * This invocation fails on a mac. 
124       * 
125       * @param classPath            location of classes with multiple 
126       *                             directories and jars. 
127       * @param destinationDirectory place to put the stubs 
128       * @param workingDirectory     place from which to launch the program. 
129       * @param c            target of the stub generator. 
130       */ 
131      public static void runRmic(String classPath, 
132                                 File destinationDirectory, 
133                                 File workingDirectory, 
134                                 Class c) { 
135          String rmicLocation = SystemUtils.getRmicPath().toString(); 
136          System.out.println("rmicLocation="+rmicLocation); 
137          String args[] = { 
138              rmicLocation, 
139              "-v1.2", 
140              "-classpath", classPath, 
141              "-d", destinationDirectory.toString(), 
142              c.getName() 
143          }; 
144          try { 
145              runExec(args, workingDirectory); 
146          } catch (Exception e) { 
147              e.printStackTrace(); 
148          } 
149      } 
150   
151      /** 
152       * my Theory is that you can kill the process at any time and not 
153       * bother reading the output with a readline...however this has not 
154       * been tested. 
155       * 
156       * @param args 
157       * @param workingDirectory 
158       * @return 
159       */ 
160      private static Process asynchronousRunExec(final String[] args, 
161                                                 final File workingDirectory) { 
162          final Process[] p = new Process[]{null}; 
163          Thread t = new Thread(new Runnable() { 
164              public void run() { 
165                  Runtime rt = Runtime.getRuntime(); 
166                  try { 
167                      p[0] = rt.exec(args, null, workingDirectory); 
168                  } catch (IOException e) { 
169                      e.printStackTrace(); 
170                  } 
171              } 
172          }); 
173          t.start(); 
174          return p[0]; 
175      } 
176   
177      /** 
178       * Asynchronous invocation of the OS commands in the args directory 
179       * 
180       * @param args 
181       * @param workingDirectory 
182       * @return A stoppable process. 
183       * @throws IOException 
184       */ 
185      public static Runtime asyncRunExec(final String[] args, 
186                                         final File workingDirectory) 
187              throws IOException { 
188          final Runtime rt = Runtime.getRuntime(); 
189          final Process p; 
190          p = rt.exec(args, null, workingDirectory); 
191          Thread t = new Thread(new Runnable() { 
192              public void run() { 
193                  try { 
194   
195                      BufferedReader br 
196                              = new BufferedReader(new InputStreamReader( 
197                                      p.getErrorStream())); 
198                      String s = null; 
199                      while ((s = br.readLine()) != null) 
200                          System.out.println("dos:" + s); 
201                  } catch (IOException e) { 
202                      e.printStackTrace(); 
203   
204                  } 
205   
206              } 
207          }); 
208          t.start(); 
209          return rt; 
210      } 
211   
212      /** 
213       * Run the args array using the workingDirectory as the home. 
214       * 
215       * @param args 
216       * @param workingDirectory 
217       * @throws IOException 
218       */ 
219      public static void runExec(String[] args, 
220                                 File workingDirectory) throws IOException { 
221          for (int i = 0; i < args.length; i++) 
222              System.out.print(" " + args[i]); 
223          System.out.println("\nworking directory=" + workingDirectory); 
224   
225          Runtime rt = Runtime.getRuntime(); 
226          Process p = rt.exec(args, null, workingDirectory); 
227          BufferedReader br 
228                  = new BufferedReader( 
229                          new InputStreamReader(p.getErrorStream())); 
230          String s = null; 
231          while ((s = br.readLine()) != null) 
232              System.out.println("dos:" + s); 
233          try { 
234              p.waitFor(); 
235          } catch (InterruptedException e) { 
236              e.printStackTrace(); 
237          } 
238      } 
239   
240   
241   
242      public static void rmic(String version, 
243                              String classpath, 
244                              File destinationDirectory, 
245                              Class clazz) { 
246          String args[] = { 
247              version, 
248              "-classpath", classpath, 
249              "-d", destinationDirectory.toString(), 
250              clazz.getName() 
251          }; 
252          rmic(args); 
253      } 
254   
255      public static void rmic(String[] args) { 
256          sun.rmi.rmic.Main.main(args); 
257      } 
258   
259      public static void rmicHelp(){ 
260          String args[] ={ 
261              "-help" 
262          }; 
263          rmic(args); 
264      } 
265      /** 
266       * This is an older version of RMI that no longer 
267       * works with jdk version 1.4 or higher! 
268       * @param c 
269       */ 
270      public static void rmic(Class c) { 
271          String cn = c.getName(); 
272          String cp = ClassPathUtils.getClassPathInUserDir(); 
273          String destDir = cp; 
274          String args[] = { 
275              "-classpath", cp, 
276              "-v1.2", 
277              "-d", destDir, 
278              cn 
279          }; 
280          sun.rmi.rmic.Main.main(args); 
281      } 
282   
283      static public void rmic(String fn) { 
284          String args[] = {fn}; 
285          rmic(args); 
286      } 
287  }