📄 webxmlconfiguration.java
字号:
// ========================================================================// Copyright 2003-2005 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.net.URL;import java.util.ArrayList;import java.util.EventListener;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import javax.servlet.UnavailableException;import org.mortbay.jetty.Handler;import org.mortbay.jetty.handler.ContextHandler;import org.mortbay.jetty.security.Authenticator;import org.mortbay.jetty.security.BasicAuthenticator;import org.mortbay.jetty.security.ClientCertAuthenticator;import org.mortbay.jetty.security.Constraint;import org.mortbay.jetty.security.ConstraintMapping;import org.mortbay.jetty.security.DigestAuthenticator;import org.mortbay.jetty.security.FormAuthenticator;import org.mortbay.jetty.security.UserRealm;import org.mortbay.jetty.servlet.Dispatcher;import org.mortbay.jetty.servlet.ErrorPageErrorHandler;import org.mortbay.jetty.servlet.FilterHolder;import org.mortbay.jetty.servlet.FilterMapping;import org.mortbay.jetty.servlet.ServletHandler;import org.mortbay.jetty.servlet.ServletHolder;import org.mortbay.jetty.servlet.ServletMapping;import org.mortbay.log.Log;import org.mortbay.resource.Resource;import org.mortbay.util.LazyList;import org.mortbay.util.Loader;import org.mortbay.xml.XmlParser;/* ------------------------------------------------------------------------------- *//** * Configure by parsing default web.xml and web.xml * * @author gregw */public class WebXmlConfiguration implements Configuration{ protected WebAppContext _context; protected XmlParser _xmlParser; protected Object _filters; protected Object _filterMappings; protected Object _servlets; protected Object _servletMappings; protected Object _welcomeFiles; protected Object _constraintMappings; protected Object _listeners; protected Map _errorPages; protected boolean _hasJSP; protected String _jspServletName; protected String _jspServletClass; protected boolean _defaultWelcomeFileList; protected ServletHandler _servletHandler; protected int _version; public WebXmlConfiguration() { // Get parser _xmlParser=webXmlParser(); } public static XmlParser webXmlParser() { XmlParser xmlParser=new XmlParser(); //set up cache of DTDs and schemas locally URL dtd22=WebAppContext.class.getResource("/javax/servlet/resources/web-app_2_2.dtd"); URL dtd23=WebAppContext.class.getResource("/javax/servlet/resources/web-app_2_3.dtd"); URL jsp20xsd=WebAppContext.class.getResource("/javax/servlet/resources/jsp_2_0.xsd"); URL jsp21xsd=WebAppContext.class.getResource("/javax/servlet/resources/jsp_2_1.xsd"); URL j2ee14xsd=WebAppContext.class.getResource("/javax/servlet/resources/j2ee_1_4.xsd"); URL webapp24xsd=WebAppContext.class.getResource("/javax/servlet/resources/web-app_2_4.xsd"); URL webapp25xsd=WebAppContext.class.getResource("/javax/servlet/resources/web-app_2_5.xsd"); URL schemadtd=WebAppContext.class.getResource("/javax/servlet/resources/XMLSchema.dtd"); URL xmlxsd=WebAppContext.class.getResource("/javax/servlet/resources/xml.xsd"); URL webservice11xsd=WebAppContext.class.getResource("/javax/servlet/resources/j2ee_web_services_client_1_1.xsd"); URL webservice12xsd=WebAppContext.class.getResource("/javax/servlet/resources/javaee_web_services_client_1_2.xsd"); URL datatypesdtd=WebAppContext.class.getResource("/javax/servlet/resources/datatypes.dtd"); xmlParser.redirectEntity("web-app_2_2.dtd",dtd22); xmlParser.redirectEntity("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN",dtd22); xmlParser.redirectEntity("web.dtd",dtd23); xmlParser.redirectEntity("web-app_2_3.dtd",dtd23); xmlParser.redirectEntity("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",dtd23); xmlParser.redirectEntity("XMLSchema.dtd",schemadtd); xmlParser.redirectEntity("http://www.w3.org/2001/XMLSchema.dtd",schemadtd); xmlParser.redirectEntity("-//W3C//DTD XMLSCHEMA 200102//EN",schemadtd); xmlParser.redirectEntity("jsp_2_0.xsd",jsp20xsd); xmlParser.redirectEntity("http://java.sun.com/xml/ns/j2ee/jsp_2_0.xsd",jsp20xsd); xmlParser.redirectEntity("jsp_2_1.xsd",jsp21xsd); xmlParser.redirectEntity("http://java.sun.com/xml/ns/javaee/jsp_2_1.xsd",jsp21xsd); xmlParser.redirectEntity("j2ee_1_4.xsd",j2ee14xsd); xmlParser.redirectEntity("http://java.sun.com/xml/ns/j2ee/j2ee_1_4.xsd",j2ee14xsd); xmlParser.redirectEntity("web-app_2_4.xsd",webapp24xsd); xmlParser.redirectEntity("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd",webapp24xsd); xmlParser.redirectEntity("web-app_2_5.xsd",webapp25xsd); xmlParser.redirectEntity("http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd",webapp25xsd); xmlParser.redirectEntity("xml.xsd",xmlxsd); xmlParser.redirectEntity("http://www.w3.org/2001/xml.xsd",xmlxsd); xmlParser.redirectEntity("datatypes.dtd",datatypesdtd); xmlParser.redirectEntity("http://www.w3.org/2001/datatypes.dtd",datatypesdtd); xmlParser.redirectEntity("j2ee_web_services_client_1_1.xsd",webservice11xsd); xmlParser.redirectEntity("http://www.ibm.com/webservices/xsd/j2ee_web_services_client_1_1.xsd",webservice11xsd); xmlParser.redirectEntity("javaee_web_services_client_1_2.xsd",webservice12xsd); xmlParser.redirectEntity("http://www.ibm.com/webservices/xsd/javaee_web_services_client_1_2.xsd",webservice12xsd); return xmlParser; } /* ------------------------------------------------------------------------------- */ public void setWebAppContext (WebAppContext context) { _context = context; } /* ------------------------------------------------------------------------------- */ public WebAppContext getWebAppContext() { return _context; } /* ------------------------------------------------------------------------------- */ /** Configure ClassPath. */ public void configureClassLoader() throws Exception { } /* ------------------------------------------------------------------------------- */ public void configureDefaults() throws Exception { //cannot configure if the context is already started if (_context.isStarted()) { if (Log.isDebugEnabled()){Log.debug("Cannot configure webapp after it is started");} return; } String defaultsDescriptor=getWebAppContext().getDefaultsDescriptor(); if(defaultsDescriptor!=null&&defaultsDescriptor.length()>0) { Resource dftResource=Resource.newSystemResource(defaultsDescriptor); if(dftResource==null) dftResource=Resource.newResource(defaultsDescriptor); configure(dftResource.getURL().toString()); _defaultWelcomeFileList=_welcomeFiles!=null; } } /* ------------------------------------------------------------------------------- */ public void configureWebApp() throws Exception { //cannot configure if the context is already started if (_context.isStarted()) { if (Log.isDebugEnabled()) Log.debug("Cannot configure webapp after it is started"); return; } URL webxml=findWebXml(); if (webxml!=null) configure(webxml.toString()); String overrideDescriptor=getWebAppContext().getOverrideDescriptor(); if(overrideDescriptor!=null&&overrideDescriptor.length()>0) { Resource orideResource=Resource.newSystemResource(overrideDescriptor); if(orideResource==null) orideResource=Resource.newResource(overrideDescriptor); _xmlParser.setValidating(false); configure(orideResource.getURL().toString()); } } /* ------------------------------------------------------------------------------- */ protected URL findWebXml() throws IOException, MalformedURLException { String descriptor=getWebAppContext().getDescriptor(); if (descriptor!=null) { Resource web= Resource.newResource(descriptor); if (web.exists()&& !web.isDirectory()) return web.getURL(); } Resource web_inf=getWebAppContext().getWebInf(); if(web_inf!=null && web_inf.isDirectory()) { // do web.xml file Resource web=web_inf.addPath("web.xml"); if(web.exists()) return web.getURL(); Log.debug("No WEB-INF/web.xml in "+getWebAppContext().getWar() +". Serving files and default/dynamic servlets only"); } return null; } /* ------------------------------------------------------------------------------- */ public void configure(String webXml) throws Exception { XmlParser.Node config=null; config=_xmlParser.parse(webXml); initialize(config); } /* ------------------------------------------------------------------------------- */ public void deconfigureWebApp() throws Exception { // TODO preserve any configuration that pre-existed. _servletHandler = getWebAppContext().getServletHandler(); _servletHandler.setFilters(null); _servletHandler.setFilterMappings(null); _servletHandler.setServlets(null); _servletHandler.setServletMappings(null); getWebAppContext().setEventListeners(null); getWebAppContext().setWelcomeFiles(null); if (getWebAppContext().getSecurityHandler() != null) getWebAppContext().getSecurityHandler().setConstraintMappings(null); if (getWebAppContext().getErrorHandler() instanceof ErrorPageErrorHandler) ((ErrorPageErrorHandler)getWebAppContext().getErrorHandler()).setErrorPages(null); // TODO remove classpaths from classloader } /* ------------------------------------------------------------ */ protected void initialize(XmlParser.Node config) throws ClassNotFoundException,UnavailableException { _servletHandler = getWebAppContext().getServletHandler(); // Get any existing servlets and mappings. _filters=LazyList.array2List(_servletHandler.getFilters()); _filterMappings=LazyList.array2List(_servletHandler.getFilterMappings()); _servlets=LazyList.array2List(_servletHandler.getServlets()); _servletMappings=LazyList.array2List(_servletHandler.getServletMappings()); _listeners = LazyList.array2List(getWebAppContext().getEventListeners()); _welcomeFiles = LazyList.array2List(getWebAppContext().getWelcomeFiles()); _constraintMappings = LazyList.array2List(getWebAppContext().getSecurityHandler().getConstraintMappings()); _errorPages = getWebAppContext().getErrorHandler() instanceof ErrorPageErrorHandler ? ((ErrorPageErrorHandler)getWebAppContext().getErrorHandler()).getErrorPages():null; String version=config.getAttribute("version","DTD"); if ("2.5".equals(version)) _version=25; else if ("2.4".equals(version)) _version=24; else if ("DTD".equals(version)) { _version=23; String dtd=_xmlParser.getDTD(); if (dtd!=null && dtd.indexOf("web-app_2_2")>=0) _version=22; } Iterator iter=config.iterator(); XmlParser.Node node=null; while(iter.hasNext()) { try { Object o=iter.next(); if(!(o instanceof XmlParser.Node)) continue; node=(XmlParser.Node)o; String name=node.getTag(); initWebXmlElement(name,node); } catch(ClassNotFoundException e) { throw e; } catch(Exception e) { Log.warn("Configuration problem at "+node,e); throw new UnavailableException("Configuration problem"); } } _servletHandler.setFilters((FilterHolder[])LazyList.toArray(_filters,FilterHolder.class)); _servletHandler.setFilterMappings((FilterMapping[])LazyList.toArray(_filterMappings,FilterMapping.class)); _servletHandler.setServlets((ServletHolder[])LazyList.toArray(_servlets,ServletHolder.class)); _servletHandler.setServletMappings((ServletMapping[])LazyList.toArray(_servletMappings,ServletMapping.class)); getWebAppContext().setEventListeners((EventListener[])LazyList.toArray(_listeners,EventListener.class)); getWebAppContext().setWelcomeFiles((String[])LazyList.toArray(_welcomeFiles,String.class)); getWebAppContext().getSecurityHandler().setConstraintMappings((ConstraintMapping[])LazyList.toArray(_constraintMappings, ConstraintMapping.class)); if (_errorPages!=null && getWebAppContext().getErrorHandler() instanceof ErrorPageErrorHandler) ((ErrorPageErrorHandler)getWebAppContext().getErrorHandler()).setErrorPages(_errorPages); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -