📄 contextmanager.java
字号:
// addContextInterceptor(new PolicyInterceptor());
addContextInterceptor(new LoaderInterceptor());
addContextInterceptor(new DefaultCMSetter());
addContextInterceptor(new WorkDirInterceptor());
addContextInterceptor( new WebXmlReader());
addContextInterceptor(new LoadOnStartupInterceptor());
}
if( requestInterceptors.size()==0) {
if(debug>5) logInt("Setting default request interceptors");
addRequestInterceptor(new SessionInterceptor());
SimpleMapper1 smap=new SimpleMapper1();
smap.setContextManager( this );
addRequestInterceptor(smap);
addRequestInterceptor(new
org.apache.tomcat.session.StandardSessionInterceptor());
}
}
/** Init() is called after the context manager is set up
* and configured. It will init all internal components
* to be ready for start.
*
* There is a difference between Context and Adapters - the
* adapter has start/stop, the Context has init/shutdown(destroy
* may be a better name ? ). ( Initializing is different from starting.)
*/
public void init() throws TomcatException {
// logInt( "Tomcat install = " + getInstallDir());
// logInt( "Tomcat home = " + home);
if(debug>0 ) logInt( "Tomcat classpath = " +
System.getProperty( "java.class.path" ));
setAccount( ACC_INIT_START, System.currentTimeMillis());
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].engineInit( this );
}
// init contexts
// XXX init must be called whith no context inside !!!
Enumeration enum = getContexts();
while (enum.hasMoreElements()) {
Context context = (Context)enum.nextElement();
try {
initContext( context );
} catch (TomcatException ex ) {
if( context!=null ) {
logInt( "ERROR initializing " + context.toString() );
removeContext( context );
Throwable ex1=ex.getRootCause();
if( ex1!=null ) ex.printStackTrace();
}
}
}
setAccount( ACC_INIT_END, System.currentTimeMillis() );
}
/** Will shutdown all contexts
*/
public void shutdown() throws TomcatException {
while (!contextsV.isEmpty()) {
removeContext((Context) contextsV.firstElement());
}
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].engineShutdown( this );
}
}
/**
* Initializes this context to be able to accept requests. This action
* will cause the context to load it's configuration information
* from the webapp directory in the docbase.
*
* <p>This method must be called
* before any requests are handled by this context. It will be called
* after the context was added, typically when the engine starts
* or after the admin added a new context.
*/
public void initContext( Context ctx ) throws TomcatException {
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].contextInit( ctx );
}
}
/** Stop the context and release all resources.
*/
public void shutdownContext( Context ctx ) throws TomcatException {
// XXX This is here by accident, it should be moved as part
// of a normal context interceptor that will handle all standard
// start/stop actions
// shut down and servlets
Enumeration enum = ctx.getServletNames();
while (enum.hasMoreElements()) {
String key = (String)enum.nextElement();
ServletWrapper wrapper = ctx.getServletByName( key );
ctx.removeServletByName( key );
try {
wrapper.destroy();
} catch(Exception ex ) {
ctx.log( "Error in destroy ", ex);
}
}
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].contextShutdown( ctx );
}
}
/** Will start the connectors and begin serving requests.
* It must be called after init.
*/
public void start() throws Exception {// XXX TomcatException {
Enumeration connE=getConnectors();
while( connE.hasMoreElements() ) {
((ServerConnector)connE.nextElement()).start();
}
}
/** Will stop all connectors
*/
public void stop() throws Exception {// XXX TomcatException {
if(debug>0) logInt("Stopping context manager ");
Enumeration connE=getConnectors();
while( connE.hasMoreElements() ) {
((ServerConnector)connE.nextElement()).stop();
}
shutdown();
}
// -------------------- Contexts --------------------
/** Return the list of contexts managed by this server
*/
public Enumeration getContexts() {
return contextsV.elements();
}
/**
* Adds a new Context to the set managed by this ContextManager.
*
* @param ctx context to be added.
*/
public void addContext( Context ctx ) throws TomcatException {
// Make sure context knows about its manager.
ctx.setContextManager( this );
// If the context already exist - the interceptors need
// to deal with that ( either replace or throw an exception ).
// The mapping alghoritm may use more than path and host -
// if not now, then in future.
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].addContext( this, ctx );
}
String vhost=ctx.getHost();
logInt("Adding context " + ctx.toString());
// XXX temporary workaround for the old SimpleMapper -
// This code will be removed as soon as the new mapper is stable.
if( vhost ==null ) // the old mapper will support only "default" server
contexts.put( ctx.getPath(), ctx );
contextsV.addElement( ctx );
}
/** Shut down and removes a context from service
*/
public void removeContext( Context context ) throws TomcatException {
if( context==null ) return;
logInt( "Removing context " + context.toString());
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].removeContext( this, context );
}
shutdownContext( context );
contextsV.removeElement(context);
}
void doReload( Request req, Context context ) throws TomcatException {
if( context==null ) return;
if( debug>0 ) logInt( "Reloading context " + context.toString());
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].reload( req, context );
}
}
/** Notify interceptors that a new container was added.
*/
public void addContainer( Container container )
throws TomcatException
{
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].addContainer( container);
}
}
/** Notify interceptors that a container was removed.
*/
public void removeContainer( Container container )
throws TomcatException
{
ContextInterceptor cI[]=getContextInterceptors();
for( int i=0; i< cI.length; i++ ) {
cI[i].removeContainer( container);
}
}
// -------------------- Connectors and Interceptors --------------------
/**
* Add the specified server connector to the those attached to this server.
*
* @param con The new server connector
*/
public synchronized void addServerConnector( ServerConnector con ) {
if(debug>0) logInt("Add connector javaClass=\"" +
con.getClass().getName() + "\"");
con.setServer( this );
connectors.addElement( con );
}
public Enumeration getConnectors() {
return connectors.elements();
}
public void addRequestInterceptor( RequestInterceptor ri ) {
if(debug>0) logInt("Add requestInterceptor javaClass=\"" +
ri.getClass().getName() + "\" ");
requestInterceptors.addElement( ri );
if( ri instanceof ContextInterceptor )
contextInterceptors.addElement( ri );
}
/** Return all the interceptors associated with a request.
That includes global ( context manager ) interceptors,
webapp ( Context ) interceptors and possibly interceptors
associated with containers ( urls inside the web app ).
For performance reasons we use arrays and cache the result inside
containers.
XXX Todo: container-level interceptors are not supported.
Dynamic add of interceptors is not supported.
*/
public RequestInterceptor[] getRequestInterceptors( Request req ) {
// Container ct=req.getContext().getContainer();
// return ct.getRequestInterceptors();
// just global interceptors
return getRequestInterceptors();
}
/** Return the context interceptors as an array.
For performance reasons we use an array instead of
returning the vector - the interceptors will not change at
runtime and array access is faster and easier than vector
access
*/
public RequestInterceptor[] getRequestInterceptors() {
if( rInterceptors == null ||
rInterceptors.length != requestInterceptors.size())
{
rInterceptors=new RequestInterceptor[requestInterceptors.size()];
for( int i=0; i<rInterceptors.length; i++ ) {
rInterceptors[i]=(RequestInterceptor)
requestInterceptors.elementAt(i);
}
}
return rInterceptors;
}
public void addContextInterceptor( ContextInterceptor ci) {
if(debug>0) logInt("Add contextInterceptor javaClass=\"" +
ci.getClass().getName() + "\" ");
contextInterceptors.addElement( ci );
}
/** Return the context interceptors as an array.
For performance reasons we use an array instead of
returning the vector - the interceptors will not change at
runtime and array access is faster and easier than vector
access
*/
public ContextInterceptor[] getContextInterceptors() {
if( contextInterceptors.size() == 0 ) {
setDefaults();
}
if( cInterceptors == null ||
cInterceptors.length != contextInterceptors.size())
{
cInterceptors=new ContextInterceptor[contextInterceptors.size()];
for( int i=0; i<cInterceptors.length; i++ ) {
cInterceptors[i]=(ContextInterceptor)contextInterceptors.
elementAt(i);
}
}
return cInterceptors;
}
// -------------------- Request processing / subRequest ------------------
// -------------------- Main request processing methods ------------------
/** Prepare the req/resp pair for use in tomcat.
* Call it after you create the request/response objects
*/
public void initRequest( Request req, Response resp ) {
// used to be done in service(), but there is no need to do it
// every time.
// We may add other special calls here.
// XXX Maybe make it a callback?
resp.setRequest( req );
req.setResponse( resp );
req.setContextManager( this );
}
/** This is the entry point in tomcat - the connectors ( or any other
* component able to generate Request/Response implementations ) will
* call this method to get it processed.
* XXX make sure the alghoritm is right, deal with response codes
*/
public void service( Request req, Response res ) {
/**
* XXX Normalize the request URI. This is important
* to prevent non-normalized URIs from causing security constraints
* from being bypassed. For example, /examples/jsp/../jsp/security/protected/index.jsp
* would not trigger the AccessInterceptor.
*/
req.setRequestURI(URLUtil.normalizeURI(req.getRequestURI()));
internalService( req, res );
// clean up
try {
res.finish();
req.recycle();
res.recycle();
} catch( Throwable ex ) {
handleError( req, res, ex );
}
return;
}
// Request processing steps and behavior
private void internalService( Request req, Response res ) {
try {
/* assert req/res are set up
corectly - have cm, and one-one relation
*/
// wront request - parsing error
int status=res.getStatus();
if( status >= 400 ) {
if( debug > 0)
log( "Error reading request " + req + " " + status);
handleStatus( req, res, status );
return;
}
status= processRequest( req );
if( status != 0 ) {
if( debug > 0)
log("Error mapping the request " + req + " " + status);
handleStatus( req, res, status );
return;
}
if( req.getWrapper() == null ) {
status=404;
if( debug > 0)
log("No handler for request " + req + " " + status);
handleStatus( req, res, status );
return;
}
String roles[]=req.getRequiredRoles();
if(roles != null )
status=doAuthorize( req, res, roles );
if( status > 200 ) {
if( debug > 0)
log("Authorize error " + req + " " + status);
handleStatus( req, res, status );
return;
}
req.getWrapper().service(req, res);
} catch (Throwable t) {
handleError( req, res, t );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -