KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > multiServer > launch > LaunchApp


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.multiServer.launch;
23
24 // Kernel imports
25
import org.enhydra.multiServer.kernel.Kernel;
26 import org.enhydra.multiServer.bootstrap.Bootstrap;
27
28 // Standard imports
29
import java.util.Properties JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.PrintStream JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37 import java.io.FileWriter JavaDoc;
38 import javax.swing.JOptionPane JavaDoc;
39
40 //
41
public class LaunchApp {
42     protected static boolean debug = false;
43     private final String JavaDoc PROPERTY_HOME = "launch.home";
44     private final String JavaDoc PROPERTY_CONF_DIR = "enhydra.conf";
45     private final String JavaDoc PROPERTY_CONF_FILE = "launch.bootstrap";
46     private final String JavaDoc PROPERTY_POLICY = "java.security.policy";
47     private final String JavaDoc PROPERTY_BROWSER = "launch.browser";
48     private final String JavaDoc PROPERTY_USER_HOME = "user.home";
49     private String JavaDoc[] bootArgs = new String JavaDoc[0];
50     private String JavaDoc home = null;
51     private String JavaDoc browser = null;
52     private BootThread boot = null;
53     private LaunchFrame frame = null;
54     private PrintWriter JavaDoc logWriter = null;
55     protected static PrintStream JavaDoc sysOut = System.out;
56     protected static PrintStream JavaDoc sysErr = System.err;
57     protected static LaunchApp app = null;
58
59     public static void main(String JavaDoc[] args) {
60         LaunchApp.app = new LaunchApp();
61         app.init(args);
62     }
63
64     public LaunchApp() {}
65
66     public static void shutdownAlert(Kernel k) {
67         if (LaunchApp.app != null) {
68             LaunchApp.app.runKiller(30);
69             LaunchApp.app.storeAppProperties();
70             LaunchApp.app.closeFrame();
71         }
72     }
73
74     protected boolean isStarted() {
75         return (boot != null);
76     }
77
78     protected void setHome(String JavaDoc path) {
79         home = path;
80     }
81
82     protected String JavaDoc getHome() {
83         return home;
84     }
85
86     protected String JavaDoc getConfFile() {
87         String JavaDoc path = null;
88
89         if (bootArgs.length > 0) {
90             path = bootArgs[0];
91         }
92         return path;
93     }
94
95     protected void setConfFile(String JavaDoc path) {
96         if (bootArgs.length == 0) {
97             bootArgs = new String JavaDoc[1];
98         }
99         bootArgs[0] = path;
100     }
101
102     protected String JavaDoc getConfDir() {
103         String JavaDoc path = null;
104
105         if (System.getProperty(PROPERTY_CONF_DIR) != null) {
106             File JavaDoc dir = new File JavaDoc(System.getProperty(PROPERTY_CONF_DIR));
107
108             path = dir.getAbsolutePath();
109         }
110         return path;
111     }
112
113     protected void setConfDir(String JavaDoc path) {
114         System.setProperty(PROPERTY_CONF_DIR, path);
115     }
116
117     protected String JavaDoc getPolicy() {
118         String JavaDoc path = null;
119
120         if (System.getProperty(PROPERTY_POLICY) != null) {
121             File JavaDoc dir = new File JavaDoc(System.getProperty(PROPERTY_POLICY));
122
123             path = dir.getAbsolutePath();
124         }
125         return path;
126     }
127
128     protected void setPolicy(String JavaDoc path) {
129         System.setProperty(PROPERTY_POLICY, path);
130     }
131
132     protected void setBrowser(String JavaDoc path) {
133         browser = path;
134     }
135
136     protected String JavaDoc getBrowser() {
137         return browser;
138     }
139
140     protected void startServer() {
141         if ((frame == null) || (validatePaths())) {
142             stopServer();
143             boot = new BootThread();
144             boot.start();
145         }
146     }
147
148     private boolean validatePaths() {
149         boolean valid = true;
150         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
151         StringBuffer JavaDoc message = new StringBuffer JavaDoc();
152         File JavaDoc file = null;
153
154         // check home for enhydra.jar
155
if (!isHomeValid()) {
156             valid = false;
157             path.setLength(0);
158             path.append(getHome());
159             path.append(File.separator);
160             path.append("lib");
161             path.append(File.separator);
162             path.append("enhydra.jar");
163             message.append("Invalid enhydra home directory: \n");
164             message.append(getHome());
165             message.append("\n\n");
166             message.append("File not found: \n");
167             message.append(path.toString());
168         }
169
170         // check conf dir for servlet.conf
171
if (valid) {
172             path.setLength(0);
173             path.append(getConfDir());
174             path.append(File.separator);
175             path.append("servlet");
176             path.append(File.separator);
177             path.append("servlet.conf");
178             file = new File JavaDoc(path.toString());
179             if (!file.isFile()) {
180                 valid = false;
181                 message.append("Invalid configuration directory: \n");
182                 message.append(getConfDir());
183                 message.append("\n\n");
184                 message.append("File not found: \n");
185                 message.append(path.toString());
186             }
187         }
188
189         // check conf dir for jndi.properties
190
if (valid) {
191             path.setLength(0);
192             path.append(getConfDir());
193             path.append(File.separator);
194             path.append("naming");
195             path.append(File.separator);
196             path.append("jndi.properties");
197             file = new File JavaDoc(path.toString());
198             if (!file.isFile()) {
199                 valid = false;
200                 message.append("Invalid configuration directory: \n");
201                 message.append(getConfDir());
202                 message.append("\n\n");
203                 message.append("File not found: \n");
204                 message.append(path.toString());
205             }
206         }
207
208         // check policy file
209
if (valid) {
210             path.setLength(0);
211             path.append(getPolicy());
212             file = new File JavaDoc(path.toString());
213             if ((!file.isFile()) || (!file.canRead())) {
214                 valid = false;
215                 message.append("Invalid policy file: \n");
216                 message.append(getPolicy());
217             }
218         }
219
220         // check conf file
221
if (valid) {
222             path.setLength(0);
223             path.append(this.getConfFile());
224             file = new File JavaDoc(path.toString());
225             if ((!file.isFile()) || (!file.canRead())) {
226                 valid = false;
227                 message.append("Invalid configuration file: \n");
228                 message.append(getConfFile());
229             }
230         }
231         if (!valid) {
232             JOptionPane.showMessageDialog(frame, message.toString(),
233                                           frame.getTitle(),
234                                           JOptionPane.ERROR_MESSAGE);
235         }
236         return valid;
237     }
238
239     protected void stopServer() {
240         storeAppProperties();
241         if (boot != null) {
242             runKiller(15);
243             boot.degrade();
244             boot = null;
245         }
246     }
247
248     protected void runKiller(int seconds) {
249         KillerThread killer = new KillerThread(seconds);
250
251         killer.setPriority(Thread.MAX_PRIORITY);
252         killer.start();
253     }
254
255     protected boolean isServerRunning() {
256         boolean running = false;
257
258         if (Kernel.getKernel() != null) {
259             running =
260                 Kernel.getKernel().getState().equalsIgnoreCase("running");
261         }
262         return running;
263     }
264
265     private void closeFrame() {
266         if (frame != null) {
267             freeOutput();
268             frame.clearAll();
269             frame = null;
270         }
271     }
272
273     protected static void freeOutput() {
274         System.setOut(LaunchApp.sysOut);
275         System.setErr(LaunchApp.sysErr);
276     }
277
278     protected void exit() {
279         storeAppProperties();
280         if (Kernel.getKernel() == null) {
281             System.setSecurityManager(null);
282             System.exit(0);
283         } else {
284             int response = JOptionPane.showConfirmDialog(frame,
285                     "Enhydra may be running. Do you want to shutdown the server?",
286                     frame.getTitle(), JOptionPane.YES_NO_CANCEL_OPTION,
287                     JOptionPane.QUESTION_MESSAGE);
288
289             if (response == JOptionPane.CANCEL_OPTION) {
290                 return;
291             } else if (response == JOptionPane.NO_OPTION) {
292                 closeFrame();
293                 closeLogWriter();
294             } else {
295                 stopServer();
296                 closeLogWriter();
297
298             }
299         }
300     }
301
302     private void closeLogWriter() {
303
304                     if (logWriter != null) {
305                    logWriter.flush();
306                    logWriter.close();
307                 }
308
309     }
310
311     private void init(String JavaDoc[] args) {
312         boolean showFrame = true;
313         Properties JavaDoc appProps = new Properties JavaDoc();
314
315         // readin and remove noframe flag
316
Vector JavaDoc argVector = new Vector JavaDoc();
317
318         for (int i = 0; i < args.length; i++) {
319             if (args[i].equalsIgnoreCase("-noframe")) {
320                 showFrame = false;
321             } else if (args[i].equalsIgnoreCase("-debug")) {
322                 LaunchApp.debug = true;
323             } else {
324                 argVector.addElement(args[i]);
325             }
326         }
327         bootArgs = (String JavaDoc[]) argVector.toArray(bootArgs);
328         argVector.removeAllElements();
329
330         //
331
appProps = readAppProperties();
332         initHome(appProps);
333         initConfFile(appProps);
334         initConfDir(appProps);
335         initPolicy(appProps);
336         initBrowser(appProps);
337         File JavaDoc logFile = new File JavaDoc(getLogFilePath());
338
339         if (logFile.exists()) {
340             logFile.delete();
341             try {
342                 logFile.createNewFile();
343             } catch (IOException JavaDoc e) {
344
345                 // ignore
346
}
347         }
348         if (showFrame) {
349             SwingUtil.setLookAndFeelToSystem();
350             frame = new LaunchFrame();
351             frame.initPaths();
352             System.setSecurityManager(null);
353             frame.show();
354         } else {
355             startServer();
356         }
357     }
358
359     private void initHome(Properties JavaDoc appProps) {
360         File JavaDoc f = null;
361
362         // Try extracting enhydra home from runtime directory
363
// which should be <enhydra home>/bin or
364
// <enhydra home>/lib
365
f = new File JavaDoc("."); // <enhydra home>/lib
366
f = new File JavaDoc(f.getAbsolutePath());
367         setHome(f.getParentFile().getParent());
368
369         // BOOTSTRAP
370
if (isHomeValid()) {
371             return;
372         } else if (bootArgs.length > 0) {
373             f = new File JavaDoc(bootArgs[0]);
374             setHome(f.getParentFile().getParent());
375         }
376
377         // POLICY
378
if (isHomeValid()) {
379             return;
380         } else if (getPolicy() != null) {
381             f = new File JavaDoc(getPolicy());
382             setHome(f.getParentFile().getParent());
383         }
384
385         // CONF
386
if (isHomeValid()) {
387             return;
388         } else if (getConfDir() != null) {
389             f = new File JavaDoc(getConfDir());
390             setHome(f.getParent());
391         }
392
393         // APP PROPS
394
if (isHomeValid()) {
395             return;
396         } else if (appProps.getProperty(PROPERTY_HOME) != null) {
397             f = new File JavaDoc(appProps.getProperty(PROPERTY_HOME));
398             setHome(f.getAbsolutePath());
399         }
400     }
401
402     private boolean isHomeValid() {
403         boolean valid = false;
404         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
405         File JavaDoc f = null;
406
407         path.append(getHome());
408         path.append(File.separator);
409         path.append("lib");
410         path.append(File.separator);
411         path.append("enhydra.jar");
412         f = new File JavaDoc(path.toString());
413         valid = f.isFile();
414         f = null;
415         return valid;
416     }
417
418     private void initConfDir(Properties JavaDoc appProps) {
419         if (getConfDir() == null) {
420             if (appProps.getProperty(PROPERTY_CONF_DIR) == null) {
421                 File JavaDoc dir = null;
422
423                 dir = new File JavaDoc(getHome() + File.separator + "conf");
424                 setConfDir(dir.getAbsolutePath());
425                 dir = null;
426             } else {
427                 setConfDir(appProps.getProperty(PROPERTY_CONF_DIR));
428             }
429         }
430     }
431
432     private void initPolicy(Properties JavaDoc appProps) {
433         if (getPolicy() == null) {
434             if (appProps.getProperty(PROPERTY_POLICY) == null) {
435                 File JavaDoc policyFile = null;
436                 StringBuffer JavaDoc path = new StringBuffer JavaDoc();
437
438                 path.append(getHome());
439                 path.append(File.separator);
440                 path.append("bin");
441                 path.append(File.separator);
442                 path.append("java.policy");
443                 policyFile = new File JavaDoc(path.toString());
444                 if (!policyFile.isFile()) {
445                     policyFile = createPolicyFile();
446                 }
447                 setPolicy(policyFile.getAbsolutePath());
448             } else {
449                 setPolicy(appProps.getProperty(PROPERTY_POLICY));
450             }
451         }
452     }
453
454     private void initBrowser(Properties JavaDoc appProps) {
455         if (getBrowser() == null) {
456             if (appProps.getProperty(PROPERTY_BROWSER) != null) {
457                 setBrowser(appProps.getProperty(PROPERTY_BROWSER));
458             }
459         }
460     }
461
462     private File JavaDoc createPolicyFile() {
463         File JavaDoc file = null;
464         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
465
466         path.append(System.getProperty(PROPERTY_USER_HOME));
467         path.append("java.policy");
468         file = new File JavaDoc(path.toString());
469         if (!file.exists()) {
470             try {
471                 file = new File JavaDoc(path.toString());
472                 file.getParentFile().mkdirs();
473                 file.createNewFile();
474                 PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
475
476                 writer.println("grant {");
477                 writer.println("permission java.security.AllPermission;");
478                 writer.println("};");
479                 writer.flush();
480                 writer.close();
481                 file = null;
482                 file = new File JavaDoc(path.toString());
483             } catch (java.io.IOException JavaDoc e) {
484                 e.printStackTrace();
485             }
486         }
487         return file;
488     }
489
490     private void initConfFile(Properties JavaDoc appProps) {
491         if (bootArgs.length == 0) {
492             if (appProps.getProperty(PROPERTY_CONF_FILE) == null) {
493                 StringBuffer JavaDoc path = new StringBuffer JavaDoc();
494
495                 path.append(getHome());
496                 path.append(File.separator);
497                 path.append("bin");
498                 path.append(File.separator);
499                 path.append("bootstrap.conf");
500                 setConfFile(path.toString());
501             } else {
502                 setConfFile(appProps.getProperty(PROPERTY_CONF_FILE));
503             }
504         }
505     }
506
507     protected String JavaDoc getLogFilePath() {
508         StringBuffer JavaDoc logPath = null;
509
510         logPath = new StringBuffer JavaDoc();
511         logPath.append(getHome());
512         logPath.append(File.separator);
513         logPath.append("logs");
514         logPath.append(File.separator);
515         logPath.append("launcher.txt");
516         return logPath.toString();
517     }
518
519     protected PrintWriter JavaDoc getLogWriter() throws IOException JavaDoc {
520       if (logWriter == null) {
521         File JavaDoc logFile = null;
522         logFile = new File JavaDoc(getLogFilePath());
523          if (!logFile.getParentFile().exists()) {
524              logFile.getParentFile().mkdirs();
525          }
526          logFile = null;
527          logWriter =
528                  new PrintWriter JavaDoc(new FileWriter JavaDoc(getLogFilePath(),
529                                                 true));
530       }
531       return logWriter;
532     }
533
534     private String JavaDoc getAppPropertyPath() {
535         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
536
537         buf.append(System.getProperty("user.home"));
538         buf.append(File.separator);
539         buf.append(".enhydra");
540         buf.append(File.separator);
541         buf.append("launcher.properties");
542         return buf.toString();
543     }
544
545     private boolean isFile(String JavaDoc path) {
546         boolean is = false;
547
548         if (path != null) {
549             File JavaDoc file = new File JavaDoc(path);
550
551             is = file.isFile() && file.canRead();
552         }
553         return is;
554     }
555
556     private boolean isDir(String JavaDoc path) {
557         boolean is = false;
558
559         if (path != null) {
560             File JavaDoc file = new File JavaDoc(path);
561
562             is = file.isDirectory();
563         }
564         return is;
565     }
566
567     private Properties JavaDoc readAppProperties() {
568         Properties JavaDoc props = new Properties JavaDoc();
569         File JavaDoc file = new File JavaDoc(getAppPropertyPath());
570
571         if (file.exists()) {
572             try {
573                 FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
574
575                 props.load(in);
576             } catch (Exception JavaDoc e) {
577                 e.printStackTrace();
578                 props = new Properties JavaDoc();
579             }
580         }
581         return props;
582     }
583
584     protected void storeAppProperties() {
585         File JavaDoc file = null;
586         FileOutputStream JavaDoc out = null;
587         Properties JavaDoc prop = new Properties JavaDoc();
588
589         if (isDir(getHome())) {
590             prop.setProperty(PROPERTY_HOME, getHome());
591         }
592         if (isDir(getConfDir())) {
593             prop.setProperty(PROPERTY_CONF_DIR, getConfDir());
594         }
595         if (isFile(getConfFile())) {
596             prop.setProperty(PROPERTY_CONF_FILE, getConfFile());
597         }
598         if (isFile(getPolicy())) {
599             prop.setProperty(PROPERTY_POLICY, getPolicy());
600         }
601         if (isFile(getBrowser())) {
602             prop.setProperty(PROPERTY_BROWSER, getBrowser());
603         }
604         file = new File JavaDoc(getAppPropertyPath());
605         file.getParentFile().mkdirs();
606         try {
607             out = new FileOutputStream JavaDoc(file);
608             prop.store(out, "Enhydra Launch Utility Properties");
609         } catch (IOException JavaDoc e) {
610             e.printStackTrace();
611         }
612         file = null;
613     }
614
615     class BootThread extends Thread JavaDoc {
616         private boolean fresh = true;
617
618         public void run() {
619             try {
620                 if (fresh && (frame != null)) {
621                     frame.refreshStatus("Launched server", null, true);
622                 }
623                 Bootstrap.main(bootArgs);
624                 while (fresh) {
625                     sleep(5000);
626                     if (Kernel.getKernel() == null) {
627                         if (fresh && (frame != null)) {
628                             frame.refreshStatus("Server not present", "",
629                                                 false);
630                         }
631                     } else {
632                         if (fresh && (frame != null)) {
633                             frame.refreshStatus("Server "
634                                                 + Kernel.getKernel().getState(),
635                                                 "", false);
636                         }
637                     }
638                 }
639             } catch (Exception JavaDoc e) {
640                 if (fresh && (frame != null)) {
641                     frame.refreshStatus("Failed to start server",
642                                         e.getMessage(), false);
643                 }
644             } catch (Error JavaDoc e) {
645                 if (fresh && (frame != null)) {
646                     frame.refreshStatus("Failed to start server",
647                                         e.getMessage(), false);
648                 }
649             }
650         }
651
652         protected void degrade() {
653             fresh = false;
654             if (frame != null) {
655                 frame.refreshStatus("Shutting down server", null, false);
656             }
657             closeFrame();
658             if (isServerRunning()) {
659                 try {
660                     Kernel.getKernel().shutdownHard();
661                 } catch (Exception JavaDoc e) {
662                     e.printStackTrace();
663                 }
664             }
665         }
666
667     }
668     class KillerThread extends Thread JavaDoc {
669         private int seconds = 10;
670
671         public KillerThread(int s) {
672             seconds = s;
673         }
674
675         public void run() {
676             try {
677                 sleep(seconds * 1000);
678             } catch (Exception JavaDoc e) {
679
680                 // ignore
681
}
682             closeLogWriter();
683             System.setSecurityManager(null);
684             System.exit(0);
685         }
686
687     }
688 }
689
Popular Tags