tomcat中StandardContext

来源:互联网 发布:网线端口显示红灯 编辑:程序博客网 时间:2024/06/11 15:44

一个上下文容器(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则表示出现了错误。

Java代码  收藏代码
  1. public synchronized void start() throws LifecycleException {  
  2.     if (started)  
  3.         throw new LifecycleException (sm.getString("containerBase.alreadyStarted", logName()));  
  4.     lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);  
  5.     setAvailable(false);  
  6.     setConfigured(false);  
  7.     boolean ok = true;  
  8.     if (getResources() == null) {  
  9.         try {  
  10.             if ((docBase != null) && (docBase.endsWith(".war")))   
  11.                 setResources(new WARDirContext());  
  12.             else   
  13.                 setResources(new FileDirContext());  
  14.         }catch (IllegalArgumentException e) {  
  15.             ok = false;  
  16.         }  
  17.     }  
  18.     if (ok && (resources instanceof ProxyDirContext)) {  
  19.         DirContext dirContext = ((ProxyDirContext) resources).getDirContext();  
  20.         if ((dirContext != null) && (dirContext instanceof BaseDirContext)) {  
  21.             ((BaseDirContext) dirContext).setDocBase(getBasePath());   
  22.             ((BaseDirContext) dirContext).allocate();  
  23.         }  
  24.     }  
  25.     if (getLoader() == null) {  
  26.         if (getPrivileged()) {  
  27.             setLoader(new WebappLoader(this.getClass().getClassLoader()));  
  28.         }else{  
  29.             setLoader(new WebappLoader(getParentClassLoader()));  
  30.         }  
  31.     }  
  32.     if (getManager() == null) {  
  33.         setManager(new StandardManager());  
  34.     }  
  35.     // Initialize character set mapper   
  36.     getCharsetMapper();  
  37.     // Post work directory   
  38.     postWorkDirectory();  
  39.     String useNamingProperty = System.getProperty("catalina.useNaming");  
  40.     if ((useNamingProperty != null) && (useNamingProperty.equals("false"))) {  
  41.         useNaming = false;  
  42.     }  
  43.     if (ok && isUseNaming()) {  
  44.         if (namingContextListener == null) {  
  45.             namingContextListener = new NamingContextListener();   
  46.             namingContextListener.setDebug(getDebug());   
  47.             namingContextListener.setName(getNamingContextName()); addLifecycleListener(namingContextListener);  
  48.         }  
  49.     }  
  50.     ClassLoader oldCCL = bindThread();  
  51.     if (ok) {  
  52.         try {   
  53.             addDefaultMapper(this.mapperClass);   
  54.             started = true;  
  55.             if ((loader != null) && (loader instanceof Lifecycle))   
  56.                 ((Lifecycle) loader).start();  
  57.             if ((logger != null) && (logger instanceof Lifecycle))  
  58.                 ((Lifecycle) logger).start();  
  59.             // Unbinding thread  
  60.             unbindThread(oldCCL);  
  61.             oldCCL = bindThread();  
  62.             if ((cluster != null) && (cluster instanceof Lifecycle))   
  63.                 ((Lifecycle) cluster).start();  
  64.             if ((realm != null) && (realm instanceof Lifecycle))   
  65.                 ((Lifecycle) realm).start();  
  66.             if ((resources != null) && (resources instanceof Lifecycle))   
  67.                 ((Lifecycle) resources).start();  
  68.               
  69.             Mapper mappers[] = findMappers();  
  70.             for (int i = 0; i < mappers.length; i++) {  
  71.                 if (mappers[i] instanceof Lifecycle)   
  72.                     ((Lifecycle) mappers[i]).start();  
  73.             }  
  74.             Container children[] = findChildren();  
  75.             for (int i = 0; i < children.length; i++) {  
  76.                 if (children[i] instanceof Lifecycle)  
  77.                     ((Lifecycle) children[i]).start();  
  78.             }  
  79.             if (pipeline instanceof Lifecycle)   
  80.                 ((Lifecycle) pipeline).start();  
  81.             // Notify our interested LifecycleListeners  
  82.             lifecycle.fireLifecycleEvent(START_EVENT, null);  
  83.             if ((manager != null) && (manager instanceof Lifecycle))  
  84.                 ((Lifecycle) manager).start();  
  85.         } finally {  
  86.             // Unbinding thread   
  87.             unbindThread(oldCCL);  
  88.         }  
  89.     }  
  90.     if (!getConfigured())  
  91.         ok = false;  
  92.     if (ok)   
  93.         getServletContext().setAttribute (Globals.RESOURCES_ATTR, getResources());  
  94.     if (ok) {  
  95.         postWelcomeFiles();  
  96.     }  
  97.     if (ok) {   
  98.         if (!listenerStart())  
  99.             ok = false;  
  100.     }  
  101.     if (ok) {  
  102.         if (!filterStart())   
  103.             ok = false;  
  104.     }  
  105.     // Load and initialize all "load on startup" servlets   
  106.     if (ok)   
  107.         loadOnStartup(findChildren());  
  108.     unbindThread(oldCCL);  
  109.     if (ok){  
  110.         setAvailable(true);  
  111.     }else{  
  112.         stop();  
  113.         setAvailable(false);  
  114.     }  
  115.     lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);  
  116. }  

 

 

下面是该方法做的事情:
· 触发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方法

Java代码  收藏代码
  1. public void invoke(Request request, Response response){  
  2.     while (getPaused()) {  
  3.         try {  
  4.             Thread.sleep(1000);   
  5.         } catch (InterruptedException e) { ; }  
  6.     }  
  7.     if (swallowOutput) {   
  8.         try {  
  9.             SystemLogHandler.startCapture();  
  10.             super.invoke(request, response);  
  11.         }  
  12.     }else {   
  13.         super.invoke(request, response);   
  14.     }  
  15. }  

 

 

方法getPaused获得属性paused的值,当应用程序正在加载的时候该属性为ture。


StandardContextMapper
对于每一个请求,invoke方法都会调用StandarContext流水线基本阀门的invoke方法。
StandarContext的基本阀门用org.apache.catalina.core.StandardContextValve类表示。
StandardContextValve实例查找包含它的StandardContext。StandardContextValve使用上下文容器的map来查找合适的包装器
StandardContext的父类ContainerBase定义了addDefaultMapper方法来添加

Java代码  收藏代码
  1. protected void addDefaultMapper(String mapperClass) {  
  2.     if (mapperClass == null)   
  3.         return;  
  4.     if (mappers.size() >= 1)  
  5.         return;  
  6.     try {   
  7.         Class clazz = Class.forName(mapperClass);  
  8.         Mapper mapper = (Mapper) clazz.newInstance();  
  9.         mapper.setProtocol("http");   
  10.         addMapper(mapper);  
  11.     } catch (Exception e) {}  
  12. }  

 

 

重加载支持
StandardContext定义了reloadable属性来标识是否支持应用程序的重加载。
当允许重加载的时候,当web.xml或者WEB-INF/classes目录下的文件被改变的时候会重加载。
StandardContext 中Loader接口的标准实现WebappLoader类,有一个单独线程来检查WEB-INF目录下面所有类和JAR文件的时间戳
你需要做的是启动该线程,将 WebappLoader关联到StandardContext,使用setContainer方法即可

Java代码  收藏代码
  1. public void setContainer(Container container) {  
  2.     if ((this.container != null) && (this.container instanceof Context))   
  3.         ((Context) this.container).removePropertyChangeListener(this);  
  4.     Container oldContainer = this.container;  
  5.     this.container = container;  
  6.     support.firePropertyChange("container", oldContainer, this.container);  
  7.     // Register with the new Container (if any)   
  8.     if ((this.container!=null) && (this.container instanceof Context)) {  
  9.         setReloadable( ((Context) this.container).getReloadable() );   
  10.         ((Context) this.container).addPropertyChangeListener(this);   
  11.     }  
  12. }  

 

 


注意最后一个if语句块中,如果容器是一个上下文容器,调用setReloadable方法,
也就是说WebappLoader的reloadable属性跟StandardContext的reloadable属性相同。
下面是WebappLoader对setReload方法的实现:

 

Java代码  收藏代码
  1. public void setReloadable(boolean reloadable) {  
  2.     boolean oldReloadable = this.reloadable;  
  3.     this.reloadable = reloadable;  
  4.     support.firePropertyChange("reloadable"new Boolean(oldReloadable), new Boolean(this.reloadable));  
  5.     if (!started)  
  6.         return;  
  7.     if (!oldReloadable && this.reloadable)  
  8.         threadStart();  
  9.     else if (oldReloadable && !this.reloadable)  
  10.         threadStop();  
  11.     }  
  12. }  

 

如果将reloadable属性设置为true,调用threadStart方法。如果从true到false,则调用threadStop方法。
threadStart方法启动一个线程持续的检查WEB-INF目录下面的类文件和JAR文件的时间戳。threadStop方法用于停止该线程。
类的时间戳是由backgroundProcess方法调用

backgroundProcess方法
一个上下文容器需要其它组件如加载器和管理器的支持。这些组件通常需要一个单独的线程来处理后台过程(background processing)
所有的后台过程都分享同一个线程。如果一个组件或者是容器需要定期的来执行操作,
它需要做的是将这些代码写入到backgroundProcess方法即可。

 

Java代码  收藏代码
  1. protected void threadStart() {  
  2.     if (thread != null)   
  3.         return;  
  4.     if (backgroundProcessorDelay <= 0)  
  5.         return;  
  6.     threadDone = false;   
  7.     String threadName = "ContainerBackgroundProcessor[" + toString() + "]";  
  8.     thread = new Thread(new ContainerBackgroundProcessor(), threadName);  
  9.     thread.setDaemon(true);   
  10.     thread.start();  
  11. }  

 


方法threadStart传递一个ContainerBackgroundProcessor对象创建一个新线程。
ContainerBackgroundProcessor实现了java.lang.Runnable接口

Java代码  收藏代码
  1. protected class ContainerBackgroundProcessor implements Runnable {  
  2.     public void run() {  
  3.         while (!threadDone) {  
  4.             try {  
  5.                 Thread.sleep(backgroundProcessorDelay * 1000L);  
  6.             } catch (InterruptedException e) { ; }  
  7.             if (!threadDone) {  
  8.                 Container parent = (Container) getMappingObject();  
  9.                 ClassLoader cl = Thread.currentThread().getContextClassLoader();  
  10.                 if (parent.getLoader() != null) {   
  11.                     cl = parent.getLoader().getClassLoader();   
  12.                 }  
  13.                 processChildren(parent, cl);  
  14.             }  
  15.         }  
  16.     }  
  17.   
  18.     protected void processChildren(Container container, ClassLoader cl) {  
  19.         try {   
  20.             if (container.getLoader() != null) {  
  21.                 Thread.currentThread().setContextClassLoader (container.getLoader().getClassLoader());  
  22.             }  
  23.             container.backgroundProcess();  
  24.         }catch (Throwable t) {}  
  25.         finally {  
  26.             Thread.currentThread().setContextClassLoader(cl);  
  27.         }  
  28.         Container[] children = container.findChildren();  
  29.         for (int i = 0; i < children.length; i++) {  
  30.             if (children[i].getBackgroundProcessorDelay() <= 0) {  
  31.                 processChildren(children[i], cl);  
  32.             }  
  33.         }  
  34.     }  
  35. }  

 

ContainerBackgroundProcessor是ContainerBase的内部类,
在他的run方法里,有一个while循环定期的调用它的processChildren方法

0 0
原创粉丝点击