zoukankan      html  css  js  c++  java
  • tomcat源码阅读_代码篇1

    首先读Bootstrap类,该类在how Tomcat works里有比较详细的介绍,结合书和代码看应该没有问题。接下来看Catalina类,主要主要该类中的start()方法:

        /**
         * Start a new server instance.
         */
        public void start() {

            if (server == null) {
                load();
            }

            long t1 = System.nanoTime();
           
            // Start the new server
            if (server instanceof Lifecycle) {
                try {
                    ((Lifecycle) server).start();
                } catch (LifecycleException e) {
                    log.error("Catalina.start: ", e);
                }
            }

            long t2 = System.nanoTime();
            if(log.isInfoEnabled())
                log.info("Server startup in " + ((t2 - t1) / 1000000) + " ms");

            try {
                // Register shutdown hook
                if (useShutdownHook) {
                    if (shutdownHook == null) {
                        shutdownHook = new CatalinaShutdownHook();
                    }
                    Runtime.getRuntime().addShutdownHook(shutdownHook);
                }
            } catch (Throwable t) {
                // This will fail on JDK 1.2. Ignoring, as Tomcat can run
                // fine without the shutdown hook.
            }

            if (await) {
                await();
                stop();
            }

        }

    下面是使用的时候的load方法
        /**
         * Start a new server instance.
         */
        public void load() {

            long t1 = System.nanoTime();

            initDirs();

            // Before digester - it may be needed

            initNaming();

            // Create and execute our Digester初始化组件根据XML文件在这里完成
            Digester digester = createStartDigester();

            InputSource inputSource = null;
            InputStream inputStream = null;
            File file = null;
            try {
                file = configFile();
                inputStream = new FileInputStream(file);
                inputSource = new InputSource("file://" + file.getAbsolutePath());
            } catch (Exception e) {
                ;
            }
            if (inputStream == null) {
                try {
                    inputStream = getClass().getClassLoader()
                        .getResourceAsStream(getConfigFile());
                    inputSource = new InputSource
                        (getClass().getClassLoader()
                         .getResource(getConfigFile()).toString());
                } catch (Exception e) {
                    ;
                }
            }

            // This should be included in catalina.jar
            // Alternative: don't bother with xml, just create it manually.
            if( inputStream==null ) {
                try {
                    inputStream = getClass().getClassLoader()
                    .getResourceAsStream("server-embed.xml");
                    inputSource = new InputSource
                    (getClass().getClassLoader()
                            .getResource("server-embed.xml").toString());
                } catch (Exception e) {
                    ;
                }
            }
           

            if ((inputStream == null) && (file != null)) {
                log.warn("Can't load server.xml from " + file.getAbsolutePath());
                return;
            }

            try {
                inputSource.setByteStream(inputStream);
                digester.push(this);
                digester.parse(inputSource);
                inputStream.close();
            } catch (Exception e) {
                log.warn("Catalina.start using "
                                   + getConfigFile() + ": " , e);
                return;
            }

            // Stream redirection
            initStreams();

            // Start the new server
            if (server instanceof Lifecycle) {
                try {
                    server.initialize();
                } catch (LifecycleException e) {
                    log.error("Catalina.start", e);
                }
            }

            long t2 = System.nanoTime();
            if(log.isInfoEnabled())
                log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");

        }

  • 相关阅读:
    [程序员修炼之道]读书笔记
    protobufnet 简介
    提高IIS响应能力
    彻底解决性能问题,就因为一个配置
    2009年12月小记(Split,netstat,SortedList)
    如何在vmwarer中的linux安装lrzsz文件共享工具
    Liunx常用命令
    正则表达式 浅析
    java的final关键字
    JAVA正则表达式 Pattern和Matcher
  • 原文地址:https://www.cnblogs.com/macula7/p/1960493.html
Copyright © 2011-2022 走看看