org.apache.tomcat.core
Class Context

java.lang.Object
  |
  +--org.apache.tomcat.core.Context

public class Context
extends java.lang.Object

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. This object has many properties, but doesn't do anything special except simple cashing. You need to set at least "path" and "base" before adding a context to a server. You can also set any other properties. At addContext() stage log and paths will be "fixed" based on context manager settings. At initContext() stage, web.xml will be read and all other properties will be set. WebXmlReader must be the first module in initContext() chain.

Author:
James Duncan Davidson [duncan@eng.sun.com], James Todd [gonzo@eng.sun.com], Jason Hunter [jch@eng.sun.com], Harish Prabandham, costin@dnt.ro, Gal Shachor shachor@il.ibm.com

Field Summary
static java.lang.String ATTRIB_PREFIX
          Private tomcat attribute names
static java.lang.String ATTRIB_PROTECTION_DOMAIN
          Protection domain to be used to create new classes in this context.
static java.lang.String ATTRIB_REAL_CONTEXT
          This attribute will return the real context ( org.apache.tomcat.core.Context).
static int STATE_ADDED
          Context was added to the server, but contextInit() is not called.
static int STATE_DISABLED
          Relative paths are fixed, based on server base, and CM is set.
static int STATE_NEW
          Context is new, possibly not even added to server.
static int STATE_READY
          Context is initialized and ready to serve.
 
Constructor Summary
Context()
           
 
Method Summary
 void addClassPath(java.net.URL url)
           
 void addContainer(Container ct)
          Add a new container.
 void addContentType(java.lang.String ext, java.lang.String type)
           
 void addEnvEntry(java.lang.String name, java.lang.String type, java.lang.String value, java.lang.String description)
          Add Env-entry to this context
 void addErrorPage(java.lang.String errorType, java.lang.String value)
           
 void addHandler(Handler wrapper)
          Add a servlet.
 void addHostAlias(java.lang.String alias)
          Virtual host support - this context will be part of a virtual host with the specified name.
 void addInitParameter(java.lang.String name, java.lang.String value)
           
 void addInterceptor(BaseInterceptor ri)
          Add a per-context interceptor.
 void addSecurityConstraint(java.lang.String[] path, java.lang.String[] methods, java.lang.String[] roles, java.lang.String transport)
          Deprecated. Use addContainer
 void addServlet(Handler wrapper)
          Add a servlet.
 void addServletMapping(java.lang.String path, java.lang.String servletName)
          Deprecated. Use addContainer
 void addTaglib(java.lang.String uri, java.lang.String location)
          Add a taglib declaration for this context
 void addWelcomeFile(java.lang.String s)
          Add an welcome file.
 boolean allowAttribute(java.lang.String name)
          Check if "special" attributes can be used by user application.
 java.lang.String getAbsolutePath()
          Return the absolute path for the docBase, if we are file-system based, null otherwise.
 java.lang.Object getAttribute(java.lang.String name)
          Return an attribute value.
 java.util.Enumeration getAttributeNames()
           
 java.lang.String getAuthMethod()
          Authentication method, if any specified
 java.lang.ClassLoader getClassLoader()
          The current class loader.
 java.net.URL[] getClassPath()
          Returns the full classpath - concatenation of ContextManager classpath and locally specified class path
 Container getContainer()
          Default container for this context.
 Container getContainer(java.lang.String path)
          Return the container ( properties ) associated with a path ( relative to this context )
 java.util.Enumeration getContainerLocations()
          Return an enumeration of Strings, representing all URLs ( relative to this context ) having associated properties ( handlers, security, etc)
 java.util.Enumeration getContainers()
           
 ContextManager getContextManager()
          Returned the main server ( servlet container )
 int getDebug()
           
 java.lang.String getDescription()
           
 java.lang.String getDocBase()
           
 java.lang.String getEngineHeader()
           
 java.util.Enumeration getEnvEntries()
           
 java.lang.String getEnvEntryType(java.lang.String name)
           
 java.lang.String getEnvEntryValue(java.lang.String name)
           
 java.lang.String getErrorPage(int errorCode)
           
 java.lang.String getErrorPage(java.lang.String errorCode)
           
 java.lang.Object getFacade()
          Every context is associated with a facade.
 java.lang.String getFormErrorPage()
           
 java.lang.String getFormLoginPage()
           
 java.lang.String getHost()
          Return the virtual host name, or null if we are in the default context
 java.lang.String getHostAddress()
          Return the virtual host ip address.
 java.util.Enumeration getHostAliases()
           
 java.lang.String getId()
           
 java.lang.String getInitParameter(java.lang.String name)
           
 java.util.Enumeration getInitParameterNames()
           
 Log getLog()
           
 java.net.FileNameMap getMimeMap()
           
 java.lang.String getName()
          Return a name ( id ) for this context.
 java.lang.String getPath()
          Base URL for this context
 java.lang.String getProperty(java.lang.String n)
           
 java.lang.String getRealmName()
          Realm to be used
 boolean getReloadable()
          Should we reload servlets ?
 java.lang.String getServletAPI()
           
 Handler getServletByName(java.lang.String servletName)
           
 Log getServletLog()
           
 java.util.Enumeration getServletNames()
          Return all servlets registered with this Context The elements will be of type Handler ( or sub-types )
 int getSessionTimeOut()
           
 int getState()
           
 java.lang.String getTaglibLocation(java.lang.String uri)
           
 java.util.Enumeration getTaglibs()
           
 java.lang.String[] getWelcomeFiles()
          Return welcome files defined in web.xml or the defaults, if user doesn't define any
 java.io.File getWorkDir()
           
 void init()
          Initializes this context to be able to accept requests.
 boolean isDistributable()
           
 boolean isTrusted()
           
 void log(java.lang.String msg)
          Internal log method
 void log(java.lang.String msg, java.lang.Throwable t)
          Internal log method
 void log(java.lang.String msg, java.lang.Throwable t, int level)
          Internal log method
 void logServlet(java.lang.String msg, java.lang.Throwable t)
          User-level log method ( called from a servlet).
 void removeAttribute(java.lang.String name)
           
 void removeContainer(Container ct)
          Remove a container
 void removeHandler(Handler handler)
           
 void removeServletByName(java.lang.String servletName)
          Remove the servlet with a specific name
 void setAbsolutePath(java.lang.String s)
          Set the absolute path to this context.
 void setAttribute(java.lang.String name, java.lang.Object object)
          XXX Use callbacks !!
 void setClassLoader(java.lang.ClassLoader cl)
           
 void setContextManager(ContextManager cm)
          This method is called when the Context is added to a server.
protected  void setContextManager1(ContextManager cm)
           
 void setDebug(int level)
           
 void setDescription(java.lang.String description)
           
 void setDistributable(boolean isDistributable)
           
 void setDocBase(java.lang.String docB)
          DocBase points to the web application files.
 void setEngineHeader(java.lang.String s)
          What is reported in the "Servlet-Engine" header for this context.
 void setFacade(java.lang.Object obj)
           
 void setFormErrorPage(java.lang.String page)
           
 void setFormLoginPage(java.lang.String page)
           
 void setHost(java.lang.String h)
          Make this context visible as part of a virtual host.
 void setHostAddress(java.lang.String ip)
          Set the virtual host ip address.
 void setIcon(java.lang.String icon)
           
 void setLog(Log logger)
           
 void setLoginConfig(java.lang.String authMethod, java.lang.String realmName, java.lang.String formLoginPage, java.lang.String formErrorPage)
           
 void setName(java.lang.String s)
           
 void setPath(java.lang.String path)
          Base URL for this context
 void setProperty(java.lang.String n, java.lang.String v)
           
 void setReloadable(boolean b)
           
 void setServletAPI(java.lang.String s)
          The servlet API variant that will be used for requests in this context
 void setServletLog(Log logger)
           
 void setSessionTimeOut(int sessionTimeOut)
           
 void setState(int state)
          Move the context in a different state.
protected  void setState1(int state)
           
 void setTrusted(boolean t)
          Mark the webapplication as trusted, i.e.
 void setWorkDir(java.io.File workDir)
           
 void setWorkDir(java.lang.String workDir)
          Work dir is a place where servlets are allowed to write
 void shutdown()
          Stop the context.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ATTRIB_PREFIX

public static final java.lang.String ATTRIB_PREFIX
Private tomcat attribute names

ATTRIB_PROTECTION_DOMAIN

public static final java.lang.String ATTRIB_PROTECTION_DOMAIN
Protection domain to be used to create new classes in this context. This is used only by JspServlet, and should be avoided - the preferred mechanism is to use the default policy file and URLClassLoader.

ATTRIB_REAL_CONTEXT

public static final java.lang.String ATTRIB_REAL_CONTEXT
This attribute will return the real context ( org.apache.tomcat.core.Context). Only "trusted" applications will get the value. Null if the application is not trusted.

STATE_NEW

public static final int STATE_NEW
Context is new, possibly not even added to server. ContextManager is not set, and most of the paths are not fixed

STATE_ADDED

public static final int STATE_ADDED
Context was added to the server, but contextInit() is not called. Paths are not set yet, the only valid information is the contextURI.

STATE_DISABLED

public static final int STATE_DISABLED
Relative paths are fixed, based on server base, and CM is set. If a request arives for this context, an error message should be displayed ( "application is temporary disabled" )

STATE_READY

public static final int STATE_READY
Context is initialized and ready to serve. We have all mappings and configs from web.xml.
Constructor Detail

Context

public Context()
Method Detail

addContainer

public void addContainer(Container ct)
                  throws TomcatException
Add a new container. Container define special properties for a set of urls.

addServletMapping

public void addServletMapping(java.lang.String path,
                              java.lang.String servletName)
                       throws TomcatException
Deprecated. Use addContainer

Maps a named servlet to a particular path or extension. If the named servlet is unregistered, it will be added and subsequently mapped. The servlet can be set by intereceptors during addContainer() hook. If the mapping already exists it will be replaced by the new mapping.

addSecurityConstraint

public void addSecurityConstraint(java.lang.String[] path,
                                  java.lang.String[] methods,
                                  java.lang.String[] roles,
                                  java.lang.String transport)
                           throws TomcatException
Deprecated. Use addContainer

Will add a new security constraint: For all paths: if( match(path) && match(method) && match( transport ) ) then require("roles") This is equivalent with adding a Container with the path, method and transport. If the container will be matched, the request will have to pass the security constraints.

allowAttribute

public boolean allowAttribute(java.lang.String name)
Check if "special" attributes can be used by user application. Only trusted apps can get access to the implementation object.

getFacade

public java.lang.Object getFacade()
Every context is associated with a facade. We don't know the exact type of the facade, as a Context can be associated with a 2.2 ... ServletContext.

setFacade

public void setFacade(java.lang.Object obj)

getContextManager

public final ContextManager getContextManager()
Returned the main server ( servlet container )

setContextManager

public void setContextManager(ContextManager cm)
This method is called when the Context is added to a server. Some of the Context properties depend on the ContextManager, and will be adjusted by interceptors ( DefaultCMSetter )

setContextManager1

protected void setContextManager1(ContextManager cm)

getContainer

public Container getContainer()
Default container for this context.

getState

public final int getState()

setState

public void setState(int state)
              throws TomcatException
Move the context in a different state. Can be called only from tomcat.core.ContextManager. ( package access )

setState1

protected void setState1(int state)

init

public void init()
          throws TomcatException
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.

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 adds a new context. After this call, the context will be in READY state and will be able to server requests.

Throws:
if - any interceptor throws an exception the error will prevent the context from becoming READY

shutdown

public void shutdown()
              throws TomcatException
Stop the context. After the call the context will be disabled, ( DISABLED state ) and it'll not be able to serve requests. The context will still be available and can be enabled later by calling initContext(). Requests mapped to this context should report a "temporary unavailable" message. All servlets will be destroyed, and resources held by the context will be freed. The contextShutdown callbacks can wait until the running serlvets are completed - there is no way to force the shutdown.

getName

public java.lang.String getName()
Return a name ( id ) for this context. Currently it's composed from the virtual host and path, or set explicitely by the app.

setName

public void setName(java.lang.String s)

getPath

public java.lang.String getPath()
Base URL for this context

setPath

public void setPath(java.lang.String path)
Base URL for this context

setHost

public void setHost(java.lang.String h)
Make this context visible as part of a virtual host. The host is the "default" name, it may also have aliases.

getHost

public java.lang.String getHost()
Return the virtual host name, or null if we are in the default context

setHostAddress

public final void setHostAddress(java.lang.String ip)
Set the virtual host ip address.

getHostAddress

public final java.lang.String getHostAddress()
Return the virtual host ip address.

setDocBase

public void setDocBase(java.lang.String docB)
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 ).

getDocBase

public java.lang.String getDocBase()

getAbsolutePath

public java.lang.String getAbsolutePath()
Return the absolute path for the docBase, if we are file-system based, null otherwise.

setAbsolutePath

public void setAbsolutePath(java.lang.String s)
Set the absolute path to this context. If not set explicitely, it'll be docBase ( if absolute ) or relative to "home" ( cm.getHome() ). DefaultCMSetter will "fix" the path.

getProperty

public java.lang.String getProperty(java.lang.String n)

setProperty

public void setProperty(java.lang.String n,
                        java.lang.String v)

setReloadable

public void setReloadable(boolean b)

getReloadable

public boolean getReloadable()
Should we reload servlets ?

setServletAPI

public void setServletAPI(java.lang.String s)
The servlet API variant that will be used for requests in this context

getServletAPI

public java.lang.String getServletAPI()

getWelcomeFiles

public java.lang.String[] getWelcomeFiles()
Return welcome files defined in web.xml or the defaults, if user doesn't define any

addWelcomeFile

public void addWelcomeFile(java.lang.String s)
Add an welcome file.

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)

addInitParameter

public void addInitParameter(java.lang.String name,
                             java.lang.String value)

getInitParameterNames

public java.util.Enumeration getInitParameterNames()

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Return an attribute value. "Special" attributes ( defined org.apache.tomcat ) are computed

getAttributeNames

public java.util.Enumeration getAttributeNames()

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object object)
XXX Use callbacks !!

removeAttribute

public void removeAttribute(java.lang.String name)

addTaglib

public void addTaglib(java.lang.String uri,
                      java.lang.String location)
Add a taglib declaration for this context

getTaglibLocation

public java.lang.String getTaglibLocation(java.lang.String uri)

getTaglibs

public java.util.Enumeration getTaglibs()

addEnvEntry

public void addEnvEntry(java.lang.String name,
                        java.lang.String type,
                        java.lang.String value,
                        java.lang.String description)
Add Env-entry to this context

getEnvEntryType

public java.lang.String getEnvEntryType(java.lang.String name)

getEnvEntryValue

public java.lang.String getEnvEntryValue(java.lang.String name)

getEnvEntries

public java.util.Enumeration getEnvEntries()

getDescription

public java.lang.String getDescription()

setDescription

public void setDescription(java.lang.String description)

setIcon

public void setIcon(java.lang.String icon)

isDistributable

public boolean isDistributable()

setDistributable

public void setDistributable(boolean isDistributable)

getSessionTimeOut

public int getSessionTimeOut()

setSessionTimeOut

public void setSessionTimeOut(int sessionTimeOut)

getMimeMap

public java.net.FileNameMap getMimeMap()

addContentType

public void addContentType(java.lang.String ext,
                           java.lang.String type)

getErrorPage

public java.lang.String getErrorPage(int errorCode)

addErrorPage

public void addErrorPage(java.lang.String errorType,
                         java.lang.String value)

getErrorPage

public java.lang.String getErrorPage(java.lang.String errorCode)

getAuthMethod

public java.lang.String getAuthMethod()
Authentication method, if any specified

getRealmName

public java.lang.String getRealmName()
Realm to be used

getFormLoginPage

public java.lang.String getFormLoginPage()

getFormErrorPage

public java.lang.String getFormErrorPage()

setFormLoginPage

public void setFormLoginPage(java.lang.String page)

setFormErrorPage

public void setFormErrorPage(java.lang.String page)

setLoginConfig

public void setLoginConfig(java.lang.String authMethod,
                           java.lang.String realmName,
                           java.lang.String formLoginPage,
                           java.lang.String formErrorPage)

getContainers

public java.util.Enumeration getContainers()

getContainerLocations

public java.util.Enumeration getContainerLocations()
Return an enumeration of Strings, representing all URLs ( relative to this context ) having associated properties ( handlers, security, etc)

getContainer

public Container getContainer(java.lang.String path)
Return the container ( properties ) associated with a path ( relative to this context )

removeContainer

public void removeContainer(Container ct)
                     throws TomcatException
Remove a container

addServlet

public void addServlet(Handler wrapper)
                throws TomcatException
Add a servlet. Servlets are mapped by name. This method is used to maintain the list of declared servlets, that can be used for mappings.

addHandler

public void addHandler(Handler wrapper)
                throws TomcatException
Add a servlet. Servlets are mapped by name. This method is used to maintain the list of declared servlets, that can be used for mappings.

removeHandler

public void removeHandler(Handler handler)
                   throws TomcatException

removeServletByName

public void removeServletByName(java.lang.String servletName)
                         throws TomcatException
Remove the servlet with a specific name

getServletByName

public Handler getServletByName(java.lang.String servletName)

getServletNames

public java.util.Enumeration getServletNames()
Return all servlets registered with this Context The elements will be of type Handler ( or sub-types )

getClassLoader

public java.lang.ClassLoader getClassLoader()
The current class loader. This value may change if reload is used, you shouldn't cache the result

setClassLoader

public void setClassLoader(java.lang.ClassLoader cl)

addClassPath

public void addClassPath(java.net.URL url)

getClassPath

public java.net.URL[] getClassPath()
Returns the full classpath - concatenation of ContextManager classpath and locally specified class path

setDebug

public void setDebug(int level)

getDebug

public int getDebug()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getId

public java.lang.String getId()

log

public void log(java.lang.String msg)
Internal log method

log

public void log(java.lang.String msg,
                java.lang.Throwable t)
Internal log method

log

public void log(java.lang.String msg,
                java.lang.Throwable t,
                int level)
Internal log method

logServlet

public void logServlet(java.lang.String msg,
                       java.lang.Throwable t)
User-level log method ( called from a servlet). Context supports 2 log streams - one is used by the tomcat core ( internals ) and one is used by servlets

setLog

public void setLog(Log logger)

setServletLog

public void setServletLog(Log logger)

getLog

public Log getLog()

getServletLog

public Log getServletLog()

setEngineHeader

public void setEngineHeader(java.lang.String s)
What is reported in the "Servlet-Engine" header for this context. It is set automatically by a facade interceptor. XXX Do we want to allow user to customize it ?

getEngineHeader

public java.lang.String getEngineHeader()

setWorkDir

public void setWorkDir(java.lang.String workDir)
Work dir is a place where servlets are allowed to write

getWorkDir

public java.io.File getWorkDir()

setWorkDir

public void setWorkDir(java.io.File workDir)

addHostAlias

public void addHostAlias(java.lang.String alias)
Virtual host support - this context will be part of a virtual host with the specified name. You should set all the aliases. XXX Not implemented

getHostAliases

public java.util.Enumeration getHostAliases()

setTrusted

public void setTrusted(boolean t)
Mark the webapplication as trusted, i.e. it can access internal objects and manipulate tomcat core

isTrusted

public boolean isTrusted()

addInterceptor

public void addInterceptor(BaseInterceptor ri)
                    throws TomcatException
Add a per-context interceptor. The hooks defined will be used only for requests that are matched in this context. contextMap hook is not called ( since the context is not known at that time. This method will only store the interceptor. No action takes place before the context is added ( since contextM may be unknown ).


Copyright © 2001 Apache Software Foundation. All Rights Reserved.