/Users/lyon/j4p/src/security/WebStartUtils.java

1    package security; 
2     
3    // security.WebStartUtils 
4     
5    import classUtils.reflection.Command; 
6    import com.jcraft.jsch.UserInfo; 
7    import futils.Futil; 
8    import gui.In; 
9    import net.scp.ScpTo; 
10   import net.rmi.utils.Compile; 
11   import utils.SystemUtils; 
12    
13   import java.io.File; 
14   import java.io.FileWriter; 
15   import java.io.IOException; 
16   import java.security.KeyStore; 
17   import java.util.Date; 
18   import java.util.jar.JarFile; 
19    
20    
21   /** 
22    * DocJava, Inc. User: lyon Date: May 26, 2004 Time: 10:39:47 AM 
23    */ 
24   public class WebStartUtils { 
25       String passwordField1 = ""; 
26    
27       public static void upLoadEmbeddedClass() { 
28           Class classFromFile = Command.getClassFromFile(); 
29           packSignUploadJNLP(classFromFile); 
30           In.message("finised with up load:"); 
31           System.exit(0); 
32       } 
33    
34       public static void generalUpload() { 
35           String className = In.getString("enter class name"); 
36           Command.getClass(className); 
37           final File keyStoreFile = 
38                   KeyUtils.getKeystoreFile(); 
39           String vendor = In.getString("enter vendor"); 
40           String url = In.getString("enter URL"); 
41           final String password = In.getPassword("enter keystore password"); 
42           KeyStore ks = KeyUtils.getKeyStore(password); 
43           final String alias = KeyUtils.getAlias(ks); 
44           String codeBase = In.getString("enter codebase, for example:" + 
45                   "http://show.docjava.com:8086/" + 
46                   "book/cgij/code/jnlp/"); 
47           String iconUrl = In.getString("enter icon url, for example:" + 
48                   "http://show.docjava.com:8086/" + 
49                   "consulti/docjava.jpe"); 
50           String user = In.getString("enter uid, for example:lyon"); 
51           String host = In.getString("enter target upload host, " + 
52                   "for example:" + 
53                   "192.168.1.95"); 
54           final String rootDirectory = In.getString("enter root directory on upload host," + 
55                   " for example:/var/www/html/book/" + 
56                   "cgij/code/jnlp/"); 
57           String jarFileName = className + 
58                   ".jar"; 
59           Initium.packSignAndUpload(className, 
60                   jarFileName, 
61                   keyStoreFile, 
62                   password, 
63                   alias, 
64                   vendor, 
65                   url, 
66                   codeBase, 
67                   iconUrl, 
68                   user, 
69                   host, 
70                   rootDirectory); 
71           In.message("finish upload"); 
72           System.exit(0); 
73       } 
74    
75       public static void packSignUploadJNLP() { 
76           packSignUploadJNLP(gui.In.getString("enter class name")); 
77       } 
78    
79       public static void packSignUploadJNLP(Class c) { 
80           packSignUploadJNLP(c.getName()); 
81       } 
82    
83       public static void packSignUploadJNLP(String mainClassName) { 
84    
85           Command.getClass(mainClassName); 
86           final File keyStoreFile = 
87                   KeyUtils.getKeystoreFile(); 
88           //= futils.Futil.getReadFile( 
89           //"select keystore"); 
90           String vendor = "DocJava, Inc."; 
91           String url = "http://www.docjava.com"; 
92           final String password = In.getPassword("enter keystore password"); 
93           KeyStore ks = KeyUtils.getKeyStore(password); 
94           final String alias = KeyUtils.getAlias(ks); 
95           String codeBase = "http://show.docjava.com:8086/" + 
96                   "book/cgij/code/jnlp/"; 
97           String iconUrl = "http://show.docjava.com:8086/" + 
98                   "consulti/docjava.jpe\""; 
99           String user = "lyon"; 
100          String webServer = "192.168.1.95"; 
101          final String rootDirectory = "/var/www/html/book/cgij/code/jnlp/"; 
102          String jarFileName = mainClassName + 
103                  ".jar"; 
104   
105   
106          Initium.packSignAndUpload(mainClassName, 
107                  jarFileName, 
108                  keyStoreFile, 
109                  password, 
110                  alias, 
111                  vendor, 
112                  url, 
113                  codeBase, 
114                  iconUrl, 
115                  user, 
116                  webServer, 
117                  rootDirectory); 
118      } 
119   
120      public static void packSignOutputJnlp(final String mainClassName, 
121                                            final File keyStoreFile, 
122                                            final String password, 
123                                            final String alias, 
124                                            String vendor, 
125                                            String url) { 
126          String codeBase = "http://show.docjava.com:8086/book/cgij/code/jnlp/"; 
127          String iconUrl = "http://show.docjava.com:8086/consulti/docjava.jpe\""; 
128          String user = "lyon"; 
129          String webServer = "192.168.1.95"; 
130          final String rootDirectory = "/var/www/html/book/cgij/code/jnlp/"; 
131          String jarFileName = mainClassName + 
132                  ".jar"; 
133          Initium.packSignAndUpload(mainClassName, 
134                  jarFileName, 
135                  keyStoreFile, 
136                  password, 
137                  alias, 
138                  vendor, 
139                  url, 
140                  codeBase, 
141                  iconUrl, 
142                  user, 
143                  webServer, 
144                  rootDirectory); 
145   
146          //SignUtils.verify(jarFileName); 
147          verifyJar(jarFileName); 
148      } 
149   
150      /** 
151       * Use the built-in JarVerifier class to verify the existance and 
152       * signing of the Jar file. 
153       */ 
154      public static void verifyJar(String jarFileName) { 
155          try { 
156              new JarFile(jarFileName); 
157          } catch (IOException e) { 
158              e.printStackTrace(); 
159          } 
160      } 
161   
162      /** 
163       * Synthesize and upload the JNLP and Jar files. 
164       * 
165       * @param title       Application title 
166       * @param vendor      Application owner 
167       * @param jarFileName A packed jar file 
168       * @param mainClass   A fully qualified class name 
169       * @param jnlpFile    The JNLP used for launching 
170       */ 
171      public static void upLoadJnlp(String title, 
172                                    String vendor, 
173                                    String homePage, 
174                                    String jarFileName, 
175                                    String mainClass, 
176                                    File jnlpFile, 
177                                    String codeBase, 
178                                    String iconUrl, 
179                                    String user, 
180                                    String host, 
181                                    UserInfo ui, 
182                                    String rootDirectory) { 
183          String cmd = 
184                  "<jnlp href=\"" + 
185                  jnlpFile.getName() + 
186                  "\" codebase=\"" + 
187                  codeBase + 
188                  "\">\n" + 
189                  "  <information>\n" + 
190                  "    <title>" + 
191                  title + 
192                  "</title>\n" + 
193                  "    <vendor>" + 
194                  vendor + 
195                  "</vendor>\n" + 
196                  "    <homepage href=\"" + 
197                  homePage + 
198                  "\"/>\n" + 
199                  "<icon href=\"" + 
200                  iconUrl + 
201                  "/>" + 
202                  "\n" + 
203                  "    <offline-allowed />\n" + 
204                  "  </information>\n" + 
205                  "  \n" + 
206                  "  <security>\n" + 
207                  "    <all-permissions />\n" + 
208                  "  </security>\n" + 
209                  "  \n" + 
210                  "  <resources>\n" + 
211                  "    <j2se version=\"" + 
212                  "1.4+" + 
213                  "\" />\n" + 
214                  "    <jar href=\"" + 
215                  jarFileName + 
216                  "\" />\n" + 
217                  "  </resources>\n" + 
218                  " \n" + 
219                  "  <application-desc main-class=\"" + 
220                  mainClass + 
221                  "\" />\n" + 
222                  "</jnlp>\n"; 
223          try { 
224              uploadJnlp(jnlpFile, 
225                      cmd, 
226                      rootDirectory, 
227                      jarFileName, 
228                      user, 
229                      host, 
230                      ui); 
231          } catch (IOException e) { 
232              e.printStackTrace(); 
233          } 
234      } 
235   
236      private static void uploadJnlp(File jnlpFile, 
237                                     String cmd, 
238                                     final String rootDirectory, 
239                                     String jarFileName, 
240                                     String user, 
241                                     String host, 
242                                     UserInfo ui) 
243              throws IOException { 
244          saveToDisk(jnlpFile, cmd); 
245          String rJnlpFile = rootDirectory + 
246                  jnlpFile.getName(); 
247          String rJarFile = rootDirectory + 
248                  jarFileName; 
249          ScpTo.scpTo(jnlpFile.toString(), 
250                  user, 
251                  host, 
252                  rJnlpFile, 
253                  ui); 
254          ScpTo.scpTo(jarFileName, 
255                  user, 
256                  host, 
257                  rJarFile, 
258                  ui); 
259          print("done with xfer at:" + 
260                  new Date()); 
261      } 
262   
263      public static void print(Object o) { 
264          System.out.println(o); 
265      } 
266   
267      public static void writeJnlp(String title, 
268                                   String vendor, 
269                                   String homePage, 
270                                   String jarFileName, 
271                                   String mainClass, 
272                                   File jnlpFile, 
273                                   String codeBase) { 
274          String jnlpData = 
275                  "<jnlp href=\"" + 
276                  jnlpFile.getName() + 
277                  "\" codebase=\"file://" + 
278                  codeBase + 
279                  "\">\n" + 
280                  "  <information>\n" + 
281                  "    <title>" + 
282                  title + 
283                  "</title>\n" + 
284                  "    <vendor>" + 
285                  vendor + 
286                  "</vendor>\n" + 
287                  "    <homepage href=\"" + 
288                  homePage + 
289                  "\"/>\n" + 
290                  "\n" + 
291                  "    <offline-allowed />\n" + 
292                  "  </information>\n" + 
293                  "  \n" + 
294                  "  <security>\n" + 
295                  "    <all-permissions />\n" + 
296                  "  </security>\n" + 
297                  "  \n" + 
298                  "  <resources>\n" + 
299                  "    <j2se version=\"" + 
300                  "1.4+" + 
301                  "\" />\n" + 
302                  "    <jar href=\"" + 
303                  jarFileName + 
304                  "\" />\n" + 
305                  "  </resources>\n" + 
306                  " \n" + 
307                  "  <application-desc main-class=\"" + 
308                  mainClass + 
309                  "\" />\n" + 
310                  "</jnlp>\n"; 
311          try { 
312              saveToDisk(jnlpFile, jnlpData); 
313          } catch (IOException e) { 
314              e.printStackTrace(); 
315          } 
316      } 
317   
318      /** 
319       * Save the <code>data</code> out to the file called 
320       * <code>jnlpFile</code> 
321       * 
322       * @param jnlpFile 
323       * @param data 
324       * @throws IOException 
325       */ 
326      private static void saveToDisk(File jnlpFile, 
327                                     String data) 
328              throws IOException { 
329          System.out.println("output to:" + 
330                  jnlpFile + 
331                  "\n" + 
332                  data); 
333          FileWriter fw = 
334                  new FileWriter(jnlpFile); 
335          fw.write(data); 
336          fw.write("\n"); 
337          fw.close(); 
338      } 
339   
340      public static void testGetWebstartLocation() { 
341          In.message("found javaws at:" + getWebstartLocation()); 
342      } 
343   
344      /** 
345       * Search for the Java web start application. If you cannot find it, 
346       * prompt the user. In the future, there should be a better way to do 
347       * this, but what, I don't know. And what if javaws cannot be found 
348       * because it is not installed? Tricky! 
349       * 
350       * @return the java webstart file. 
351       */ 
352      public static File getWebstartLocation() { 
353          String s = SystemUtils.getDirectorySeparator(); 
354          // check widows location: 
355          File f = new File("C:" + 
356                  s + 
357                  "Program Files" + 
358                  s + 
359                  "Java Web Start" + 
360                  s + 
361                  "javaws.exe"); 
362          if (f.exists()) return f; 
363          // check mac location: 
364          f = new File("/" + 
365                  "Applications" + 
366                  s + 
367                  "Utilities" + 
368                  s + 
369                  "Java" + 
370                  s + 
371                  "Java Web Start.app" + 
372                  s + 
373                  "Contents" + 
374                  s + 
375                  "MacOS" + 
376                  s + 
377                  "Java Web Start"); 
378          if (f.exists()) return f; 
379          // check linux location for jdk1.5: 
380          f = new File("" + 
381                  s + 
382                  "usr" + 
383                  s + 
384                  "java" + 
385                  s + 
386                  "j2sdk1.5.0" + 
387                  s + 
388                  "bin" + 
389                  s + 
390                  "javaws"); 
391          if (f.exists()) return f; 
392          f = 
393                  Futil.getReadFile( 
394                          "could not find javaws, please help me locate it"); 
395          if (f == null) { 
396              In.message("user canceled operation, program terminates"); 
397              System.exit(0); 
398          } 
399          String cn = "security.WebstartUtils"; 
400          System.out.println("please add:" + f + " to" + cn); 
401          return f; 
402      } 
403   
404      /** 
405       * Start web start on the given url, then kill the process, to show we 
406       * have control over stopping and starting a process. 
407       */ 
408      public static void startWebStart() { 
409          File webStart = getWebstartLocation(); 
410          String args[] = { 
411              webStart.toString(), 
412              "http://show.docjava.com:8086/book/cgij/code/jnlp/dhry.Main.jnlp" 
413          }; 
414          try { 
415              Runtime rt = Compile.asyncRunExec(args, null); 
416              rt.exit(0); 
417          } catch (IOException e) { 
418              e.printStackTrace(); 
419          } 
420      } 
421  } 
422