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");

        }

  • 相关阅读:
    如何将自己的镜像上传到私库
    基于spring-cloud的微服务(1) 服务注册中心eureka
    关于对象池技术的一些记录
    为Docker容器中运行的gitlab添加ssh的一些问题记录
    使用java实现的socket代理(支持socket4和socket5)
    ConfluenceRemoteUserAuth
    JiraRemoteUserAuth
    Apache 的mod_auth_cas模块的介绍和使用
    基于乌班图的标准镜像添加中文支持
    apache反向代解决绝对路径可能出现的问题
  • 原文地址:https://www.cnblogs.com/macula7/p/1960493.html
Copyright © 2011-2022 走看看