📄 contextmanager.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.core.*;
import org.apache.tomcat.net.*;
import org.apache.tomcat.context.*;
import org.apache.tomcat.loader.*;
import org.apache.tomcat.request.*;
import org.apache.tomcat.util.*;
import org.apache.tomcat.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.tomcat.service.PoolTcpConnector;
//import java.security.*;
/* XXX The main function of CM is to serve as an entry point into
tomcat and manage a list of resources that are part of the servlet
processing. ( manage == keep a list and provide centralized access ).
It also have helper functions for common callbacks - but we need to
review and change that.
*/
/*
* It is possible to extend and override some of the methods ( this is not
* "final" ), but this is an extreme case and shouldn't be used - if you want
* to extend the server you should use interceptors.
* Another extreme case is having more than one ContextManager instances.
* Each will corespond to a separate servlet engine/container that will work
* independent of each other in the same VM ( each having possible multiple
* virtual hosts, etc). Both uses are not forbiden, but shouldn't be used
* unless there is real need for that - and if that happen we should
* add interfaces to express the use cases.
*/
/**
* ContextManager is the entry point and "controler" of the servlet execution.
* It maintains a list of WebApplications and a list of global event interceptors
* that are set up to handle the actual execution.
*
* The ContextManager is a helper that will direct the request processing flow
* from its arrival from the server/protocl adapter ( in service() ).
* It is also responsible for controlling the request processing steps, from
* request parsing and mapping, auth, autorization, pre/post service, actual
* invocation and logging.
*
* It will also store properties that are global to the servlet container -
* like root directory, install dir, work dir.
*
*
* @author James Duncan Davidson [duncan@eng.sun.com]
* @author James Todd [gonzo@eng.sun.com]
* @author Harish Prabandham
* @author costin@eng.sun.com
* @author Hans Bergsten [hans@gefionsoftware.com]
*/
public class ContextManager {
/**
* The string constants for this ContextManager.
*/
private static StringManager sm = StringManager.getManager("org.apache.tomcat.core");
/** Global interceptors - all requests that will be served by this
engine will pass those filters
*/
private Vector requestInterceptors = new Vector();
private Vector contextInterceptors = new Vector();
// cache - faster access to interceptors, using [] instead of Vector
ContextInterceptor cInterceptors[];
RequestInterceptor rInterceptors[];
/**
* The default security permissions to use
*/
private Object permissions;
/** Adapters for the incoming protocol
*/
Vector connectors=new Vector();
/** Contexts managed by this server
*/
private Vector contextsV=new Vector();
int debug=0;
// Global properties for this tomcat instance:
/** Private workspace for this server
*/
String workDir = null; // Initialized the first time we get it
/** Configured workspace directory name (not absolutized yet)
*/
String workDirProperty = null;
/** The base directory where this instance runs.
* It can be different from the install directory to
* allow one install per system and multiple users
*/
String home;
/** The directory where tomcat is installed
*/
String installDir;
// port for SSL endpoint - for redirect
int securePort=-1;
/** The setting which controls display of stack traces
* in the default exception handling
*/
boolean showDebugInfo = true;
/** Default work dir, relative to home
*/
public static final String DEFAULT_WORK_DIR="work";
// when tomcat is embeded in a product, this will be
// used as parent for all context class loaders.
private ClassLoader parentLoader;
/**
* Construct a new ContextManager instance with default values.
*/
public ContextManager() {
}
// -------------------- setable properties: tomcat directories ---
/**
* The home of the tomcat instance - you can have multiple
* users running tomcat, with a shared install directory.
* Every instance will have its own logs, webapps directory
* and local config, all relative to this directory.
*/
public void setHome(String home) {
this.home=FileUtil.getCanonicalPath( home );
if (debug>0) logInt( "Setting home to " + this.home );
}
/**
* The home of the tomcat instance - you can have multiple
* users running tomcat, with a shared install directory.
* Every instance will have its own logs, webapps directory
* and local config, all relative to this directory.
*
* If no home is configured we'll try the install dir
* XXX clean up the order and process of guessing - maybe we can
* just throw error instead of guessing wrong.
*/
public String getHome() {
if( debug > 20 ) {
// we want to know all places that need this property
// and find how it's computed - for embeding tc.
logInt( "getHome " + home + " " + installDir + " " +
System.getProperty("tomcat.home") + " " +
FileUtil.getCanonicalPath( "." ));
/*DEBUG*/ try {throw new Exception(); } catch(Exception ex)
{ex.printStackTrace();}
}
if(home!=null) return home;
// If none defined, assume tomcat.home is used as base.
if( installDir != null )
home=FileUtil.getCanonicalPath( installDir );
if(home!=null) return home;
// try at least the system property
home=FileUtil.getCanonicalPath( System.getProperty("tomcat.home") );
if(home!=null) return home;
home=FileUtil.getCanonicalPath( "." );
// try current dir - we should throw an exception
return home;
}
/** Get installation directory, where libraries and default files
* are located. If path specified is relative,
* evaluate it relative to the current working directory.
*/
public String getInstallDir() {
if( debug > 20 ) {
// we want to know all places that need this property
// and find how it's computed - for embeding tc.
logInt( "getInstallDir " + installDir + " " +
System.getProperty("tomcat.home"));
/*DEBUG*/ try {throw new Exception(); } catch(Exception ex) {
ex.printStackTrace();}
}
if(installDir!= null) return installDir;
installDir=System.getProperty("tomcat.home");
if(installDir!= null) return installDir;
// If the property is not set ( for example JNI worker ) assume
// at least home is set up corectly.
installDir=getHome();
return installDir;
}
/** Set installation directory, where libraries and default files
* are located. If path specified is relative,
* evaluate it relative to the current working directory.
*/
public void setInstallDir( String tH ) {
installDir=tH;
}
public int getSecurePort() {
return securePort;
}
/** Secure port is set to the SSL connector that will handle
* INTEGRAL/CONFIDENTIAL transport. This is an initial solution,
* it may change !
*/
public void setSecurePort(int p) {
securePort=p;
}
/**
* WorkDir property - where all working files will be created
*/
public void setWorkDir( String wd ) {
if (debug>0) logInt("set work dir " + wd);
this.workDirProperty = wd; // Store only the string for now
}
/**
* WorkDir property - where all working files will be created
*/
public String getWorkDir() {
// The first time this is called, calculate the right value
if (this.workDir == null) {
File f = null;
if (this.workDirProperty == null)
f = new File(DEFAULT_WORK_DIR);
else
f = new File(this.workDirProperty);
if (!f.isAbsolute())
f = getAbsolute(f);
this.workDir = f.getAbsolutePath();
if (debug>0) logInt("calc work dir " + this.workDir);
}
// Return the calculated work directory value
return (this.workDir);
}
/**
* Get the default Security Permissions for this server
*/
public Object getPermissions() {
return permissions;
}
/**
* Add a Permission to the default Permissions
*/
public void setPermissions(Object permissions) {
this.permissions = permissions;
}
/** Get the name of the class to be used for generating random numbers by the
* session id generator. By default this is <code>java.security.SecureRandom</code>.
**/
public String getRandomClass() {
String randomClass = System.getProperty("tomcat.sessionid.randomclass");
return randomClass == null ? "java.security.SecureRandom" : randomClass;
}
/** Sets the name of the class used for generating random numbers by the
* session id generator.
*/
public void setRandomClass(String randomClass) {
Properties sysProp = System.getProperties();
sysProp.put("tomcat.sessionid.randomclass", randomClass);
}
/** Get the showDebugInfo property to be used for controlling the display of
* debugging information in default responses.
**/
public boolean getShowDebugInfo() {
return showDebugInfo;
}
/** Sets the showDebugInfo property used for controlling the display of
* debugging information in default responses.
*/
public void setShowDebugInfo(boolean showDebugInfo) {
this.showDebugInfo = showDebugInfo;
}
/** When tomcat is embeded in other applications you
can set this property to the application classloader.
This will be used as the parent loader for all
context class loaders.
*/
public void setParentClassLoader( ClassLoader cl ) {
parentLoader=cl;
}
public ClassLoader getParentClassLoader() {
return parentLoader;
}
// -------------------- Support functions --------------------
/**
* Set default settings ( interceptors, connectors, loader, manager )
* It is called from init if no connector is set up - note that we
* try to avoid any "magic" - you either set up everything ( using
* server.xml or alternatives) or you don't set up and then defaults
* will be used.
*
* Set interceptors or call setDefaults before adding contexts.
*
* This is mostly used to allow "0 config" case ( you just want the
* reasonable defaults and nothing else ).
*/
public void setDefaults() {
if(connectors.size()==0) {
if(debug>5) logInt("Setting default adapter");
PoolTcpConnector sc=new PoolTcpConnector();
sc.setTcpConnectionHandler( new
org.apache.tomcat.service.http.HttpConnectionHandler());
addServerConnector( sc );
}
if( contextInterceptors.size()==0) {
if(debug>5) logInt("Setting default context interceptors");
addContextInterceptor(new LogEvents());
addContextInterceptor(new AutoSetup());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -