001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance witarh the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the Licenseget.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.tool.boot;
024:
025: // ToolBox
026: import org.enhydra.tool.ToolBoxInfo;
027:
028: // JDK
029: import java.io.*;
030: import java.awt.*;
031: import java.awt.event.*;
032: import java.util.ArrayList;
033: import java.util.Enumeration;
034: import java.util.Properties;
035: import java.net.URLClassLoader;
036:
037: //
038: public class StartServer extends Frame {
039: String[] args = new String[0];
040: Button button = new Button();
041: Process process = null;
042:
043: public static void main(String[] args) {
044: StartServer starter = new StartServer();
045:
046: starter.args = args;
047: starter.setSize(200, 100);
048: starter.setLocation(new Point(200, 200));
049: starter.show();
050: starter.setLocation(new Point(200, 200)); // reset for Linux AWT
051: starter.createProcess();
052: }
053:
054: public StartServer() {
055: button.setLabel("Shutdown Server");
056: button.addActionListener(new ActionListener() {
057: public void actionPerformed(ActionEvent e) {
058: process.destroy();
059: System.exit(0);
060: }
061:
062: });
063: add(button);
064: enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK);
065: setTitle("Application Server");
066: }
067:
068: protected void processWindowEvent(java.awt.event.WindowEvent e) {
069: if (e.getID() == java.awt.event.WindowEvent.WINDOW_CLOSING) {
070: process.destroy();
071: System.exit(0);
072: }
073: }
074:
075: private String createCommandLine() {
076:
077: StringBuffer buf = new StringBuffer();
078: File java = new File(System.getProperty("java.home"));
079:
080: buf.append(java.getParentFile());
081: buf.append(File.separator + "bin" + File.separator + "java");
082: if (isWindows()) {
083: buf.append("w");
084: }
085: buf.append(' ');
086: buf.append("-classpath");
087: buf.append(' ');
088: if (isWindows()) {
089: buf.append('"');
090: }
091: //Zeljko Kovacevic comment this 30.10 2003
092: // if (ToolBoxInfo.isEnhydra3()) {
093: buf.append(getCurrentClassPath());
094: /* } else {
095: buf.append(getBootClassPath());
096: }
097: */
098: if (isWindows()) {
099: buf.append('"');
100: }
101: buf.append(' ');
102: buf.append(getVMProperties());
103: buf.append(' ');
104: buf.append(ToolBoxInfo.getEnhydraMainClass());
105: for (int i = 0; i < args.length; i++) {
106: buf.append(' ');
107: if (isWindows()) {
108: buf.append('"');
109: }
110: buf.append(args[i].replace('\\', '/'));
111: if (isWindows()) {
112: buf.append('"');
113: }
114: }
115: return buf.toString();
116: }
117:
118: private void createProcess() {
119: String commandLine = createCommandLine();
120: boolean running = true;
121:
122: try {
123: System.out.println(commandLine);
124: process = Runtime.getRuntime().exec(commandLine);
125: BufferedReader errorReader = new BufferedReader(
126: new InputStreamReader(process.getErrorStream()));
127: BufferedReader inputReader = new BufferedReader(
128: new InputStreamReader(process.getInputStream()));
129:
130: while (running) {
131: try {
132: int exit = process.exitValue();
133:
134: System.out.println("Server shutdown: " + exit);
135: running = false;
136: } catch (Exception e) {
137: print(errorReader);
138: print(inputReader);
139: }
140: }
141: } catch (IOException e) {
142: e.printStackTrace(System.err);
143: }
144: }
145:
146: private void print(BufferedReader reader) throws IOException {
147: String printLine = null;
148: StringBuffer buf = new StringBuffer();
149: int count = 0;
150:
151: while (reader.ready()) {
152: buf.append((char) reader.read());
153: count++;
154: if (count == 1000) {
155: break;
156: }
157: }
158: System.out.print(buf.toString());
159: try {
160: Thread.sleep(250);
161: } catch (InterruptedException e) {
162: System.err.println(e.getMessage());
163: }
164: }
165:
166: private String getBootClassPath() {
167: StringBuffer buf = new StringBuffer();
168: String eRoot = ToolBoxInfo.getEnhydraRoot();
169:
170: if (isWindows()) {
171: buf.append('"');
172: }
173: buf.append(File.pathSeparatorChar);
174: buf.append(eRoot);
175: buf.append("/lib/Boot.jar");
176: buf.append(File.pathSeparatorChar);
177: buf.append(eRoot);
178: buf.append("/lib/EAAL.jar");
179: if (isWindows()) {
180: buf.append('"');
181: }
182: return buf.toString();
183: }
184:
185: private String getCurrentClassPath() {
186: StringBuffer buf = new StringBuffer();
187: ClassLoader loader = ClassLoader.getSystemClassLoader();
188: URLClassLoader urlLoader = (URLClassLoader) loader;
189: String path = new String();
190:
191: buf.append(File.pathSeparatorChar);
192: for (int i = 0; i < urlLoader.getURLs().length; i++) {
193: path = urlLoader.getURLs()[i].getFile().replace('/',
194: File.separatorChar);
195: if (path.charAt(0) == '\\') {
196: path = path.substring(1);
197: if (path.endsWith(File.separator)) {
198: path = path.substring(0, path.length() - 1);
199: }
200: }
201: buf.append(path);
202: if ((i + 1) < urlLoader.getURLs().length) {
203: buf.append(File.pathSeparatorChar);
204: }
205: }
206: return buf.toString();
207: }
208:
209: private String getVMProperties() {
210:
211: Properties props = System.getProperties();
212: Enumeration names = System.getProperties().propertyNames();
213: StringBuffer buf = new StringBuffer();
214: ArrayList list = new ArrayList();
215:
216: list.add("tc_path_add");
217: list.add("java.security.policy");
218: list.add("org.enhydra.boot.properties");
219: list.trimToSize();
220: while (names.hasMoreElements()) {
221: String name = names.nextElement().toString();
222: String value = props.getProperty(name);
223:
224: if (list.contains(name)) {
225: buf.append("-D");
226: buf.append(name);
227: buf.append('=');
228: if (isWindows()) {
229: buf.append('"');
230: }
231: buf.append(value.replace('\\', '/'));
232: if (isWindows()) {
233: buf.append('"');
234: }
235: buf.append(' ');
236: if (name.equals("org.enhydra.boot.properties")) {
237: buf.append(getUserDir(value));
238: }
239: }
240: }
241: list.clear();
242: return buf.toString();
243: }
244:
245: private String getUserDir(String bootProp) {
246: File boot = new File(bootProp);
247: StringBuffer buf = new StringBuffer();
248:
249: System.setProperty("user.dir", boot.getParent());
250: buf.append("-D");
251: buf.append("user.dir");
252: buf.append('=');
253: if (isWindows()) {
254: buf.append('"');
255: }
256: buf.append(boot.getParent().replace('\\', '/'));
257: buf.append('/');
258: if (isWindows()) {
259: buf.append('"');
260: }
261: buf.append(' ');
262: return buf.toString();
263: }
264:
265: private boolean isWindows() {
266: return (File.separatorChar == '\\');
267: }
268:
269: }
|