`
wu_quanyin
  • 浏览: 204420 次
  • 性别: Icon_minigender_1
  • 来自: 福建省
社区版块
存档分类
最新评论

Tomcat源码---容器启动六(3)

阅读更多

一,容器已经启动到部暑文件(webapps),接下去是StandardContext,standardWarpper还有Connector等的启动

我们来了解一下部暑war文件

 

   // Deploy WARs, and loop if additional descriptors are found

       //appBase:webapps File appBase.list:所存放的工程
        deployWARs(appBase, appBase.list());

  /**

 

     * Deploy WAR files.
     */
    protected void deployWARs(File appBase, String[] files) {
        
        if (files == null)
            return;
        
        for (int i = 0; i < files.length; i++) {
            
            if (files[i].equalsIgnoreCase("META-INF"))
                continue;
            if (files[i].equalsIgnoreCase("WEB-INF"))
                continue;
            File dir = new File(appBase, files[i]);
            if (files[i].toLowerCase().endsWith(".war") && dir.isFile()) {
                
                // Calculate the context path and make sure it is unique
                String contextPath = "/" + files[i].replace('#','/');
                int period = contextPath.lastIndexOf(".");
                if (period >= 0)
                    contextPath = contextPath.substring(0, period);
                if (contextPath.equals("/ROOT"))
                    contextPath = "";
                
                if (isServiced(contextPath))
                    continue;
                
                String file = files[i];
                //以上是对每一个工程名以及路径进行解析
                //这一步才进行部暑
                deployWAR(contextPath, dir, file);
                
            }
            
        }
        
    }

  /**

     * @param contextPath
     * @param war
     * @param file
     */
    protected void deployWAR(String contextPath, File war, String file) {
        
        if (deploymentExists(contextPath))
            return;
        
        // Checking for a nested /META-INF/context.xml
        JarFile jar = null;
        JarEntry entry = null;
        InputStream istream = null;
        BufferedOutputStream ostream = null;
        File xml = new File
            (configBase, file.substring(0, file.lastIndexOf(".")) + ".xml");
        if (deployXML && !xml.exists()) {
            try {
                jar = new JarFile(war);
                entry = jar.getJarEntry(Constants.ApplicationContextXml);
                if (entry != null) {
                    istream = jar.getInputStream(entry);
                    
                    configBase.mkdirs();
                    
                    ostream =
                        new BufferedOutputStream
                        (new FileOutputStream(xml), 1024);
                    byte buffer[] = new byte[1024];
                    while (true) {
                        int n = istream.read(buffer);
                        if (n < 0) {
                            break;
                        }
                        ostream.write(buffer, 0, n);
                    }
                    ostream.flush();
                    ostream.close();
                    ostream = null;
                    istream.close();
                    istream = null;
                    entry = null;
                    jar.close();
                    jar = null;
                }
            } catch (Exception e) {
                // Ignore and continue
                if (ostream != null) {
                    try {
                        ostream.close();
                    } catch (Throwable t) {
                        ;
                    }
                    ostream = null;
                }
                if (istream != null) {
                    try {
                        istream.close();
                    } catch (Throwable t) {
                        ;
                    }
                    istream = null;
                }
            } finally {
                entry = null;
                if (jar != null) {
                    try {
                        jar.close();
                    } catch (Throwable t) {
                        ;
                    }
                    jar = null;
                }
            }
        }
        //这个是用来存放已经部暑好的文件
        DeployedApplication deployedApp = new DeployedApplication(contextPath);
        
        // Deploy the application in this WAR file
        if(log.isInfoEnabled()) 
            log.info(sm.getString("hostConfig.deployJar", file));

        try {
            Context context = null;
            if (deployXML && xml.exists()) {
                synchronized (digester) {
                    try {
                        context = (Context) digester.parse(xml);
                        if (context == null) {
                            log.error(sm.getString("hostConfig.deployDescriptor.error",
                                    file));
                            return;
                        }
                    } finally {
                        digester.reset();
                    }
                }
                context.setConfigFile(xml.getAbsolutePath());
                deployedApp.redeployResources.put
                    (xml.getAbsolutePath(), new Long(xml.lastModified()));
            } else {
                context = (Context) Class.forName(contextClass).newInstance();
            }

            // Populate redeploy resources with the WAR file
            deployedApp.redeployResources.put
                (war.getAbsolutePath(), new Long(war.lastModified()));

            if (context instanceof Lifecycle) {
                Class clazz = Class.forName(host.getConfigClass());
                LifecycleListener listener =
                    (LifecycleListener) clazz.newInstance();
                ((Lifecycle) context).addLifecycleListener(listener);
            }
            context.setPath(contextPath);
            context.setDocBase(file);
            //以下这一步跟进去,StandardContext的启动
            host.addChild(context);
            // If we're unpacking WARs, the docBase will be mutated after
            // starting the context
            if (unpackWARs && (context.getDocBase() != null)) {
                String name = null;
                String path = context.getPath();
                if (path.equals("")) {
                    name = "ROOT";
                } else {
                    if (path.startsWith("/")) {
                        name = path.substring(1);
                    } else {
                        name = path;
                    }
                }
                name = name.replace('/', '#');
                File docBase = new File(name);
                if (!docBase.isAbsolute()) {
                    docBase = new File(appBase(), name);
                }
 		//将部暑完的工程存放进该map中
                deployedApp.redeployResources.put(docBase.getAbsolutePath(),
                        new Long(docBase.lastModified()));
                addWatchedResources(deployedApp, docBase.getAbsolutePath(), context);
            } else {
                addWatchedResources(deployedApp, null, context);
            }
        } catch (Throwable t) {
            log.error(sm.getString("hostConfig.deployJar.error", file), t);
        }
        
        deployed.put(contextPath, deployedApp);
    }

  //以下这一步跟进去,StandardContext的启动

            host.addChild(context);
private void addChildInternal(Container child) {

        if( log.isDebugEnabled() )
            log.debug("Add child " + child + " " + this);
        synchronized(children) {
            if (children.get(child.getName()) != null)
                throw new IllegalArgumentException("addChild:  Child name '" +
                                                   child.getName() +
                                                   "' is not unique");
            child.setParent(this);  // May throw IAE
            children.put(child.getName(), child);

            // Start child
            if (started && startChildren && (child instanceof Lifecycle)) {
                boolean success = false;
                try {
		    //StandardContext的启动
                    ((Lifecycle) child).start();
                    success = true;
                } catch (LifecycleException e) {
                    log.error("ContainerBase.addChild: start: ", e);
                    throw new IllegalStateException
                        ("ContainerBase.addChild: start: " + e);
                } finally {
                    if (!success) {
                        children.remove(child.getName());
                    }
                }
            }

            fireContainerEvent(ADD_CHILD_EVENT, child);
        }

    }

 StandardContext#start//由于里面的方法过长,就对里面的个别调用进行详解

 public synchronized void start() throws LifecycleException {
     if( !initialized ) { 
            try {
               //war文件解压缩成工程就是在这步执行
                init();
            } catch( Exception ex ) {
                throw new LifecycleException("Error initializaing ", ex);
            }
        }
	
}

 以上是对环境部暑启动的简单调试过程,没能全部理解清楚,,会根据后面的调试补全

主要是做了把工程文件存放在DeployedApplication这个类里的  deployedApp.redeployResources.put(docBase.getAbsolutePath(),

                        new Long(docBase.lastModified()));以备访问调用 

0
0
分享到:
评论

相关推荐

    tomcat 源码分析系列文档

    tomcat 源码分析系列文档 http深入分析,tomcat中类的加载,tomcat的启动,tomcat的容器思想,

    3-8Tomcat请求容器中的处理与启动过程源码实现(1).mp4

    3-8Tomcat请求容器中的处理与启动过程源码实现(1).mp4

    apache-tomcat-8.5.50-src.zip

    Tomcat启动流程分析 组件的生命周期管理 用Lifecycle管理启动、停止、关闭 Lifecycle接口预览 几个核心方法 Server中的init方法示例 为啥StandardServer没有init方法 LifecycleBase中的init与...

    3-8Tomcat请求容器中的处理与启动过程源码实现(2).mp4

    3-8Tomcat请求容器中的处理与启动过程源码实现(2).mp4

    Tomcat源码研究.pdf

    Tomcat源码研究.pdf。Catalina脚本解析,Tomcat启动遇到的常见问题,架构探讨,JMX在tomcat中的应用,容器初探,生命周期

    基于内嵌apache-tomcat-8.0.24的应用开发

    开发人员无需搭建Tomcat的环境就可以使用内嵌式Tomcat进行开发,减少搭建J2EE容器环境的时间和开发时容器频繁启动所花时间,提高开发的效率. 基于内嵌apache-tomcat-8.0.24的应用开发解决了网上提供省了el-ri相关...

    tomcat源码分析图谱

    tomcat的基础脚本分析 tomcat的源码启动分析 tomcat的web应用启动分析 tomcat的socket分析 tomcat的cocket与容器对接时序分析

    java8源码-docker-tomcat8:在jre-8上运行Tomcat服务器8的简单docker镜像

    容器启动后会自动创建一个具有所有权限的admin用户,并自动生成随机密码。你可以通过查看容器log获得密码,比如 =&gt; Creating and admin user with a random password in Tomcat =&gt; Done! ==========================...

    tomcat-sr:Tomcat启动脚本,配置文件,源码剖析

    TomcatTomcat启动脚本,配置文件,源码剖析============ Tomcat 7.0.56 RUNNING.txt-安装并运行Tomcat 7.0 Servlet / JSP容器(2014.10.28-) serverStartup.txt-Tomcat 5启动顺序,serverStartup.pdf-启动过程的UML...

    Tomcat源码分析

    Tomcat的核心类图如下所示:1、Catalina:与开始/关闭shell脚本交互的主类,因此如果要研究启动和关闭的过程,就从这个类开始看起。2、Server:是整个Tomcat组件的容器,包含一个或多个Service。3、

    Idea中tomcat启动源码调试进入到tomcat内部进行调试的方法

    使用idea开发工具调试代码的时候,如果是java的web项目,使用的是tomcat作为web容器,打断点debug调试跟踪,当跟踪到org.apache.catalina包下的时候,则无法进入,这是因为idea运行的tomcat是通过插件的方式集成的,...

    java版ss源码-couchdb-lucene:使用Lucene启用CouchDB文档的全文搜索

    java版s源码couchdb-lucene 版本兼容性 CouchDB-Lucene 适用于 0.10 以上的所有版本的 CouchDB。 问题跟踪 问题跟踪在 . 最低系统要求 需要 Java 1.8; 推荐使用 Oracle Java 8 或 OpenJDK 8。 构建并运行 couchdb-...

    java开发斗地主源码-LandManager:房东的小型网站以简化会计(2013)

    java开发斗地主源码#About LandManager 是一个让房东轻松记账的小网站(2013 年)。 客户允许共享源代码(无保密协议)。 从项目中删除了有关客户和环境(数据库帐户)的所有数据。 #技术: maven、spring、spring-...

    Java ssm校园在线点餐系统源码(含数据库).zip

    3.修改源码中biyesheji\src\main\resources\jdbc.properties数据库配置文件,修改1-3行,配置为本机数据库地址 4.将项目放入tomcat容器中,并启动项目 5.浏览器访问地址 前台访问路径:...

    单点登录源码

    MyBatis | ORM框架 | [http://www.mybatis.org/mybatis-3/zh/index.html](http://www.mybatis.org/mybatis-3/zh/index.html) MyBatis Generator | 代码生成 | [http://www.mybatis.org/generator/index.html]...

    JAVA上百实例源码以及开源项目源代码

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...

    JAVA上百实例源码以及开源项目

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...

    深入浅出Hibernate源码

    它会自动进行数据库初始化、编译、启动tomcat容器.(此命令将会启动tomcat,因此之前tomcat必须处于停止状态) 然后,打开您的浏览器,输入http://localhost:8080/forum 您应该看到我们的示例正常运行. 9, 在您的...

    Java的过滤器与拦截器的区别.docx

    createWebServer()这个方法的源码我就不贴上了,大家可以自己看一下源码,最后就会看到new tomcat();并进行启动tomcat。启动容器后当然是开始进行初始化。 1 private void selfInitialize(ServletContext ...

Global site tag (gtag.js) - Google Analytics