1 16 package org.mortbay.jetty; 17 18 import java.io.IOException ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.net.URL ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 import org.mortbay.http.HttpContext; 29 import org.mortbay.http.HttpServer; 30 import org.mortbay.jetty.servlet.ServletHttpContext; 31 import org.mortbay.jetty.servlet.WebApplicationContext; 32 import org.mortbay.util.LogSupport; 33 import org.mortbay.util.Resource; 34 import org.mortbay.xml.XmlConfiguration; 35 36 37 38 59 public class Server extends HttpServer 60 { 61 static Log log = LogFactory.getLog(Server.class); 62 private String [] _webAppConfigurationClassNames = 63 new String []{"org.mortbay.jetty.servlet.XMLConfiguration", "org.mortbay.jetty.servlet.JettyWebConfiguration"}; 64 private String _configuration; 65 private String _rootWebApp; 66 private static ShutdownHookThread hookThread = new ShutdownHookThread(); 67 68 69 71 public Server() 72 { 73 } 74 75 76 80 public Server(String configuration) 81 throws IOException 82 { 83 this(Resource.newResource(configuration).getURL()); 84 } 85 86 87 91 public Server(Resource configuration) 92 throws IOException 93 { 94 this(configuration.getURL()); 95 } 96 97 98 102 public Server(URL configuration) 103 throws IOException 104 { 105 _configuration=configuration.toString(); 106 Server.hookThread.add(this); 107 try 108 { 109 XmlConfiguration config=new XmlConfiguration(configuration); 110 config.configure(this); 111 } 112 catch(IOException e) 113 { 114 throw e; 115 } 116 catch(InvocationTargetException e) 117 { 118 log.warn(LogSupport.EXCEPTION,e.getTargetException()); 119 throw new IOException ("Jetty configuration problem: "+e.getTargetException()); 120 } 121 catch(Exception e) 122 { 123 log.warn(LogSupport.EXCEPTION,e); 124 throw new IOException ("Jetty configuration problem: "+e); 125 } 126 } 127 128 129 public boolean getStopAtShutdown() 130 { 131 return hookThread.contains(this); 132 } 133 134 135 public void setStopAtShutdown(boolean stop) 136 { 137 if (stop) 138 hookThread.add(this); 139 else 140 hookThread.remove(this); 141 } 142 143 144 147 public String getRootWebApp() 148 { 149 return _rootWebApp; 150 } 151 152 153 156 public void setRootWebApp(String rootWebApp) 157 { 158 _rootWebApp = rootWebApp; 159 } 160 161 162 166 public void configure(String configuration) 167 throws IOException 168 { 169 170 URL url=Resource.newResource(configuration).getURL(); 171 if (_configuration!=null && _configuration.equals(url.toString())) 172 return; 173 if (_configuration!=null) 174 throw new IllegalStateException ("Already configured with "+_configuration); 175 try 176 { 177 XmlConfiguration config=new XmlConfiguration(url); 178 _configuration=url.toString(); 179 config.configure(this); 180 } 181 catch(IOException e) 182 { 183 throw e; 184 } 185 catch(Exception e) 186 { 187 log.warn(LogSupport.EXCEPTION,e); 188 throw new IOException ("Jetty configuration problem: "+e); 189 } 190 } 191 192 193 public String getConfiguration() 194 { 195 return _configuration; 196 } 197 198 199 206 protected HttpContext newHttpContext() 207 { 208 return new ServletHttpContext(); 209 } 210 211 212 222 protected WebApplicationContext newWebApplicationContext( 223 String webApp 224 ) 225 { 226 return new WebApplicationContext(webApp); 227 } 228 229 230 237 public WebApplicationContext addWebApplication(String contextPathSpec, 238 String webApp) 239 throws IOException 240 { 241 return addWebApplication(null,contextPathSpec,webApp); 242 } 243 244 245 253 public WebApplicationContext addWebApplication(String virtualHost, 254 String contextPathSpec, 255 String webApp) 256 throws IOException 257 { 258 WebApplicationContext appContext = 259 newWebApplicationContext(webApp); 260 appContext.setContextPath(contextPathSpec); 261 addContext(virtualHost,appContext); 262 if(log.isDebugEnabled())log.debug("Web Application "+appContext+" added"); 263 return appContext; 264 } 265 266 267 268 275 public WebApplicationContext[] addWebApplications(String webapps) 276 throws IOException 277 { 278 return addWebApplications(null,webapps,null,false); 279 } 280 281 282 290 public WebApplicationContext[] addWebApplications(String host, 291 String webapps) 292 throws IOException 293 { 294 return addWebApplications(host,webapps,null,false); 295 } 296 297 298 308 public WebApplicationContext[] addWebApplications(String host, 309 String webapps, 310 boolean extract) 311 throws IOException 312 { 313 return addWebApplications(host,webapps,null,extract); 314 } 315 316 317 331 public WebApplicationContext[] addWebApplications(String host, 332 String webapps, 333 String defaults, 334 boolean extract) 335 throws IOException 336 { 337 return addWebApplications(host,webapps,defaults,extract,true); 338 } 339 340 341 356 public WebApplicationContext[] addWebApplications(String host, 357 String webapps, 358 String defaults, 359 boolean extract, 360 boolean java2CompliantClassLoader) 361 throws IOException 362 { 363 ArrayList wacs = new ArrayList (); 364 Resource r=Resource.newResource(webapps); 365 if (!r.exists()) 366 throw new IllegalArgumentException ("No such webapps resource "+r); 367 368 if (!r.isDirectory()) 369 throw new IllegalArgumentException ("Not directory webapps resource "+r); 370 371 String [] files=r.list(); 372 373 for (int f=0;files!=null && f<files.length;f++) 374 { 375 String context=files[f]; 376 377 if (context.equalsIgnoreCase("CVS/") || 378 context.equalsIgnoreCase("CVS") || 379 context.startsWith(".")) 380 continue; 381 382 383 String app = r.addPath(r.encode(files[f])).toString(); 384 if (context.toLowerCase().endsWith(".war") || 385 context.toLowerCase().endsWith(".jar")) 386 { 387 context=context.substring(0,context.length()-4); 388 Resource unpacked=r.addPath(context); 389 if (unpacked!=null && unpacked.exists() && unpacked.isDirectory()) 390 continue; 391 } 392 393 if (_rootWebApp!=null && (context.equals(_rootWebApp)||context.equals(_rootWebApp+"/"))) 394 context="/"; 395 else 396 context="/"+context; 397 398 WebApplicationContext wac= addWebApplication(host, 399 context, 400 app); 401 wac.setExtractWAR(extract); 402 wac.setClassLoaderJava2Compliant(java2CompliantClassLoader); 403 if (defaults!=null) 404 { 405 if (defaults.length()==0) 406 wac.setDefaultsDescriptor(null); 407 else 408 wac.setDefaultsDescriptor(defaults); 409 } 410 wacs.add(wac); 411 } 412 413 return (WebApplicationContext[])wacs.toArray(new WebApplicationContext[wacs.size()]); 414 } 415 416 417 418 424 public void setWebApplicationConfigurationClassNames (String [] configurationClassNames) 425 { 426 if (configurationClassNames != null) 427 { 428 _webAppConfigurationClassNames = new String [configurationClassNames.length]; 429 System.arraycopy(configurationClassNames, 0, _webAppConfigurationClassNames, 0, configurationClassNames.length); 430 } 431 } 432 433 public String [] getWebApplicationConfigurationClassNames () 434 { 435 return _webAppConfigurationClassNames; 436 } 437 438 439 440 441 public static void main(String [] arg) 442 { 443 String [] dftConfig={"etc/jetty.xml"}; 444 445 if (arg.length==0) 446 { 447 log.info("Using default configuration: etc/jetty.xml"); 448 arg=dftConfig; 449 } 450 451 final Server[] servers=new Server[arg.length]; 452 453 for (int i=0;i<arg.length;i++) 455 { 456 try 457 { 458 servers[i] = new Server(arg[i]); 459 servers[i].setStopAtShutdown(true); 460 servers[i].start(); 461 462 } 463 catch(Exception e) 464 { 465 log.warn(LogSupport.EXCEPTION,e); 466 } 467 } 468 469 for (int i=0;i<arg.length;i++) 471 { 472 try{servers[i].join();} 473 catch (Exception e){LogSupport.ignore(log,e);} 474 } 475 } 476 477 482 private static class ShutdownHookThread extends Thread { 483 private boolean hooked = false; 484 private ArrayList servers = new ArrayList (); 485 486 490 private void createShutdownHook() { 491 if (!Boolean.getBoolean("JETTY_NO_SHUTDOWN_HOOK") && !hooked) { 492 try { 493 Method shutdownHook = java.lang.Runtime .class.getMethod("addShutdownHook", 494 new Class [] { java.lang.Thread .class }); 495 shutdownHook.invoke(Runtime.getRuntime(), new Object [] { this }); 496 this.hooked = true; 497 } catch (Exception e) { 498 if (log.isDebugEnabled()) log.debug("No shutdown hook in JVM ", e); 499 } 500 } 501 } 502 503 506 public boolean add(Server server) { 507 createShutdownHook(); 508 return this.servers.add(server); 509 } 510 511 514 public boolean contains(Server server) { 515 return this.servers.contains(server); 516 } 517 518 521 public boolean addAll(Collection c) { 522 createShutdownHook(); 523 return this.servers.addAll(c); 524 } 525 526 529 public void clear() { 530 createShutdownHook(); 531 this.servers.clear(); 532 } 533 534 537 public boolean remove(Server server) { 538 createShutdownHook(); 539 return this.servers.remove(server); 540 } 541 542 545 public boolean removeAll(Collection c) { 546 createShutdownHook(); 547 return this.servers.removeAll(c); 548 } 549 550 553 public void run() { 554 setName("Shutdown"); 555 log.info("Shutdown hook executing"); 556 Iterator it = servers.iterator(); 557 while (it.hasNext()) { 558 Server svr = (Server) it.next(); 559 if (svr == null) continue; 560 try { 561 svr.stop(); 562 } catch (Exception e) { 563 log.warn(LogSupport.EXCEPTION, e); 564 } 565 log.info("Shutdown hook complete"); 566 567 try { 569 Thread.sleep(1000); 570 } catch (Exception e) { 571 log.warn(LogSupport.EXCEPTION, e); 572 } 573 } 574 } 575 } 576 } 577 578 579 580 581 | Popular Tags |