KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > boot > StartServer


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

24 package org.enhydra.tool.boot;
25
26 // ToolBox
27
import org.enhydra.tool.ToolBoxInfo;
28
29 // JDK
30
import java.io.*;
31 import java.awt.*;
32 import java.awt.event.*;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.net.URLClassLoader JavaDoc;
37
38
39 //
40
public class StartServer extends Frame {
41     String JavaDoc[] args = new String JavaDoc[0];
42     Button button = new Button();
43     Process JavaDoc process = null;
44
45     public static void main(String JavaDoc[] args) {
46         StartServer starter = new StartServer();
47
48         starter.args = args;
49         starter.setSize(200, 100);
50         starter.setLocation(new Point(200, 200));
51         starter.show();
52         starter.setLocation(new Point(200, 200)); // reset for Linux AWT
53
starter.createProcess();
54     }
55
56     public StartServer() {
57         button.setLabel("Shutdown Server");
58         button.addActionListener(new ActionListener() {
59             public void actionPerformed(ActionEvent e) {
60                 process.destroy();
61                 System.exit(0);
62             }
63
64         });
65         add(button);
66         enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK);
67         setTitle("Application Server");
68     }
69
70     protected void processWindowEvent(java.awt.event.WindowEvent JavaDoc e) {
71         if (e.getID() == java.awt.event.WindowEvent.WINDOW_CLOSING) {
72             process.destroy();
73             System.exit(0);
74         }
75     }
76
77     private String JavaDoc createCommandLine() {
78
79
80         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
81         File java = new File(System.getProperty("java.home"));
82
83         buf.append(java.getParentFile());
84         buf.append(File.separator + "bin" + File.separator + "java");
85         if (isWindows()) {
86             buf.append("w");
87         }
88         buf.append(' ');
89         buf.append("-classpath");
90         buf.append(' ');
91         if (isWindows()) {
92            buf.append('"');
93         }
94         if (ToolBoxInfo.isEnhydra3()) {
95             buf.append(getCurrentClassPath());
96         } else {
97             buf.append(getBootClassPath());
98         }
99         if (isWindows()) {
100            buf.append('"');
101         }
102         buf.append(' ');
103         buf.append(getVMProperties());
104         buf.append(' ');
105         buf.append(ToolBoxInfo.getEnhydraMainClass());
106         for (int i = 0; i < args.length; i++) {
107             buf.append(' ');
108             if (isWindows()) {
109                buf.append('"');
110             }
111             buf.append(args[i].replace('\\','/'));
112             if (isWindows()) {
113                buf.append('"');
114             }
115         }
116         return buf.toString();
117     }
118
119     private void createProcess() {
120         String JavaDoc commandLine = createCommandLine();
121         boolean running = true;
122
123         try {
124             System.out.println(commandLine);
125             process = Runtime.getRuntime().exec(commandLine);
126             BufferedReader errorReader =
127                 new BufferedReader(new InputStreamReader(process.getErrorStream()));
128             BufferedReader inputReader =
129                 new BufferedReader(new InputStreamReader(process.getInputStream()));
130
131             while (running) {
132                 try {
133                     int exit = process.exitValue();
134
135                     System.out.println("Server shutdown: " + exit);
136                     running = false;
137                 } catch (Exception JavaDoc e) {
138                     print(errorReader);
139                     print(inputReader);
140                 }
141             }
142         } catch (IOException e) {
143             e.printStackTrace(System.err);
144         }
145     }
146
147     private void print(BufferedReader reader) throws IOException {
148         String JavaDoc printLine = null;
149         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
150         int count = 0;
151
152         while (reader.ready()) {
153             buf.append((char) reader.read());
154             count++;
155             if (count == 1000) {
156                 break;
157             }
158         }
159         System.out.print(buf.toString());
160         try {
161             Thread.sleep(250);
162         } catch (InterruptedException JavaDoc e) {
163             System.err.println(e.getMessage());
164         }
165     }
166
167     private String JavaDoc getBootClassPath() {
168         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
169         String JavaDoc eRoot = ToolBoxInfo.getEnhydraRoot();
170
171         if (isWindows()) {
172           buf.append('"');
173         }
174         buf.append(File.pathSeparatorChar);
175         buf.append(eRoot);
176         buf.append("/lib/Boot.jar");
177         buf.append(File.pathSeparatorChar);
178         buf.append(eRoot);
179         buf.append("/lib/EAAL.jar");
180         if (isWindows()) {
181           buf.append('"');
182         }
183         return buf.toString();
184     }
185
186     private String JavaDoc getCurrentClassPath() {
187         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
188         ClassLoader JavaDoc loader = ClassLoader.getSystemClassLoader();
189         URLClassLoader JavaDoc urlLoader = (URLClassLoader JavaDoc) loader;
190         String JavaDoc path = new String JavaDoc();
191
192         buf.append(File.pathSeparatorChar);
193         for (int i = 0; i < urlLoader.getURLs().length; i++) {
194             path = urlLoader.getURLs()[i].getFile().replace('/',
195                     File.separatorChar);
196             if (path.charAt(0) == '\\') {
197                 path = path.substring(1);
198                 if (path.endsWith(File.separator)) {
199                     path = path.substring(0, path.length() - 1);
200                 }
201             }
202             buf.append(path);
203             if ((i + 1) < urlLoader.getURLs().length) {
204                 buf.append(File.pathSeparatorChar);
205             }
206         }
207         return buf.toString();
208     }
209
210     private String JavaDoc getVMProperties() {
211
212         Properties JavaDoc props = System.getProperties();
213         Enumeration JavaDoc names = System.getProperties().propertyNames();
214         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
215         ArrayList JavaDoc list = new ArrayList JavaDoc();
216
217         list.add("tc_path_add");
218         list.add("java.security.policy");
219         list.add("org.enhydra.boot.properties");
220         list.trimToSize();
221         while (names.hasMoreElements()) {
222             String JavaDoc name = names.nextElement().toString();
223             String JavaDoc value = props.getProperty(name);
224
225             if (list.contains(name)) {
226                 buf.append("-D");
227                 buf.append(name);
228                 buf.append('=');
229                 if (isWindows()) {
230                    buf.append('"');
231                 }
232                 buf.append(value.replace('\\','/'));
233                 if (isWindows()) {
234                    buf.append('"');
235                 }
236                 buf.append(' ');
237                 if (name.equals("org.enhydra.boot.properties")) {
238                     buf.append(getUserDir(value));
239                 }
240             }
241         }
242         list.clear();
243         return buf.toString();
244     }
245
246     private String JavaDoc getUserDir(String JavaDoc bootProp) {
247         File boot = new File(bootProp);
248         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
249
250         System.setProperty("user.dir", boot.getParent());
251         buf.append("-D");
252         buf.append("user.dir");
253         buf.append('=');
254         if (isWindows()) {
255           buf.append('"');
256         }
257         buf.append(boot.getParent().replace('\\','/'));
258         buf.append('/');
259         if (isWindows()) {
260           buf.append('"');
261         }
262         buf.append(' ');
263         return buf.toString();
264     }
265
266     private boolean isWindows() {
267       return (File.separatorChar == '\\');
268     }
269
270 }
271
Popular Tags