1 package com.teamkonzept.webman; 2 3 import java.io.*; 4 import java.net.*; 5 6 import com.teamkonzept.lib.*; 7 import com.teamkonzept.web.TKEvent; 8 import com.teamkonzept.web.TKHttpInterface; 9 import org.apache.log4j.Category; 10 11 20 public class ProcessStarter 21 { 22 23 24 private static Category cat = Category.getInstance(ProcessStarter.class.getName()); 25 26 33 private final static int SOCKET_TIME_OUT = 1800000; 34 35 36 private PrintWriter out; 37 private PrintWriter err; 38 39 43 public final static String KEEP_ALIVE_TAG = "-- keep alive --"; 44 45 50 public ProcessStarter (PrintWriter out, PrintWriter err) 51 { 52 this.out = out; 53 this.err = err; 54 } 55 56 65 public boolean start(String startClassname, String arguments, String addClasspath) 66 { 67 Runtime rt = Runtime.getRuntime(); 68 Process proc = null; 69 ServerSocket server = null; 70 try 71 { 72 server = new ServerSocket(0); 74 int port = server.getLocalPort(); 75 String startCmd = genStartCmd(startClassname, arguments + " port=" +port, addClasspath); 76 ProcessStarter.cat.debug(startCmd); 77 proc = rt.exec(startCmd); 78 Socket socket = server.accept(); 79 socket.setSoTimeout(SOCKET_TIME_OUT); 81 InputStream in = socket.getInputStream(); 82 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 83 String line; 84 while ((line = reader.readLine()) != null) 85 { 86 if (!line.endsWith(KEEP_ALIVE_TAG)) { 87 out.println(line); 88 out.flush(); 89 } 90 91 98 } 99 106 } 107 catch (Exception e) 108 { 109 ProcessStarter.cat.error("Processstarter.start", e); 110 } 111 finally 112 { 113 try 114 { 115 server.close(); 116 } 117 catch (Exception e) 118 { 119 ProcessStarter.cat.warn("Processstarter.start.finally", e); 120 } 121 } 122 try { 123 if (proc != null) 124 proc.waitFor(); 125 } 126 catch (InterruptedException e) 127 { 128 ProcessStarter.cat.warn("Processstarter.start.waitfor", e); 129 } 130 131 int exitValue = proc.exitValue(); 132 133 proc.destroy(); 134 135 return exitValue == 0; 136 } 137 138 139 146 public static String getWMClasspath(String docRoot) 148 { 149 String pathSeparator = System.getProperty("path.separator"); 150 String servletDir = pathSeparator + docRoot + File.separator + 151 "WEB-INF" + File.separator + "classes"; 152 String addPath = ""; 154 String libDir = docRoot + "WEB-INF" + File.separator + "lib"; 155 File d = new File(libDir); 156 libDir = pathSeparator + libDir + File.separator; 157 if ( d.isDirectory() ) 158 { 159 String [] list = d.list(); 160 for ( int i = 0; i < list.length; i++ ) 161 if ( list[i].endsWith(".jar") 162 || list[i].endsWith(".zip") ) 163 addPath += libDir + list[i]; 164 } 165 String privateDir = docRoot + "WEB-INF" + File.separator + "lib" + File.separator + "webman-only"; 167 libDir = pathSeparator + privateDir + File.separator; 168 d = new File(privateDir); 169 if ( d.isDirectory() ) 170 { 171 String [] list = d.list(); 172 for ( int i = 0; i < list.length; i++ ) 173 if ( list[i].endsWith(".jar") 174 || list[i].endsWith(".zip") ) 175 addPath += libDir + list[i]; 176 } 177 return servletDir + addPath; 178 } 179 180 185 protected String genStartCmd(String startClassname, String arguments, 186 String addClasspath) 187 { 188 String javaHome = System.getProperty("java.home"); 189 String javaProg = javaHome + File.separator + "bin" + File.separator + "java"; 190 String javaClasspath = System.getProperty("java.class.path") + addClasspath; 191 192 String memory = getMemoryProps(); 194 String cmd = javaProg + memory + " -classpath " + javaClasspath + " " + startClassname + " " + arguments; 195 196 String os = System.getProperty("os.name"); 197 if (os.equalsIgnoreCase("Windows NT")) 198 { 199 cmd = "cmd.exe /c " + cmd; 200 } 201 ProcessStarter.cat.debug(cmd); 202 return cmd; 203 } 204 205 private String getMemoryProps() 206 { 207 try 208 { 209 PropertyManager man = PropertyManager.getPropertyManager("MEMORY_EXTERNAL"); 210 String initial = man.getValue("INITIAL_HEAP", null); 211 String max = man.getValue("MAX_HEAP", null); 212 String back = " "; 213 if (initial != null) 214 back += "-Xms" + initial + " "; 215 if (max != null) 216 back += "-Xmx" + max + " "; 217 return back; 218 } 219 catch (Exception e) 220 { 221 ProcessStarter.cat.info("getMemoryProps(): exception caught"); 222 } 223 return ""; 224 } 225 226 242 243 } 244 | Popular Tags |