Tomcat源码阅读(三)Catalina

来源:互联网 发布:淘宝客推广群的广告词 编辑:程序博客网 时间:2024/06/07 18:20

上一节说到,Bootstrap执行的加载操作,实际都需要反射执行Catalina类方法。现在介绍一下Catalina的主要方法load和start究竟做了些什么。

1、load

public void load() {        long t1 = System.nanoTime();        //初始化临时目录        initDirs();        //设置额外的系统属性        initNaming();        //创建和执行Digester        Digester digester = createStartDigester();        InputSource inputSource = null;        InputStream inputStream = null;        File file = null;        try {            /*            读取配置文件conf/server.xml            */            file = configFile();            inputStream = new FileInputStream(file);            inputSource = new InputSource(file.toURI().toURL().toString());        } catch (Exception e) {            if (log.isDebugEnabled()) {                log.debug(sm.getString("catalina.configFail", file), e);            }        }        if (inputStream == null) {            try {                inputStream = getClass().getClassLoader()                    .getResourceAsStream(getConfigFile());                inputSource = new InputSource                    (getClass().getClassLoader()                     .getResource(getConfigFile()).toString());            } catch (Exception e) {                if (log.isDebugEnabled()) {                    log.debug(sm.getString("catalina.configFail",                            getConfigFile()), 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 (log.isDebugEnabled()) {                    log.debug(sm.getString("catalina.configFail",                            "server-embed.xml"), e);                }            }        }        if (inputStream == null || inputSource == null) {            if  (file == null) {                log.warn(sm.getString("catalina.configFail",                        getConfigFile() + "] or [server-embed.xml]"));            } else {                log.warn(sm.getString("catalina.configFail",                        file.getAbsolutePath()));                if (file.exists() && !file.canRead()) {                    log.warn("Permissions incorrect, read permission is not allowed on the file.");                }            }            return;        }        try {            inputSource.setByteStream(inputStream);            digester.push(this);            digester.parse(inputSource);        } catch (SAXParseException spe) {            log.warn("Catalina.start using " + getConfigFile() + ": " +                    spe.getMessage());            return;        } catch (Exception e) {            log.warn("Catalina.start using " + getConfigFile() + ": " , e);            return;        } finally {            try {                inputStream.close();            } catch (IOException e) {                // Ignore            }        }        getServer().setCatalina(this);        getServer().setCatalinaHome(Bootstrap.getCatalinaHomeFile());        getServer().setCatalinaBase(Bootstrap.getCatalinaBaseFile());        //管道重定向        initStreams();        // 启动新的server        try {            //执行init方法            getServer().init();        } catch (LifecycleException e) {            if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {                throw new java.lang.Error(e);            } else {                log.error("Catalina.start", e);            }        }        long t2 = System.nanoTime();        if(log.isInfoEnabled()) {            log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");        }    }

这里调用了initNaming,主要是做一些系统属性的赋值

protected void initNaming() {        // Setting additional variables        if (!useNaming) {            log.info( "Catalina naming disabled");            System.setProperty("catalina.useNaming", "false");        } else {            System.setProperty("catalina.useNaming", "true");            String value = "org.apache.naming";            String oldValue =                System.getProperty(javax.naming.Context.URL_PKG_PREFIXES);            if (oldValue != null) {                value = value + ":" + oldValue;            }            System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value);            if( log.isDebugEnabled() ) {                log.debug("Setting naming prefix=" + value);            }            value = System.getProperty                (javax.naming.Context.INITIAL_CONTEXT_FACTORY);            if (value == null) {                System.setProperty                    (javax.naming.Context.INITIAL_CONTEXT_FACTORY,                     "org.apache.naming.java.javaURLContextFactory");            } else {                log.debug( "INITIAL_CONTEXT_FACTORY already set " + value );            }        }    }

初始化管道initStreams

protected void initStreams() {        // Replace System.out and System.err with a custom PrintStream        System.setOut(new SystemLogHandler(System.out));        System.setErr(new SystemLogHandler(System.err));    }

把自定义的PrintStream替换System.out和System.err

2、然后,再看看start方法

public void start() {        if (getServer() == null) {            load();        }        if (getServer() == null) {            log.fatal("Cannot start server. Server instance is not configured.");            return;        }        long t1 = System.nanoTime();        // 启动server        try {            getServer().start();        } catch (LifecycleException e) {            log.fatal(sm.getString("catalina.serverStartFail"), e);            try {                getServer().destroy();            } catch (LifecycleException e1) {                log.debug("destroy() failed for failed Server ", e1);            }            return;        }        long t2 = System.nanoTime();        if(log.isInfoEnabled()) {            log.info("Server startup in " + ((t2 - t1) / 1000000) + " ms");        }        // 注册关闭钩子        if (useShutdownHook) {            if (shutdownHook == null) {                shutdownHook = new CatalinaShutdownHook();            }            Runtime.getRuntime().addShutdownHook(shutdownHook);            // If JULI is being used, disable JULI's shutdown hook since            // shutdown hooks run in parallel and log messages may be lost            // if JULI's hook completes before the CatalinaShutdownHook()            LogManager logManager = LogManager.getLogManager();            if (logManager instanceof ClassLoaderLogManager) {                ((ClassLoaderLogManager) logManager).setUseShutdownHook(                        false);            }        }        if (await) {            await();            stop();        }    }

这里调用了server的start方法。

0 0