1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.webapp; 20 21 import java.io.IOException; 22 import java.net.MalformedURLException; 23 24 import org.eclipse.jetty.servlet.ErrorPageErrorHandler; 25 import org.eclipse.jetty.servlet.ServletHandler; 26 import org.eclipse.jetty.util.log.Log; 27 import org.eclipse.jetty.util.log.Logger; 28 import org.eclipse.jetty.util.resource.Resource; 29 30 /* ------------------------------------------------------------------------------- */ 31 /** 32 * Configure by parsing default web.xml and web.xml 33 * 34 */ 35 public class WebXmlConfiguration extends AbstractConfiguration 36 { 37 private static final Logger LOG = Log.getLogger(WebXmlConfiguration.class); 38 39 40 /* ------------------------------------------------------------------------------- */ 41 /** 42 * 43 */ 44 @Override preConfigure(WebAppContext context)45 public void preConfigure (WebAppContext context) throws Exception 46 { 47 //parse webdefault.xml 48 String defaultsDescriptor = context.getDefaultsDescriptor(); 49 if (defaultsDescriptor != null && defaultsDescriptor.length() > 0) 50 { 51 Resource dftResource = Resource.newSystemResource(defaultsDescriptor); 52 if (dftResource == null) 53 dftResource = context.newResource(defaultsDescriptor); 54 context.getMetaData().setDefaults (dftResource); 55 } 56 57 //parse, but don't process web.xml 58 Resource webxml = findWebXml(context); 59 if (webxml != null) 60 { 61 context.getMetaData().setWebXml(webxml); 62 context.getServletContext().setEffectiveMajorVersion(context.getMetaData().getWebXml().getMajorVersion()); 63 context.getServletContext().setEffectiveMinorVersion(context.getMetaData().getWebXml().getMinorVersion()); 64 } 65 66 //parse but don't process override-web.xml 67 for (String overrideDescriptor : context.getOverrideDescriptors()) 68 { 69 if (overrideDescriptor != null && overrideDescriptor.length() > 0) 70 { 71 Resource orideResource = Resource.newSystemResource(overrideDescriptor); 72 if (orideResource == null) 73 orideResource = context.newResource(overrideDescriptor); 74 context.getMetaData().addOverride(orideResource); 75 } 76 } 77 } 78 79 /* ------------------------------------------------------------------------------- */ 80 /** 81 * Process web-default.xml, web.xml, override-web.xml 82 * 83 */ 84 @Override configure(WebAppContext context)85 public void configure (WebAppContext context) throws Exception 86 { 87 // cannot configure if the context is already started 88 if (context.isStarted()) 89 { 90 LOG.debug("Cannot configure webapp after it is started"); 91 return; 92 } 93 94 context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor()); 95 } 96 97 /* ------------------------------------------------------------------------------- */ findWebXml(WebAppContext context)98 protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException 99 { 100 String descriptor = context.getDescriptor(); 101 if (descriptor != null) 102 { 103 Resource web = context.newResource(descriptor); 104 if (web.exists() && !web.isDirectory()) return web; 105 } 106 107 Resource web_inf = context.getWebInf(); 108 if (web_inf != null && web_inf.isDirectory()) 109 { 110 // do web.xml file 111 Resource web = web_inf.addPath("web.xml"); 112 if (web.exists()) return web; 113 if (LOG.isDebugEnabled()) 114 LOG.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only"); 115 } 116 return null; 117 } 118 119 120 /* ------------------------------------------------------------------------------- */ 121 @Override deconfigure(WebAppContext context)122 public void deconfigure (WebAppContext context) throws Exception 123 { 124 ServletHandler _servletHandler = context.getServletHandler(); 125 126 context.setWelcomeFiles(null); 127 128 if (context.getErrorHandler() instanceof ErrorPageErrorHandler) 129 ((ErrorPageErrorHandler) 130 context.getErrorHandler()).setErrorPages(null); 131 132 133 // TODO remove classpaths from classloader 134 135 } 136 } 137