📄 context.java
字号:
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.core;
import org.apache.tomcat.context.*;
import org.apache.tomcat.facade.*;
import org.apache.tomcat.util.*;
import java.security.*;
import java.lang.reflect.*;
import org.apache.tomcat.logging.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;
/* Right now we have all the properties defined in web.xml.
The interceptors will go into Container ( every request will
be associated with the final container, which will point back to the
context). That will allow us to use a simpler and more "targeted"
object model.
The only "hard" part is moving getResource() and getRealPath() in
a different class, using a filesystem independent abstraction.
*/
/**
* Context represent a Web Application as specified by Servlet Specs.
* The implementation is a repository for all the properties
* defined in web.xml and tomcat specific properties.
*
* @author James Duncan Davidson [duncan@eng.sun.com]
* @author James Todd [gonzo@eng.sun.com]
* @author Jason Hunter [jch@eng.sun.com]
* @author Harish Prabandham
* @author costin@dnt.ro
* @author Gal Shachor shachor@il.ibm.com
* @author Arieh Markel [arieh.markel@sun.com]
*/
public class Context {
private static StringManager sm =StringManager.getManager("org.apache.tomcat.core");
// -------------------- internal properties
// context "id"
private String path = "";
private String docBase;
// Absolute path to docBase if file-system based
private String absPath;
// internal state / related objects
private ContextManager contextM;
private ServletContext contextFacade;
private boolean crossContext = true;
private ServletLoader servletL;
boolean reloadable=true; // XXX change default to false after testing
private Hashtable attributes = new Hashtable();
private File workDir;
// Security Permissions for webapps and jsp for this context
Object perms = null;
Object protectionDomain;
// private RequestSecurityProvider rsProvider;
// Servlets loaded by this context( String->ServletWrapper )
private Hashtable servlets = new Hashtable();
// -------------------- from web.xml
private Hashtable initializationParameters = new Hashtable();
// all welcome files that are added are treated as "system default"
private boolean expectUserWelcomeFiles=false;
private Vector welcomeFiles = new Vector();
private Hashtable errorPages = new Hashtable();
private String description = null;
private boolean isDistributable = false;
private MimeMap mimeTypes = new MimeMap();
private int sessionTimeOut = -1;
// taglibs
Hashtable tagLibs=new Hashtable();
// Env entries
Hashtable envEntryTypes=new Hashtable();
Hashtable envEntryValues=new Hashtable();
// Maps specified in web.xml ( String url -> ServletWrapper )
private Hashtable mappings = new Hashtable();
Hashtable constraints=new Hashtable();
Hashtable containers=new Hashtable();
Container defaultContainer = null; // generalization, will replace most of the
// functionality. By using a default container we avoid a lot of checkings
// and speed up searching, and we can get rid of special properties.
private ServletWrapper defaultServlet = null;
// Authentication properties
String authMethod;
String realmName;
String formLoginPage;
String formErrorPage;
int debug=0;
// are servlets allowed to access internal objects?
boolean trusted=false;
String vhost=null;
Vector vhostAliases=new Vector();
FacadeManager facadeM;
public Context() {
defaultContainer=new Container();
defaultContainer.setContext( this );
defaultContainer.setPath( null ); // default container
}
/** Every context is associated with a facade
*/
public ServletContext getFacade() {
if(contextFacade==null )
contextFacade = getFacadeManager().createServletContextFacade( this );
return contextFacade;
}
// -------------------- Settable context properties --------------------
// -------------------- Required properties
public ContextManager getContextManager() {
return contextM;
}
public void setContextManager(ContextManager cm) {
contextM=cm;
}
public boolean getCrossContext() {
return (this.crossContext);
}
public void setCrossContext(boolean crossContext) {
this.crossContext = crossContext;
}
public FacadeManager getFacadeManager() {
if( facadeM==null ) {
/* XXX make it configurable
*/
facadeM=new SimpleFacadeManager( this );
}
return facadeM;
}
/** Base URL for this context
*/
public String getPath() {
return path;
}
/** Base URL for this context
*/
public void setPath(String path) {
// config believes that the root path is called "/",
//
if( "/".equals(path) )
path="";
this.path = path;
}
/** DocBase points to the web application files.
*
* There is no restriction on the syntax and content of DocBase,
* it's up to the various modules to interpret this and use it.
* For example, to serve from a war file you can use war: protocol,
* and set up War interceptors.
*
* "Basic" tomcat treats it as a file ( either absolute or relative to
* the CM home ).
*
* If docBase is relative assume it is relative to the context manager home.
*/
public void setDocBase( String docB ) {
this.docBase=docB;
}
public String getDocBase() {
return docBase;
}
/** Return the absolute path for the docBase, if we are file-system
* based, null otherwise.
*/
public String getAbsolutePath() {
if( absPath!=null) return absPath;
if (FileUtil.isAbsolute( docBase ) )
absPath=docBase;
else
absPath = contextM.getHome() + File.separator + docBase;
try {
absPath = new File(absPath).getCanonicalPath();
} catch (IOException npe) {
}
return absPath;
}
// -------------------- Tomcat specific properties
// workaround for XmlMapper unable to set anything but strings
public void setReloadable( String s ) {
reloadable=new Boolean( s ).booleanValue();
}
public void setReloadable( boolean b ) {
reloadable=b;
}
/** Should we reload servlets ?
*/
public boolean getReloadable() {
return reloadable;
}
// -------------------- Web.xml properties --------------------
public Enumeration getWelcomeFiles() {
return welcomeFiles.elements();
}
/** @deprecated It is used as a hack to allow web.xml override default
welcome files.
Tomcat will first load the "default" web.xml and then this file.
*/
public void removeWelcomeFiles() {
if( ! this.welcomeFiles.isEmpty() )
this.welcomeFiles.removeAllElements();
}
/** If any new welcome file is added, remove the old list of
* welcome files and start a new one. This is used as a hack to
* allow a default web.xml file to specifiy welcome files.
* We should use a better mechanism!
*/
public void expectUserWelcomeFiles() {
expectUserWelcomeFiles = true;
}
public void addWelcomeFile( String s) {
// user specified at least one user welcome file, remove the system
// files
if (s == null ) return;
s=s.trim();
if(s.length() == 0)
return;
if( expectUserWelcomeFiles ) {
removeWelcomeFiles();
expectUserWelcomeFiles=false;
}
welcomeFiles.addElement( s );
}
/** Add a taglib declaration for this context
*/
public void addTaglib( String uri, String location ) {
// System.out.println("Add taglib " + uri + " " + location );
tagLibs.put( uri, location );
}
public String getTaglibLocation( String uri ) {
return (String)tagLibs.get(uri );
}
public Enumeration getTaglibs() {
return tagLibs.keys();
}
/** Add Env-entry to this context
*/
public void addEnvEntry( String name,String type, String value, String description ) {
System.out.println("Add env-entry " + name + " " + type + " " + value + " " +description );
if( name==null || type==null) throw new IllegalArgumentException();
envEntryTypes.put( name, type );
if( value!=null)
envEntryValues.put( name, value );
}
public String getEnvEntryType(String name) {
return (String)envEntryTypes.get(name);
}
public String getEnvEntryValue(String name) {
return (String)envEntryValues.get(name);
}
public Enumeration getEnvEntries() {
return envEntryTypes.keys();
}
public String getInitParameter(String name) {
return (String)initializationParameters.get(name);
}
/** @deprecated use addInitParameter
*/
public void setInitParameter( String name, String value ) {
initializationParameters.put(name, value );
}
public void addInitParameter( String name, String value ) {
initializationParameters.put(name, value );
}
public Enumeration getInitParameterNames() {
return initializationParameters.keys();
}
public Object getAttribute(String name) {
if (name.startsWith("org.apache.tomcat")) {
// XXX XXX XXX XXX Security - servlets may get too much access !!!
// right now we don't check because we need JspServlet to
// be able to access classloader and classpath
if (name.equals("org.apache.tomcat.jsp_classpath")) {
String cp= getServletLoader().getClassPath();
return cp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -