一個上下文容器(Context)代表一個web應用,每一個上下文包括多個包裝器(Wrapper),每個包裝器代表一個Servlet
上下文還需要其它的一些組件如加載器和管理器
Context接口的標準實現,org.apache.catalina.core.StandardContext類
StandardContext配置
創建一個StandardContext實例之後,必須調用它的start方法,這樣它就能爲受到的HTTP請求服務了。一個StandardContext對象可能啓動失敗,
這時候屬性available被設置爲false,屬性available表示了StandardContext對象的可用性。
在一個Tomcat部署中,StandardContext的配置過程做了以下事情:準備讀取和解析%CATALINA_HOME%/conf 目錄下面的web.xml,
部署所有應用程序,確保StandardContext實例可以處理應用級別的web.xml。
另外,配置需要添加一個驗證器閥門和證書閥門(authenticator valve and a certificate valve)
StandardContext的屬性之一是它屬性configured,用來表示該StandardContext是否已經配置了
StandardContext使用一個事件監聽器來作爲它的配置器
當StandardContext實例的start方法被調用的時候,首先觸發一個生命週期事件
。該事件喚醒一個監聽器來配置該StandardContext實例。配置成功後,該監聽器將configured屬性設置爲true。
否則,StandardContext對象拒絕啓動,這樣就不能對HTTP請求進行服務了。
StandardContext構造函數
public StandardContext() {
super();
pipeline.setBasic(new StandardContextValve());
namingResources.setContainer(this);
}
在構造函數中,最重要的事情是在StandardContext的流水線上添加了一個類型爲StandardContextValve的基本閥門
啓動StandardContext
Start方法初始化StandardContext對象並讓生命週期監聽器配置該StandardContext實例。如果配置成功,
生命週期監聽器會將configured屬性設置爲true。最後start方法,將available屬性設置爲true或者false。
如果是true的話表示該StandardContext屬性配置完畢並且所有相關子容器和組件已經成功啓動,
這樣就能對HTTP請求進行服務了,如果是false則表示出現了錯誤。
- public synchronized void start() throws LifecycleException {
- if (started)
- throw new LifecycleException (sm.getString("containerBase.alreadyStarted", logName()));
- lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
- setAvailable(false);
- setConfigured(false);
- boolean ok = true;
- if (getResources() == null) {
- try {
- if ((docBase != null) && (docBase.endsWith(".war")))
- setResources(new WARDirContext());
- else
- setResources(new FileDirContext());
- }catch (IllegalArgumentException e) {
- ok = false;
- }
- }
- if (ok && (resources instanceof ProxyDirContext)) {
- DirContext dirContext = ((ProxyDirContext) resources).getDirContext();
- if ((dirContext != null) && (dirContext instanceof BaseDirContext)) {
- ((BaseDirContext) dirContext).setDocBase(getBasePath());
- ((BaseDirContext) dirContext).allocate();
- }
- }
- if (getLoader() == null) {
- if (getPrivileged()) {
- setLoader(new WebappLoader(this.getClass().getClassLoader()));
- }else{
- setLoader(new WebappLoader(getParentClassLoader()));
- }
- }
- if (getManager() == null) {
- setManager(new StandardManager());
- }
- // Initialize character set mapper
- getCharsetMapper();
- // Post work directory
- postWorkDirectory();
- String useNamingProperty = System.getProperty("catalina.useNaming");
- if ((useNamingProperty != null) && (useNamingProperty.equals("false"))) {
- useNaming = false;
- }
- if (ok && isUseNaming()) {
- if (namingContextListener == null) {
- namingContextListener = new NamingContextListener();
- namingContextListener.setDebug(getDebug());
- namingContextListener.setName(getNamingContextName()); addLifecycleListener(namingContextListener);
- }
- }
- ClassLoader oldCCL = bindThread();
- if (ok) {
- try {
- addDefaultMapper(this.mapperClass);
- started = true;
- if ((loader != null) && (loader instanceof Lifecycle))
- ((Lifecycle) loader).start();
- if ((logger != null) && (logger instanceof Lifecycle))
- ((Lifecycle) logger).start();
- // Unbinding thread
- unbindThread(oldCCL);
- oldCCL = bindThread();
- if ((cluster != null) && (cluster instanceof Lifecycle))
- ((Lifecycle) cluster).start();
- if ((realm != null) && (realm instanceof Lifecycle))
- ((Lifecycle) realm).start();
- if ((resources != null) && (resources instanceof Lifecycle))
- ((Lifecycle) resources).start();
- Mapper mappers[] = findMappers();
- for (int i = 0; i < mappers.length; i++) {
- if (mappers[i] instanceof Lifecycle)
- ((Lifecycle) mappers[i]).start();
- }
- Container children[] = findChildren();
- for (int i = 0; i < children.length; i++) {
- if (children[i] instanceof Lifecycle)
- ((Lifecycle) children[i]).start();
- }
- if (pipeline instanceof Lifecycle)
- ((Lifecycle) pipeline).start();
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(START_EVENT, null);
- if ((manager != null) && (manager instanceof Lifecycle))
- ((Lifecycle) manager).start();
- } finally {
- // Unbinding thread
- unbindThread(oldCCL);
- }
- }
- if (!getConfigured())
- ok = false;
- if (ok)
- getServletContext().setAttribute (Globals.RESOURCES_ATTR, getResources());
- if (ok) {
- postWelcomeFiles();
- }
- if (ok) {
- if (!listenerStart())
- ok = false;
- }
- if (ok) {
- if (!filterStart())
- ok = false;
- }
- // Load and initialize all "load on startup" servlets
- if (ok)
- loadOnStartup(findChildren());
- unbindThread(oldCCL);
- if (ok){
- setAvailable(true);
- }else{
- stop();
- setAvailable(false);
- }
- lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
- }
下面是該方法做的事情:
· 觸發BEFORE_START事件
· 設置availability屬性爲false
· 設置configured屬性爲false
· 設置源(resources)
· 設置加載器
· 設置管理器
· 初始化屬性map
· 啓動跟該上下文相關的組件
· 啓動子容器(包裝器)
· 啓動流水線
· 啓動管理器
· 觸發START事件
。監聽器(ContextConfig)會進行一系列配置操作,配置成功後,將StandardContext實例的configured屬性設置爲true。
· 檢查configured屬性的值,如果爲true:調用postWelcomPages方法,加載子包裝器,並將available屬性設置爲true。
如果configured屬性爲false調用stop方法
· 觸發AFTER_START事件
Invoke方法
StandardContext's方法由相關聯的連接器調用,
如果該上下文是一個主機(host)的子容器,
有該主機的invoke方法調用。StandardContext的invoke方法首先檢查是否正在重加載該應用程序,
是的話,等待知道加載完畢。然後調用它的父類ContainerBase的invoke方法
- public void invoke(Request request, Response response){
- while (getPaused()) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) { ; }
- }
- if (swallowOutput) {
- try {
- SystemLogHandler.startCapture();
- super.invoke(request, response);
- }
- }else {
- super.invoke(request, response);
- }
- }
方法getPaused獲得屬性paused的值,當應用程序正在加載的時候該屬性爲ture。
StandardContextMapper
對於每一個請求,invoke方法都會調用StandarContext流水線基本閥門的invoke方法。
StandarContext的基本閥門用org.apache.catalina.core.StandardContextValve類表示。
StandardContextValve實例查找包含它的StandardContext。StandardContextValve使用上下文容器的map來查找合適的包裝器
StandardContext的父類ContainerBase定義了addDefaultMapper方法來添加
- protected void addDefaultMapper(String mapperClass) {
- if (mapperClass == null)
- return;
- if (mappers.size() >= 1)
- return;
- try {
- Class clazz = Class.forName(mapperClass);
- Mapper mapper = (Mapper) clazz.newInstance();
- mapper.setProtocol("http");
- addMapper(mapper);
- } catch (Exception e) {}
- }
重加載支持
StandardContext定義了reloadable屬性來標識是否支持應用程序的重加載。
當允許重加載的時候,當web.xml或者WEB-INF/classes目錄下的文件被改變的時候會重加載。
StandardContext 中Loader接口的標準實現WebappLoader類,有一個單獨線程來檢查WEB-INF目錄下面所有類和JAR文件的時間戳
你需要做的是啓動該線程,將 WebappLoader關聯到StandardContext,使用setContainer方法即可
- public void setContainer(Container container) {
- if ((this.container != null) && (this.container instanceof Context))
- ((Context) this.container).removePropertyChangeListener(this);
- Container oldContainer = this.container;
- this.container = container;
- support.firePropertyChange("container", oldContainer, this.container);
- // Register with the new Container (if any)
- if ((this.container!=null) && (this.container instanceof Context)) {
- setReloadable( ((Context) this.container).getReloadable() );
- ((Context) this.container).addPropertyChangeListener(this);
- }
- }
注意最後一個if語句塊中,如果容器是一個上下文容器,調用setReloadable方法,
也就是說WebappLoader的reloadable屬性跟StandardContext的reloadable屬性相同。
下面是WebappLoader對setReload方法的實現:
- public void setReloadable(boolean reloadable) {
- boolean oldReloadable = this.reloadable;
- this.reloadable = reloadable;
- support.firePropertyChange("reloadable", new Boolean(oldReloadable), new Boolean(this.reloadable));
- if (!started)
- return;
- if (!oldReloadable && this.reloadable)
- threadStart();
- else if (oldReloadable && !this.reloadable)
- threadStop();
- }
- }
如果將reloadable屬性設置爲true,調用threadStart方法。如果從true到false,則調用threadStop方法。
threadStart方法啓動一個線程持續的檢查WEB-INF目錄下面的類文件和JAR文件的時間戳。threadStop方法用於停止該線程。
類的時間戳是由backgroundProcess方法調用
backgroundProcess方法
一個上下文容器需要其它組件如加載器和管理器的支持。這些組件通常需要一個單獨的線程來處理後臺過程(background processing)
所有的後臺過程都分享同一個線程。如果一個組件或者是容器需要定期的來執行操作,
它需要做的是將這些代碼寫入到backgroundProcess方法即可。
- protected void threadStart() {
- if (thread != null)
- return;
- if (backgroundProcessorDelay <= 0)
- return;
- threadDone = false;
- String threadName = "ContainerBackgroundProcessor[" + toString() + "]";
- thread = new Thread(new ContainerBackgroundProcessor(), threadName);
- thread.setDaemon(true);
- thread.start();
- }
方法threadStart傳遞一個ContainerBackgroundProcessor對象創建一個新線程。
ContainerBackgroundProcessor實現了java.lang.Runnable接口
- protected class ContainerBackgroundProcessor implements Runnable {
- public void run() {
- while (!threadDone) {
- try {
- Thread.sleep(backgroundProcessorDelay * 1000L);
- } catch (InterruptedException e) { ; }
- if (!threadDone) {
- Container parent = (Container) getMappingObject();
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- if (parent.getLoader() != null) {
- cl = parent.getLoader().getClassLoader();
- }
- processChildren(parent, cl);
- }
- }
- }
- protected void processChildren(Container container, ClassLoader cl) {
- try {
- if (container.getLoader() != null) {
- Thread.currentThread().setContextClassLoader (container.getLoader().getClassLoader());
- }
- container.backgroundProcess();
- }catch (Throwable t) {}
- finally {
- Thread.currentThread().setContextClassLoader(cl);
- }
- Container[] children = container.findChildren();
- for (int i = 0; i < children.length; i++) {
- if (children[i].getBackgroundProcessorDelay() <= 0) {
- processChildren(children[i], cl);
- }
- }
- }
- }
ContainerBackgroundProcessor是ContainerBase的內部類,
在他的run方法裏,有一個while循環定期的調用它的processChildren方法