1 20 package org.apache.cactus.integration.ant.container; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.HttpURLConnection ; 25 import java.net.URL ; 26 27 import org.apache.cactus.integration.ant.util.AntLog; 28 import org.apache.commons.logging.Log; 29 import org.apache.tools.ant.BuildException; 30 31 37 public final class ContainerRunner 38 { 39 41 44 private Container container; 45 46 49 private URL testURL; 50 51 55 private long timeout = 180000; 56 57 60 private long checkInterval = 500; 61 62 65 private long shutDownWait = 2000; 66 67 70 private boolean alreadyRunning; 71 72 76 private String serverName; 77 78 81 private transient Log log = AntLog.NULL; 82 83 85 90 public ContainerRunner(Container theContainer) 91 { 92 this.container = theContainer; 93 } 94 95 97 103 public String getServerName() 104 { 105 return this.serverName; 106 } 107 108 117 public void startUpContainer() throws IllegalStateException 118 { 119 if (this.testURL == null) 120 { 121 throw new IllegalStateException ("Property [url] must be set"); 122 } 123 124 this.alreadyRunning = isAvailable(testConnectivity(this.testURL)); 127 if (this.alreadyRunning) 128 { 129 this.log.debug("Server is already running"); 132 return; 133 } 134 135 Thread thread = new Thread (new Runnable () 137 { 138 public void run() 139 { 140 container.startUp(); 141 } 142 }); 143 thread.start(); 144 145 long startTime = System.currentTimeMillis(); 148 int responseCode = -1; 149 do 150 { 151 if ((System.currentTimeMillis() - startTime) > this.timeout) 152 { 153 throw new BuildException("Failed to start the container after " 154 + "more than [" + this.timeout + "] ms. Trying to connect " 155 + "to the [" + this.testURL + "] test URL yielded a [" 156 + responseCode + "] error code. Please run in debug mode " 157 + "for more details about the error."); 158 } 159 sleep(this.checkInterval); 160 this.log.debug("Checking if server is up ..."); 161 responseCode = testConnectivity(this.testURL); 162 } while (!isAvailable(responseCode)); 163 164 sleep(this.container.getStartUpWait()); 166 167 this.serverName = retrieveServerName(this.testURL); 168 this.log.trace("Server [" + this.serverName + "] started"); 169 } 170 171 179 public void shutDownContainer() throws IllegalStateException 180 { 181 if (this.testURL == null) 182 { 183 throw new IllegalStateException ("Property [url] must be set"); 184 } 185 186 if (this.alreadyRunning) 188 { 189 return; 190 } 191 192 if (!isAvailable(testConnectivity(this.testURL))) 193 { 194 this.log.debug("Server isn't running!"); 195 return; 196 } 197 198 Thread thread = new Thread (new Runnable () 201 { 202 public void run() 203 { 204 container.shutDown(); 205 } 206 }); 207 thread.start(); 208 209 do 211 { 212 sleep(this.checkInterval); 213 } while (isAvailable(testConnectivity(this.testURL))); 214 215 sleep(this.shutDownWait); 217 218 this.log.debug("Server stopped!"); 219 } 220 221 228 public void setCheckInterval(long theCheckInterval) 229 { 230 this.checkInterval = theCheckInterval; 231 } 232 233 238 public void setLog(Log theLog) 239 { 240 this.log = theLog; 241 } 242 243 250 public void setShutDownWait(long theShutDownWait) 251 { 252 this.shutDownWait = theShutDownWait; 253 } 254 255 262 public void setTimeout(long theTimeout) 263 { 264 this.timeout = theTimeout; 265 } 266 267 273 public void setURL(URL theTestURL) 274 { 275 if (!theTestURL.getProtocol().equals("http")) 276 { 277 throw new IllegalArgumentException ("Not a HTTP URL"); 278 } 279 this.testURL = theTestURL; 280 } 281 282 284 292 private int testConnectivity(URL theUrl) 293 { 294 int code; 295 try 296 { 297 HttpURLConnection connection = 298 (HttpURLConnection ) theUrl.openConnection(); 299 connection.setRequestProperty("Connection", "close"); 300 connection.connect(); 301 readFully(connection); 302 connection.disconnect(); 303 code = connection.getResponseCode(); 304 } 305 catch (IOException e) 306 { 307 this.log.debug("Failed to connect to [" + theUrl + "]", e); 308 code = -1; 309 } 310 return code; 311 } 312 313 314 322 private boolean isAvailable(int theCode) 323 { 324 boolean result; 325 if ((theCode != -1) && (theCode < 300)) 326 { 327 result = true; 328 } 329 else 330 { 331 result = false; 332 } 333 return result; 334 } 335 336 343 private String retrieveServerName(URL theUrl) 344 { 345 String retVal = null; 346 try 347 { 348 HttpURLConnection connection = 349 (HttpURLConnection ) theUrl.openConnection(); 350 connection.connect(); 351 retVal = connection.getHeaderField("Server"); 352 connection.disconnect(); 353 } 354 catch (IOException e) 355 { 356 this.log.debug("Could not get server name from [" 357 + theUrl + "]", e); 358 } 359 return retVal; 360 } 361 362 369 static void readFully(HttpURLConnection theConnection) 370 throws IOException 371 { 372 if (theConnection.getContentLength() != 0) 378 { 379 byte[] buf = new byte[256]; 380 InputStream in = theConnection.getInputStream(); 381 while (in.read(buf) != -1) 382 { 383 } 385 } 386 } 387 388 394 private void sleep(long theMs) throws BuildException 395 { 396 try 397 { 398 Thread.sleep(theMs); 399 } 400 catch (InterruptedException e) 401 { 402 throw new BuildException("Interruption during sleep", e); 403 } 404 } 405 } 406 | Popular Tags |