📄 webappcontext.java
字号:
//========================================================================//$Id: WebAppContext.java,v 1.5 2005/11/16 22:02:45 gregwilkins Exp $//Copyright 2004-2006 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//Licensed under the Apache License, Version 2.0 (the "License");//you may not use this file except in compliance with the License.//You may obtain a copy of the License at //http://www.apache.org/licenses/LICENSE-2.0//Unless required by applicable law or agreed to in writing, software//distributed under the License is distributed on an "AS IS" BASIS,//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.//See the License for the specific language governing permissions and//limitations under the License.//========================================================================package org.mortbay.jetty.webapp;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.security.PermissionCollection;import java.util.EventListener;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSessionActivationListener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingListener;import javax.servlet.http.HttpSessionListener;import org.mortbay.jetty.Connector;import org.mortbay.jetty.HandlerContainer;import org.mortbay.jetty.Server;import org.mortbay.jetty.deployer.ContextDeployer;import org.mortbay.jetty.deployer.WebAppDeployer;import org.mortbay.jetty.handler.ContextHandler;import org.mortbay.jetty.handler.ContextHandlerCollection;import org.mortbay.jetty.handler.ErrorHandler;import org.mortbay.jetty.handler.HandlerCollection;import org.mortbay.jetty.security.SecurityHandler;import org.mortbay.jetty.servlet.Context;import org.mortbay.jetty.servlet.ErrorPageErrorHandler;import org.mortbay.jetty.servlet.ServletHandler;import org.mortbay.jetty.servlet.SessionHandler;import org.mortbay.log.Log;import org.mortbay.resource.JarResource;import org.mortbay.resource.Resource;import org.mortbay.util.IO;import org.mortbay.util.LazyList;import org.mortbay.util.Loader;import org.mortbay.util.StringUtil;import org.mortbay.util.URIUtil;import org.mortbay.util.UrlEncoded;/* ------------------------------------------------------------ *//** Web Application Context Handler. * The WebAppContext handler is an extension of ContextHandler that * coordinates the construction and configuration of nested handlers: * {@link org.mortbay.jetty.security.SecurityHandler}, {@link org.mortbay.jetty.servlet.SessionHandler} * and {@link org.mortbay.jetty.servlet.ServletHandler}. * The handlers are configured by pluggable configuration classes, with * the default being {@link org.mortbay.jetty.webapp.WebXmlConfiguration} and * {@link org.mortbay.jetty.webapp.JettyWebXmlConfiguration}. * * @org.apache.xbean.XBean description="Creates a servlet web application at a given context from a resource base" * * @author gregw * */public class WebAppContext extends Context{ public final static String WEB_DEFAULTS_XML="org/mortbay/jetty/webapp/webdefault.xml"; public final static String ERROR_PAGE="org.mortbay.jetty.error_page"; private static String[] __dftConfigurationClasses = { "org.mortbay.jetty.webapp.WebInfConfiguration", "org.mortbay.jetty.webapp.WebXmlConfiguration", "org.mortbay.jetty.webapp.JettyWebXmlConfiguration", "org.mortbay.jetty.webapp.TagLibConfiguration" } ; private String[] _configurationClasses=__dftConfigurationClasses; private Configuration[] _configurations; private String _defaultsDescriptor=WEB_DEFAULTS_XML; private String _descriptor=null; private String _overrideDescriptor=null; private boolean _distributable=false; private boolean _extractWAR=true; private boolean _copyDir=false; private boolean _logUrlOnStart =false; private boolean _parentLoaderPriority= Boolean.getBoolean("org.mortbay.jetty.webapp.parentLoaderPriority"); private PermissionCollection _permissions; private String[] _systemClasses = {"java.","javax.servlet.","javax.xml.","org.mortbay.","org.xml.","org.w3c.", "org.apache.commons.logging.", "org.apache.log4j."}; private String[] _serverClasses = {"-org.mortbay.jetty.plus.jaas.", "org.mortbay.jetty.", "org.slf4j."}; // TODO hide all mortbay classes private File _tmpDir; private boolean _isExistingTmpDir; private String _war; private String _extraClasspath; private Throwable _unavailableException; private transient Map _resourceAliases; private transient boolean _ownClassLoader=false; private transient boolean _unavailable; public static ContextHandler getCurrentWebAppContext() { ContextHandler.SContext context=ContextHandler.getCurrentContext(); if (context!=null) { ContextHandler handler = context.getContextHandler(); if (handler instanceof WebAppContext) return (ContextHandler)handler; } return null; } /* ------------------------------------------------------------ */ /** Add Web Applications. * Add auto webapplications to the server. The name of the * webapp directory or war is used as the context name. If the * webapp matches the rootWebApp it is added as the "/" context. * @param server Must not be <code>null</code> * @param webapps Directory file name or URL to look for auto * webapplication. * @param defaults The defaults xml filename or URL which is * loaded before any in the web app. Must respect the web.dtd. * If null the default defaults file is used. If the empty string, then * no defaults file is used. * @param extract If true, extract war files * @param java2CompliantClassLoader True if java2 compliance is applied to all webapplications * @exception IOException * @deprecated use {@link org.mortbay.jetty.deployer.WebAppDeployer} or {@link org.mortbay.jetty.deployer.ContextDeployer} */ public static void addWebApplications(Server server, String webapps, String defaults, boolean extract, boolean java2CompliantClassLoader) throws IOException { addWebApplications(server, webapps, defaults, __dftConfigurationClasses, extract, java2CompliantClassLoader); } /* ------------------------------------------------------------ */ /** Add Web Applications. * Add auto webapplications to the server. The name of the * webapp directory or war is used as the context name. If the * webapp matches the rootWebApp it is added as the "/" context. * @param server Must not be <code>null</code>. * @param webapps Directory file name or URL to look for auto * webapplication. * @param defaults The defaults xml filename or URL which is * loaded before any in the web app. Must respect the web.dtd. * If null the default defaults file is used. If the empty string, then * no defaults file is used. * @param configurations Array of classnames of {@link Configuration} implementations to apply. * @param extract If true, extract war files * @param java2CompliantClassLoader True if java2 compliance is applied to all webapplications * @exception IOException * @throws IllegalAccessException * @throws InstantiationException * @deprecated use {@link org.mortbay.jetty.deployer.WebAppDeployer} or {@link org.mortbay.jetty.deployer.ContextDeployer} */ public static void addWebApplications(Server server, String webapps, String defaults, String[] configurations, boolean extract, boolean java2CompliantClassLoader) throws IOException { HandlerCollection contexts = (HandlerCollection)server.getChildHandlerByClass(ContextHandlerCollection.class); if (contexts==null) contexts = (HandlerCollection)server.getChildHandlerByClass(HandlerCollection.class); addWebApplications(contexts,webapps,defaults,configurations,extract,java2CompliantClassLoader); } /* ------------------------------------------------------------ */ /** Add Web Applications. * Add auto webapplications to the server. The name of the * webapp directory or war is used as the context name. If the * webapp is called "root" it is added as the "/" context. * @param contexts A HandlerContainer to which the contexts will be added * @param webapps Directory file name or URL to look for auto * webapplication. * @param defaults The defaults xml filename or URL which is * loaded before any in the web app. Must respect the web.dtd. * If null the default defaults file is used. If the empty string, then * no defaults file is used. * @param configurations Array of classnames of {@link Configuration} implementations to apply. * @param extract If true, extract war files * @param java2CompliantClassLoader True if java2 compliance is applied to all webapplications * @exception IOException * @throws IllegalAccessException * @throws InstantiationException * @deprecated use {@link WebAppDeployer} or {@link ContextDeployer} */ public static void addWebApplications(HandlerContainer contexts, String webapps, String defaults, boolean extract, boolean java2CompliantClassLoader) throws IOException { addWebApplications(contexts, webapps, defaults, __dftConfigurationClasses, extract, java2CompliantClassLoader); } /* ------------------------------------------------------------ */ /** Add Web Applications. * Add auto webapplications to the server. The name of the * webapp directory or war is used as the context name. If the * webapp is called "root" it is added as the "/" context. * @param contexts A HandlerContainer to which the contexts will be added * @param webapps Directory file name or URL to look for auto * webapplication. * @param defaults The defaults xml filename or URL which is * loaded before any in the web app. Must respect the web.dtd. * If null the default defaults file is used. If the empty string, then * no defaults file is used. * @param configurations Array of classnames of {@link Configuration} implementations to apply. * @param extract If true, extract war files * @param java2CompliantClassLoader True if java2 compliance is applied to all webapplications * @exception IOException * @throws IllegalAccessException * @throws InstantiationException * @deprecated use {@link WebAppDeployer} or {@link ContextDeployer} */ public static void addWebApplications(HandlerContainer contexts, String webapps, String defaults, String[] configurations, boolean extract, boolean java2CompliantClassLoader) throws IOException { Log.warn("Deprecated configuration used for "+webapps); WebAppDeployer deployer = new WebAppDeployer(); deployer.setContexts(contexts); deployer.setWebAppDir(webapps); deployer.setConfigurationClasses(configurations); deployer.setExtract(extract); deployer.setParentLoaderPriority(java2CompliantClassLoader); try { deployer.start(); } catch(IOException e) { throw e; } catch(Exception e) { throw new RuntimeException(e); } } /* ------------------------------------------------------------ */ public WebAppContext() { this(null,null,null,null); } /* ------------------------------------------------------------ */ /** * @param contextPath The context path * @param webApp The URL or filename of the webapp directory or war file. */ public WebAppContext(String webApp,String contextPath) { super(null,contextPath,SESSIONS|SECURITY); setContextPath(contextPath); setWar(webApp); setErrorHandler(new ErrorPageErrorHandler()); } /* ------------------------------------------------------------ */ /** * @param parent The parent HandlerContainer. * @param contextPath The context path * @param webApp The URL or filename of the webapp directory or war file. */ public WebAppContext(HandlerContainer parent, String webApp, String contextPath) { super(parent,contextPath,SESSIONS|SECURITY); setWar(webApp); setErrorHandler(new ErrorPageErrorHandler()); } /* ------------------------------------------------------------ */ /** */ public WebAppContext(SecurityHandler securityHandler,SessionHandler sessionHandler, ServletHandler servletHandler, ErrorHandler errorHandler) { super(null, sessionHandler!=null?sessionHandler:new SessionHandler(), securityHandler!=null?securityHandler:new SecurityHandler(), servletHandler!=null?servletHandler:new ServletHandler(), null); setErrorHandler(errorHandler!=null?errorHandler:new ErrorPageErrorHandler()); } /* ------------------------------------------------------------ */ /** Get an exception that caused the webapp to be unavailable * @return A throwable if the webapp is unavailable or null */ public Throwable getUnavailableException() { return _unavailableException; } /* ------------------------------------------------------------ */ /** Set Resource Alias. * Resource aliases map resource uri's within a context. * They may optionally be used by a handler when looking for * a resource. * @param alias * @param uri */ public void setResourceAlias(String alias, String uri) { if (_resourceAliases == null) _resourceAliases= new HashMap(5); _resourceAliases.put(alias, uri); } /* ------------------------------------------------------------ */ public Map getResourceAliases()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -