1 2 24 package org.enhydra.tool.boot; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 29 import java.io.*; 31 import java.awt.*; 32 import java.awt.event.*; 33 import java.util.ArrayList ; 34 import java.util.Enumeration ; 35 import java.util.Properties ; 36 import java.net.URLClassLoader ; 37 38 39 public class StartServer extends Frame { 41 String [] args = new String [0]; 42 Button button = new Button(); 43 Process process = null; 44 45 public static void main(String [] 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)); 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 e) { 71 if (e.getID() == java.awt.event.WindowEvent.WINDOW_CLOSING) { 72 process.destroy(); 73 System.exit(0); 74 } 75 } 76 77 private String createCommandLine() { 78 79 80 StringBuffer buf = new StringBuffer (); 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 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 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 printLine = null; 149 StringBuffer buf = new StringBuffer (); 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 e) { 163 System.err.println(e.getMessage()); 164 } 165 } 166 167 private String getBootClassPath() { 168 StringBuffer buf = new StringBuffer (); 169 String 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 getCurrentClassPath() { 187 StringBuffer buf = new StringBuffer (); 188 ClassLoader loader = ClassLoader.getSystemClassLoader(); 189 URLClassLoader urlLoader = (URLClassLoader ) loader; 190 String path = new String (); 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 getVMProperties() { 211 212 Properties props = System.getProperties(); 213 Enumeration names = System.getProperties().propertyNames(); 214 StringBuffer buf = new StringBuffer (); 215 ArrayList list = new ArrayList (); 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 name = names.nextElement().toString(); 223 String 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 getUserDir(String bootProp) { 247 File boot = new File(bootProp); 248 StringBuffer buf = new StringBuffer (); 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 |