君子博学而日参省乎己 则知明而行无过矣

博客园 首页 新随笔 联系 订阅 管理

本系列转载自 http://blog.csdn.net/haitao111313/article/category/1179996 

  我们知道,在Tomcat的世界里,一个Host容器代表一个虚机器资源,Context容器代表一个应用,所谓的部署器就是能够把Context容器添加进Host容器中去的一个组件。显然,一个Host容器应该拥有一个部署器组件。简单的部署代码应该是下面这样的:

  1. Context context = new StandardContext();  
  2. Host host = new StandardHost();  
  3. host.addChild(context);  
别看这简单,其实这就是核心的部署代码。当然,Tomcat的部署器绝不是这么点东西,但其实也是比较简单的东西。在Catalina的createStartDigester()方法中(具体怎么调用到这个方法,详细参考Tomcat源码分析(一)--服务启动),向StandardHost容器中添加了一个HostConfig的实例。HostConfig类实现了LifecycleListener接口,也就是说它是个监听器类,能监听到组件的生命周期事件(有关生命周期的东西请参看 Tomcat源码分析(七)--单一启动/关闭机制(生命周期))。  下面看接受事件的方法lifecycleEvent(LifecycleEvent)做了写什么工作:

  1. public void lifecycleEvent(LifecycleEvent event) {  
  2.   
  3.         // Identify the host we are associated with  
  4.         try {  
  5.             host = (Host) event.getLifecycle();  
  6.             if (host instanceof StandardHost) { //如果监听到的事件对象类型是StandardHost就设置相关属性。  
  7.                 int hostDebug = ((StandardHost) host).getDebug();  
  8.                 if (hostDebug > this.debug) {  
  9.                     this.debug = hostDebug;  
  10.                 }  
  11.                 setDeployXML(((StandardHost) host).isDeployXML());//是否发布xml文件的标识,默认为true  
  12.                 setLiveDeploy(((StandardHost) host).getLiveDeploy());//是否动态部署标识,默认为true  
  13.                 setUnpackWARs(((StandardHost) host).isUnpackWARs());//是否要将war文件解压缩,默认为true  
  14.             }  
  15.         } catch (ClassCastException e) {  
  16.             log(sm.getString("hostConfig.cce", event.getLifecycle()), e);  
  17.             return;  
  18.         }  
  19.   
  20.         // Process the event that has occurred  
  21.         if (event.getType().equals(Lifecycle.START_EVENT)) //监听到容器开始,则调用start方法,方法里面调用了部署应用的代码  
  22.             start();  
  23.         else if (event.getType().equals(Lifecycle.STOP_EVENT))  
  24.             stop();  
  25.   
  26.     }  
如果监听到StandardHost容器启动开始了,则调用start方法来,下面看start方法:

  1. protected void start() {  
  2.   
  3.        if (debug >= 1)  
  4.            log(sm.getString("hostConfig.start"));  
  5.   
  6.        if (host.getAutoDeploy()) {  
  7.            deployApps();//发布应用  
  8.        }  
  9.   
  10.        if (isLiveDeploy()) {  
  11.            threadStart();//动态发布应用,因为HostConfig也实现了Runnable接口,threadStart启动该线程来实现动态发布  
  12.        }  
  13.   
  14.    }  
  15.   --------------------》deployApps方法,该方法会把webapps目录下的所有目录都看作成一个应用程序  
  16.     protected void deployApps() {  
  17.   
  18.        if (!(host instanceof Deployer))  
  19.            return;  
  20.        if (debug >= 1)  
  21.            log(sm.getString("hostConfig.deploying"));  
  22.   
  23.        File appBase = appBase();//返回webapps目录  
  24.        if (!appBase.exists() || !appBase.isDirectory())  
  25.            return;  
  26.        String files[] = appBase.list();//列出webapps目录下的所有文件  
  27.   
  28.        deployDescriptors(appBase, files);//通过描述符发布应用  
  29.        deployWARs(appBase, files);//发布war文件的应用  
  30.        deployDirectories(appBase, files);//发布目录型的应用  
  31.   
  32.    }  

以上三个发布应用的方式大同小异,所以只说说常用的发布方式--目录型的应用,下面看看deployDirectories方法,只写了关键的逻辑:

  1. protected void deployDirectories(File appBase, String[] files) {  
  2.   
  3.      for (int i = 0; i < files.length; i++) {  
  4.   
  5.          if (files[i].equalsIgnoreCase("META-INF"))  
  6.              continue;  
  7.          if (files[i].equalsIgnoreCase("WEB-INF"))  
  8.              continue;  
  9.          if (deployed.contains(files[i]))  
  10.              continue;  
  11.          File dir = new File(appBase, files[i]);  
  12.          if (dir.isDirectory()) {  
  13.   
  14.              deployed.add(files[i]);  
  15.   
  16.              // Make sure there is an application configuration directory  
  17.              // This is needed if the Context appBase is the same as the  
  18.              // web server document root to make sure only web applications  
  19.              // are deployed and not directories for web space.  
  20.              File webInf = new File(dir, "/WEB-INF");  
  21.              if (!webInf.exists() || !webInf.isDirectory() ||  
  22.                  !webInf.canRead())  
  23.                  continue;  
  24.   
  25.              // Calculate the context path and make sure it is unique  
  26.              String contextPath = "/" + files[i];  
  27.              if (files[i].equals("ROOT"))  
  28.                  contextPath = "";  
  29.              if (host.findChild(contextPath) != null)  
  30.                  continue;  
  31.   
  32.              // Deploy the application in this directory  
  33.              log(sm.getString("hostConfig.deployDir", files[i]));  
  34.              try {  
  35.                  URL url = new URL("file"null, dir.getCanonicalPath());//得到应用的路径,路径的写法是   file://应用名称  
  36.                  ((Deployer) host).install(contextPath, url); //安装应用到目录下  
  37.              } catch (Throwable t) {  
  38.                  log(sm.getString("hostConfig.deployDir.error", files[i]),  
  39.                      t);  
  40.              }  
  41.   
  42.          }  
  43.   
  44.      }  
  45.   
  46.  }  

((Deployer) host).install(contextPath, url);会调用到StandardHost的install方法,再由StandardHost转交给StandardHostDeployer的install方法,StandardHostDeployer是一个辅助类,帮助StandardHost来实现发布应用,它实现了Deployer接口,看它的install(URL config, URL war)方法(它有两个install方法,分别用来发布上面不同方式的应用):

  1. public synchronized void install(String contextPath, URL war)  
  2.        throws IOException {  
  3.   
  4.      ..............................................  
  5.   
  6.        // Calculate the document base for the new web application  
  7.        host.log(sm.getString("standardHost.installing",  
  8.                              contextPath, war.toString()));  
  9.        String url = war.toString();  
  10.        String docBase = null;  
  11.        if (url.startsWith("jar:")) {   //如果是war类型的应用  
  12.            url = url.substring(4, url.length() - 2);  
  13.        }  
  14.        if (url.startsWith("file://"))//如果是目录类型的应用  
  15.            docBase = url.substring(7);  
  16.        else if (url.startsWith("file:"))  
  17.            docBase = url.substring(5);  
  18.        else  
  19.            throw new IllegalArgumentException  
  20.                (sm.getString("standardHost.warURL", url));  
  21.   
  22.        // Install the new web application  
  23.        try {  
  24.            Class clazz = Class.forName(host.getContextClass());//host.getContextClass得到的其实是StandardContext,  
  25.            Context context = (Context) clazz.newInstance();  
  26.            context.setPath(contextPath);//设置该context的访问路径为contextPath,即我们的应用访问路径  
  27.              
  28.            context.setDocBase(docBase);//设置该应用在磁盘的路径  
  29.            if (context instanceof Lifecycle) {  
  30.                clazz = Class.forName(host.getConfigClass());//实例化host的监听器类,并关联上context  
  31.                LifecycleListener listener =  
  32.                    (LifecycleListener) clazz.newInstance();  
  33.                ((Lifecycle) context).addLifecycleListener(listener);  
  34.            }  
  35.            host.fireContainerEvent(PRE_INSTALL_EVENT, context);  
  36.            host.addChild(context);           //添加到host实例,即把context应用发布到host。  
  37.            host.fireContainerEvent(INSTALL_EVENT, context);  
  38.        } catch (Exception e) {  
  39.            host.log(sm.getString("standardHost.installError", contextPath),  
  40.                     e);  
  41.            throw new IOException(e.toString());  
  42.        }  
  43.   
  44.    }  

经过上面的代码分析,已经完全了解了怎么发布一个目录型的应用到StandardHost中,其他war包和文件描述符类型的应用发布跟StandardHost大体类似,在这里就不说了,有兴趣的可以自己查看源代码。
posted on 2012-12-02 17:18  刺猬的温驯  阅读(262)  评论(0编辑  收藏  举报