1 22 package org.jboss.test.util.server; 23 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.InputStreamReader ; 29 import java.io.PrintWriter ; 30 import java.net.HttpURLConnection ; 31 import java.net.Socket ; 32 import java.net.URL ; 33 import java.net.URLConnection ; 34 35 41 public abstract class ServerController 42 { 43 44 private static final String SHUTDOWN_CLASS = "org.jboss.Shutdown"; 45 46 private static final String MAIN = "org.jboss.Main"; 47 48 private ServerController() 49 { 50 } 51 52 59 public static void startServer(Server server, ServerManager manager) throws IOException 60 { 61 if (server.isRunning()) 62 { 63 throw new IllegalArgumentException ("The " + server.getName() + " server is already running."); 64 } 65 66 if (isServerStarted(server)) 67 { 68 throw new IOException ("Found a process already listening on:" + server.getHttpUrl() + " or "+ server.getRmiUrl()); 69 } 70 71 String execCmd = getStartCommandLine(server, manager); 72 73 System.out.println("Starting server \"" + server.getName() + "\" with command: \n" + execCmd); 74 75 File binDir = new File (manager.getJBossHome(), "/bin"); 76 final Process process = Runtime.getRuntime().exec(execCmd, null, binDir); 77 78 final BufferedReader errStream = new BufferedReader (new InputStreamReader (process.getErrorStream())); 79 final BufferedReader inStream = new BufferedReader (new InputStreamReader (process.getInputStream())); 80 81 final File outFile = server.getOutputLog(); 82 initalizeLog(outFile); 83 final PrintWriter outlog = new PrintWriter (new FileWriter (outFile)); 84 server.setOutWriter(outlog); 85 86 Thread outPump = new OutputPumper(inStream, outlog); 87 outPump.start(); 88 89 final File errorFile = server.getErrorLog(); 90 initalizeLog(errorFile); 91 final PrintWriter errorlog = new PrintWriter (new FileWriter (errorFile)); 92 server.setErrorWriter(errorlog); 93 94 Thread errorPump = new OutputPumper(errStream, errorlog); 95 errorPump.start(); 96 97 106 server.setProcess(process); 107 108 try 109 { 110 waitForServer(server, manager); 111 } 112 catch (IOException e) 113 { 114 server.setProcess(null); 115 throw e; 116 } 117 118 } 119 120 125 private static void initalizeLog(final File logFile) throws IOException 126 { 127 if (logFile.exists()) 128 { 129 logFile.delete(); 130 } 131 if (!logFile.getParentFile().exists()) 132 { 133 logFile.getParentFile().mkdir(); 134 } 135 136 logFile.createNewFile(); 137 } 138 139 147 private static String getStartCommandLine(Server server, ServerManager manager) throws IOException 148 { 149 String execCmd = manager.getJavaExecutable() + " -cp " + manager.getStartClasspath() + " "; 150 execCmd = execCmd + server.getJvmArgs() + server.getSysProperties(); 151 execCmd = execCmd + " " + MAIN + " -c " + server.getConfig() + " -b " + server.getHost(); 152 153 if (manager.getUdpGroup() != null && ! manager.getUdpGroup().equals("")) 154 { 155 execCmd = execCmd + " -u " + manager.getUdpGroup(); 156 } 157 execCmd = execCmd + " " + server.getArgs(); 158 return execCmd; 159 } 160 161 167 private static void waitForServer(Server server, ServerManager manager) throws IOException 168 { 169 170 int tries = 0; 171 while (tries++ < manager.getStartupTimeout()) 172 { 173 if (!server.isRunning()) 174 { 175 throw new IOException ("Server failed to start; see logs. exit code: " + server.getProcess().exitValue()); 176 } 177 178 try 179 { 180 Thread.sleep(1000); 181 } 182 catch (InterruptedException e) 183 { 184 } 185 if (isServerStarted(server)) 186 { 187 return; 188 } 189 } 190 throw new IOException ("Server failed to start; see logs."); 191 192 } 193 194 202 private static boolean isServerStarted(Server server) throws IOException 203 { 204 URL url = server.getHttpUrl(); 205 if (server.hasWebServer()) 206 { 207 try 208 { 209 URLConnection conn = url.openConnection(); 210 if (conn instanceof HttpURLConnection ) 211 { 212 HttpURLConnection http = (HttpURLConnection ) conn; 213 int responseCode = http.getResponseCode(); 214 215 if (responseCode > 0 && responseCode < 400) 216 { 217 return true; 218 } 219 } 220 } 221 catch (java.io.IOException e) 222 { 223 return false; 224 } 225 return false; 226 } 227 else 228 { 229 Socket socket = null; 231 try 232 { 233 socket = new Socket (server.getHost(), server.getRmiPort().intValue()); 234 return true; 235 } 236 catch (IOException e) 237 { 238 return false; 239 } 240 finally 241 { 242 if (socket != null) 243 { 244 socket.close(); 245 } 246 } 247 } 248 } 249 250 258 public static void stopServer(Server server, ServerManager manager) throws IOException 259 { 260 if (!server.isRunning()) 261 { 262 throw new IllegalArgumentException ("The " + server.getName() + " is not running; it cannot be stopped."); 263 } 264 265 System.out.println("Shutting down server: " + server.getName()); 266 267 String shutdownCmd = getStopCommandLine(server, manager); 268 System.out.println("Shutting down server: " + shutdownCmd); 269 270 Runtime.getRuntime().exec(shutdownCmd); 271 272 Process process = server.getProcess(); 273 if (!waitOnShutdown(server, manager)) 274 { 275 System.err.println("Failed to shutdown server \"" + server.getName() 276 + "\" before timeout. Destroying the process."); 277 process.destroy(); 278 } 279 280 closeAllStreams(process); 281 server.getErrorWriter().close(); 282 server.getOutWriter().close(); 283 284 server.setProcess(null); 285 try 286 { 287 Thread.sleep(45000); 288 } 289 catch (InterruptedException e) 290 { 291 } 292 } 293 294 300 private static boolean waitOnShutdown(Server server, ServerManager manager) 301 { 302 int shutdownTimeout = manager.getShutdownTimeout(); 303 System.out.println("shutdownTimeout will be="+shutdownTimeout); 304 for (int tries = 0; tries < shutdownTimeout; tries++) 305 { 306 try 307 { 308 if (!server.isRunning()) 309 { 310 return true; 311 } 312 Thread.sleep(1000); 313 } 314 catch (InterruptedException e) 315 { 316 } 317 } 318 319 return false; 320 } 321 322 330 private static String getStopCommandLine(Server server, ServerManager manager) throws IOException 331 { 332 String execCmd = manager.getJavaExecutable() + " -cp " + manager.getStopClasspath() + " "; 333 execCmd = execCmd + SHUTDOWN_CLASS + " --server " + server.getRmiUrl(); 334 execCmd = execCmd + " --shutdown"; 335 return execCmd; 336 } 337 338 343 private static void closeAllStreams(Process process) 344 { 345 try 346 { 347 process.getInputStream().close(); 348 process.getOutputStream().close(); 349 process.getErrorStream().close(); 350 } 351 catch (IOException e) 352 { 353 } 354 } 355 356 362 private static class OutputPumper extends Thread 363 { 364 private BufferedReader outputReader; 365 366 private PrintWriter logWriter; 367 368 public OutputPumper(BufferedReader outputReader, PrintWriter logWriter) 369 { 370 this.outputReader = outputReader; 371 this.logWriter = logWriter; 372 } 373 374 public void run() 375 { 376 try 377 { 378 String line = null; 379 while ((line = outputReader.readLine()) != null) 380 { 381 logWriter.println(line); 382 } 383 } 384 catch (IOException e) 385 { 386 } 387 } 388 } 389 390 } 391 | Popular Tags |