1 16 package org.mortbay.jetty.servlet; 17 18 import java.io.Externalizable ; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.Serializable ; 22 import java.security.PermissionCollection ; 23 import java.util.Collections ; 24 import java.util.EventListener ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import javax.servlet.ServletContextEvent ; 29 import javax.servlet.ServletContextListener ; 30 31 import org.apache.commons.logging.Log; 32 import org.mortbay.log.LogFactory; 33 import org.mortbay.http.HttpException; 34 import org.mortbay.http.HttpHandler; 35 import org.mortbay.http.HttpRequest; 36 import org.mortbay.http.HttpResponse; 37 import org.mortbay.http.UserRealm; 38 import org.mortbay.jetty.Server; 39 import org.mortbay.util.JarResource; 40 import org.mortbay.util.LazyList; 41 import org.mortbay.util.Loader; 42 import org.mortbay.util.MultiException; 43 import org.mortbay.util.Resource; 44 45 46 63 public class WebApplicationContext extends ServletHttpContext implements Externalizable 64 { 65 private static Log log= LogFactory.getLog(WebApplicationContext.class); 66 67 68 private String _defaultsDescriptor= "org/mortbay/jetty/servlet/webdefault.xml"; 69 private String _war; 70 private boolean _extract; 71 private boolean _ignorewebjetty; 72 private boolean _distributable; 73 private Configuration[] _configurations; 74 private String [] _configurationClassNames; 75 76 private transient Map _resourceAliases; 77 private transient Resource _webApp; 78 private transient Resource _webInf; 79 private transient WebApplicationHandler _webAppHandler; 80 private transient Object _contextListeners; 81 private transient Map _errorPages; 82 83 84 86 public WebApplicationContext() 87 { 88 } 89 90 91 94 public WebApplicationContext(String webApp) 95 { 96 _war= webApp; 97 } 98 99 100 public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException 101 { 102 out.writeObject(getContextPath()); 103 out.writeObject(getVirtualHosts()); 104 HttpHandler[] handlers= getHandlers(); 105 for (int i= 0; i < handlers.length; i++) 106 { 107 if (handlers[i] instanceof WebApplicationHandler) 108 break; 109 out.writeObject(handlers[i]); 110 } 111 out.writeObject(getAttributes()); 112 out.writeBoolean(isRedirectNullPath()); 113 out.writeInt(getMaxCachedFileSize()); 114 out.writeInt(getMaxCacheSize()); 115 out.writeBoolean(getStatsOn()); 116 out.writeObject(getPermissions()); 117 out.writeBoolean(isClassLoaderJava2Compliant()); 118 119 out.writeObject(_defaultsDescriptor); 120 out.writeObject(_war); 121 out.writeBoolean(_extract); 122 out.writeBoolean(_ignorewebjetty); 123 out.writeBoolean(_distributable); 124 125 out.writeObject(_configurationClassNames); 126 } 127 128 129 public void readExternal(java.io.ObjectInput in) throws java.io.IOException , ClassNotFoundException 130 { 131 setContextPath((String )in.readObject()); 132 setVirtualHosts((String [])in.readObject()); 133 Object o= in.readObject(); 134 135 while (o instanceof HttpHandler) 136 { 137 addHandler((HttpHandler)o); 138 o= in.readObject(); 139 } 140 setAttributes((Map )o); 141 setRedirectNullPath(in.readBoolean()); 142 setMaxCachedFileSize(in.readInt()); 143 setMaxCacheSize(in.readInt()); 144 setStatsOn(in.readBoolean()); 145 setPermissions((PermissionCollection )in.readObject()); 146 setClassLoaderJava2Compliant(in.readBoolean()); 147 148 _defaultsDescriptor= (String )in.readObject(); 149 _war= (String )in.readObject(); 150 _extract= in.readBoolean(); 151 _ignorewebjetty= in.readBoolean(); 152 _distributable= in.readBoolean(); 153 _configurationClassNames=(String [])in.readObject(); 154 } 155 156 157 158 159 public void setConfigurationClassNames (String [] configurationClassNames) 160 { 161 if (null != configurationClassNames) 162 { 163 _configurationClassNames = new String [configurationClassNames.length]; 164 System.arraycopy (configurationClassNames, 0, _configurationClassNames, 0, configurationClassNames.length); 165 } 166 } 167 168 169 public String [] getConfigurationClassNames () 170 { 171 return _configurationClassNames; 172 } 173 174 175 178 public void setWAR(String war) 179 { 180 _war= war; 181 } 182 183 184 public String getWAR() 185 { 186 return _war; 187 } 188 189 190 public WebApplicationHandler getWebApplicationHandler() 191 { 192 if (_webAppHandler == null) 193 getServletHandler(); 194 return _webAppHandler; 195 } 196 197 198 private void resolveWebApp() throws IOException 199 { 200 if (_webApp == null && _war != null && _war.length() > 0) 201 { 202 _webApp= Resource.newResource(_war); 204 205 if (_webApp.getAlias() != null) 207 { 208 log.info(_webApp + " anti-aliased to " + _webApp.getAlias()); 209 _webApp= Resource.newResource(_webApp.getAlias()); 210 } 211 212 if (log.isDebugEnabled()) 213 log.debug( 214 "Try webapp=" + _webApp + ", exists=" + _webApp.exists() + ", directory=" + _webApp.isDirectory()); 215 216 if (_webApp.exists() && !_webApp.isDirectory() && !_webApp.toString().startsWith("jar:")) 218 { 219 Resource jarWebApp= Resource.newResource("jar:" + _webApp + "!/"); 221 if (jarWebApp.exists() && jarWebApp.isDirectory()) 222 { 223 _webApp= jarWebApp; 224 _war= _webApp.toString(); 225 if (log.isDebugEnabled()) 226 log.debug( 227 "Try webapp=" 228 + _webApp 229 + ", exists=" 230 + _webApp.exists() 231 + ", directory=" 232 + _webApp.isDirectory()); 233 } 234 } 235 236 if (_webApp.exists() 238 && (!_webApp.isDirectory() 239 || (_extract && _webApp.getFile() == null) 240 || (_extract && _webApp.getFile() != null && !_webApp.getFile().isDirectory()))) 241 { 242 File tempDir= new File (getTempDirectory(), "webapp"); 244 if (tempDir.exists()) 245 tempDir.delete(); 246 tempDir.mkdir(); 247 tempDir.deleteOnExit(); 248 log.info("Extract " + _war + " to " + tempDir); 249 JarResource.extract(_webApp, tempDir, true); 250 _webApp= Resource.newResource(tempDir.getCanonicalPath()); 251 252 if (log.isDebugEnabled()) 253 log.debug( 254 "Try webapp=" 255 + _webApp 256 + ", exists=" 257 + _webApp.exists() 258 + ", directory=" 259 + _webApp.isDirectory()); 260 } 261 262 if (!_webApp.exists() || !_webApp.isDirectory()) 264 { 265 log.warn("Web application not found " + _war); 266 throw new java.io.FileNotFoundException (_war); 267 } 268 269 if (log.isDebugEnabled()) 270 log.debug("webapp=" + _webApp); 271 272 _webInf= _webApp.addPath("WEB-INF/"); 274 if (!_webInf.exists() || !_webInf.isDirectory()) 275 _webInf= null; 276 else 277 { 278 Resource work= _webInf.addPath("work"); 280 if (work.exists() 281 && work.isDirectory() 282 && work.getFile() != null 283 && work.getFile().canWrite() 284 && getAttribute("javax.servlet.context.tempdir") == null) 285 setAttribute("javax.servlet.context.tempdir", work.getFile()); 286 } 287 288 super.setBaseResource(_webApp); 290 } 291 } 292 293 294 295 public Resource getWebInf() throws IOException 296 { 297 if (_webInf==null) 298 resolveWebApp(); 299 return _webInf; 300 } 301 302 303 309 public synchronized ServletHandler getServletHandler() 310 { 311 if (_webAppHandler == null) 312 { 313 _webAppHandler= (WebApplicationHandler)getHandler(WebApplicationHandler.class); 314 if (_webAppHandler == null) 315 { 316 if (getHandler(ServletHandler.class) != null) 317 throw new IllegalStateException ("Cannot have ServletHandler in WebApplicationContext"); 318 _webAppHandler= new WebApplicationHandler(); 319 addHandler(_webAppHandler); 320 } 321 } 322 return _webAppHandler; 323 } 324 325 326 public void setPermissions(PermissionCollection permissions) 327 { 328 if (!_ignorewebjetty) 329 log.warn("Permissions set with web-jetty.xml enabled"); 330 super.setPermissions(permissions); 331 } 332 333 334 public boolean isIgnoreWebJetty() 335 { 336 return _ignorewebjetty; 337 } 338 339 340 344 public void setIgnoreWebJetty(boolean b) 345 { 346 _ignorewebjetty= b; 347 if (b && getPermissions() != null) 348 log.warn("Permissions set with web-jetty.xml enabled"); 349 } 350 351 352 public boolean isDistributable() 353 { 354 return _distributable; 355 } 356 357 358 public void setDistributable(boolean distributable) 359 { 360 _distributable=distributable; 361 } 362 363 364 public Configuration[] getConfigurations () 365 { 366 return _configurations; 367 } 368 369 370 protected Configuration[] loadConfigurations() throws Exception 371 { 372 String [] names = _configurationClassNames; 373 374 if (null==names) 376 names = ((Server)getHttpServer()).getWebApplicationConfigurationClassNames(); 377 378 if (null!=names) 379 { 380 Object [] nullArgs = new Object [0]; 382 Configuration[] configurations = new Configuration[names.length]; 383 for (int i=0; i< names.length; i++) 384 { 385 configurations[i] = 386 (Configuration)Loader.loadClass(WebApplicationContext.class, names[i]).getConstructors()[0].newInstance(nullArgs); 387 if (log.isDebugEnabled()){log.debug("Loaded instance of "+names[i]);}; 388 } 389 return configurations; 390 } 391 else 392 return new Configuration[0]; 393 } 394 395 396 protected void configureClassPath() throws Exception 397 { 398 for (int i=0; i<_configurations.length;i++) 401 { 402 _configurations[i].setWebApplicationContext(this); 403 _configurations[i].configureClassPath(); 404 } 405 } 406 407 408 protected void configureDefaults() throws Exception 409 { 410 for (int i=0;i<_configurations.length;i++) 412 { 413 _configurations[i].setWebApplicationContext(this); 414 _configurations[i].configureDefaults(); 415 } 416 } 417 418 419 protected void configureWebApp () throws Exception 420 { 421 for (int i=0;i<_configurations.length;i++) 423 { 424 _configurations[i].setWebApplicationContext(this); 425 _configurations[i].configureWebApp(); 426 } 427 428 } 429 430 431 434 protected void doStart() throws Exception 435 { 436 if (isStarted()) 437 return; 438 439 Thread thread= Thread.currentThread(); 441 ClassLoader lastContextLoader= thread.getContextClassLoader(); 442 443 MultiException mex= null; 444 try 445 { 446 resolveWebApp(); 448 449 getServletHandler(); 451 452 _configurations=loadConfigurations(); 453 454 configureClassPath(); 456 initClassLoader(true); 457 thread.setContextClassLoader(getClassLoader()); 458 initialize(); 459 460 configureDefaults(); 462 463 Map.Entry entry= _webAppHandler.getHolderEntry("test.jsp"); 465 if (entry != null) 466 { 467 ServletHolder jspHolder= (ServletHolder)entry.getValue(); 468 if (jspHolder != null && jspHolder.getInitParameter("classpath") == null) 469 { 470 String fileClassPath= getFileClassPath(); 471 jspHolder.setInitParameter("classpath", fileClassPath); 472 if (log.isDebugEnabled()) 473 log.debug("Set classpath=" + fileClassPath + " for " + jspHolder); 474 } 475 } 476 477 configureWebApp(); 479 480 _webAppHandler.setAutoInitializeServlets(false); 482 483 super.doStart(); 485 486 mex= new MultiException(); 487 if (_contextListeners != null && _webAppHandler != null) 489 { 490 ServletContextEvent event= new ServletContextEvent (getServletContext()); 491 for (int i= 0; i < LazyList.size(_contextListeners); i++) 492 { 493 try 494 { 495 ((ServletContextListener )LazyList.get(_contextListeners, i)).contextInitialized(event); 496 } 497 catch (Exception ex) 498 { 499 mex.add(ex); 500 } 501 } 502 } 503 504 if (_webAppHandler != null && _webAppHandler.isStarted()) 506 { 507 try 508 { 509 _webAppHandler.initializeServlets(); 510 } 511 catch (Exception ex) 512 { 513 mex.add(ex); 514 } 515 } 516 } 517 catch (Exception e) 518 { 519 log.warn("Configuration error on " + _war, e); 520 throw e; 521 } 522 finally 523 { 524 thread.setContextClassLoader(lastContextLoader); 525 } 526 527 if (mex != null) 528 mex.ifExceptionThrow(); 529 } 530 531 532 537 protected void doStop() throws Exception 538 { 539 MultiException mex=new MultiException(); 540 541 542 Thread thread= Thread.currentThread(); 543 ClassLoader lastContextLoader= thread.getContextClassLoader(); 544 545 try 546 { 547 if (_contextListeners != null) 549 { 550 if (_webAppHandler != null) 551 { 552 ServletContextEvent event= new ServletContextEvent (getServletContext()); 553 554 for (int i= LazyList.size(_contextListeners); i-- > 0;) 555 { 556 try 557 { 558 ((ServletContextListener )LazyList.get(_contextListeners, i)).contextDestroyed(event); 559 } 560 catch (Exception e) 561 { 562 mex.add(e); 563 } 564 } 565 } 566 } 567 _contextListeners= null; 568 569 try 571 { 572 super.doStop(); 573 } 574 catch (Exception e) 575 { 576 mex.add(e); 577 } 578 579 clearSecurityConstraints(); 581 582 if (_webAppHandler != null) 583 removeHandler(_webAppHandler); 584 _webAppHandler= null; 585 586 if (_errorPages != null) 587 _errorPages.clear(); 588 _errorPages= null; 589 590 _webApp=null; 591 _webInf=null; 592 593 _configurations=null; 594 595 } 596 finally 597 { 598 thread.setContextClassLoader(lastContextLoader); 599 } 600 601 if (mex!=null) 602 mex.ifExceptionThrow(); 603 } 604 605 606 607 public void destroy() 608 { 609 super.destroy(); 610 if (isStarted()) 611 throw new IllegalStateException (); 612 613 _defaultsDescriptor=null; 614 _war=null; 615 _configurationClassNames=null; 616 if (_resourceAliases!=null) 617 _resourceAliases.clear(); 618 _resourceAliases=null; 619 _contextListeners=null; 620 if (_errorPages!=null) 621 _errorPages.clear(); 622 _errorPages=null; 623 } 624 625 626 public void handle(String pathInContext, String pathParams, HttpRequest httpRequest, HttpResponse httpResponse) 627 throws HttpException, IOException 628 { 629 if (!isStarted()) 630 return; 631 try 632 { 633 super.handle(pathInContext, pathParams, httpRequest, httpResponse); 634 } 635 finally 636 { 637 if (!httpRequest.isHandled()) 638 httpResponse.sendError(HttpResponse.__404_Not_Found); 639 httpRequest.setHandled(true); 640 if (!httpResponse.isCommitted()) 641 { 642 httpResponse.completing(); 643 httpResponse.commit(); 644 } 645 } 646 } 647 648 649 public synchronized void addEventListener(EventListener listener) throws IllegalArgumentException 650 { 651 if (listener instanceof ServletContextListener ) 652 { 653 _contextListeners= LazyList.add(_contextListeners, listener); 654 } 655 656 super.addEventListener(listener); 657 } 658 659 660 public synchronized void removeEventListener(EventListener listener) 661 { 662 _contextListeners= LazyList.remove(_contextListeners, listener); 663 super.removeEventListener(listener); 664 } 665 666 667 public String getDisplayName() 668 { 669 return getHttpContextName(); 670 } 671 672 673 public void setDisplayName(String name) 674 { 675 setHttpContextName(name); 676 } 677 678 679 686 public void setDefaultsDescriptor(String defaults) 687 { 688 _defaultsDescriptor= defaults; 689 } 690 691 692 public String getDefaultsDescriptor() 693 { 694 return _defaultsDescriptor; 695 } 696 697 698 702 public void setExtractWAR(boolean extract) 703 { 704 _extract= extract; 705 } 706 707 708 public boolean getExtractWAR() 709 { 710 return _extract; 711 } 712 713 714 721 protected void initialize() throws Exception 722 { 723 } 724 725 726 727 protected UserRealm getUserRealm(String name) 728 { 729 return getHttpServer().getRealm(name); 730 } 731 732 733 public String toString() 734 { 735 String name = getDisplayName(); 736 return "WebApplicationContext[" + getContextPath() + "," + (name == null ? _war : name) + "]"; 737 } 738 739 740 747 public void setResourceAlias(String alias, String uri) 748 { 749 if (_resourceAliases == null) 750 _resourceAliases= new HashMap (5); 751 _resourceAliases.put(alias, uri); 752 } 753 754 755 public Map getResourceAliases() 756 { 757 if (_resourceAliases == null) 758 return null; 759 return Collections.unmodifiableMap(_resourceAliases); 760 } 761 762 763 public String getResourceAlias(String alias) 764 { 765 if (_resourceAliases == null) 766 return null; 767 return (String )_resourceAliases.get(alias); 768 } 769 770 771 public String removeResourceAlias(String alias) 772 { 773 if (_resourceAliases == null) 774 return null; 775 return (String )_resourceAliases.remove(alias); 776 } 777 778 779 public Resource getResource(String uriInContext) throws IOException 780 { 781 IOException ioe= null; 782 Resource resource= null; 783 try 784 { 785 resource= super.getResource(uriInContext); 786 if (resource != null && resource.exists()) 787 return resource; 788 } 789 catch (IOException e) 790 { 791 ioe= e; 792 } 793 794 String aliasedUri= getResourceAlias(uriInContext); 795 if (aliasedUri != null) 796 return super.getResource(aliasedUri); 797 798 if (ioe != null) 799 throw ioe; 800 801 return resource; 802 } 803 804 805 810 public void setErrorPage(String error, String uriInContext) 811 { 812 if (_errorPages == null) 813 _errorPages= new HashMap (); 814 _errorPages.put(error, uriInContext); 815 } 816 817 818 823 public String getErrorPage(String error) 824 { 825 if (_errorPages == null) 826 return null; 827 return (String )_errorPages.get(error); 828 } 829 830 831 public String removeErrorPage(String error) 832 { 833 if (_errorPages == null) 834 return null; 835 return (String )_errorPages.remove(error); 836 } 837 838 839 840 841 849 public static interface Configuration extends Serializable 850 { 851 852 855 public void setWebApplicationContext (WebApplicationContext context); 856 857 858 861 public WebApplicationContext getWebApplicationContext (); 862 863 864 871 public void configureClassPath() 872 throws Exception ; 873 874 875 881 public void configureDefaults() 882 throws Exception ; 883 884 885 886 891 public void configureWebApp() 892 throws Exception ; 893 894 } 895 896 } 897 | Popular Tags |