Tomcat source code analysis-server. xml file loading, tomcatserver. xml

Source: Internet
Author: User

Tomcat source code analysis-server. xml file loading, tomcatserver. xml
Preface

As a Java programmer, you must be familiar with tomcat server. xml. Based on the Java source code of Tomcat7.0, this article analyzes how the server. xml file is loaded.

Source code analysis

The load method of Bootstrap is the entry to load tomcat's server. xml. The load method actually calls the load method of Catalina through reflection. See Code List 1.

Code List 1

/** * Load daemon. */private void load(String[] arguments)    throws Exception {    // Call the load() method    String methodName = "load";    Object param[];    Class<?> paramTypes[];    if (arguments==null || arguments.length==0) {        paramTypes = null;        param = null;    } else {        paramTypes = new Class[1];        paramTypes[0] = arguments.getClass();        param = new Object[1];        param[0] = arguments;    }    Method method =         catalinaDaemon.getClass().getMethod(methodName, paramTypes);    if (log.isDebugEnabled())        log.debug("Calling startup class " + method);    method.invoke(catalinaDaemon, param);}

For the implementation of the load method of Catalina, see Code List 2.

Code List 2

/** * 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    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) {        // Ignore    }    if (inputStream == null) {        try {            inputStream = getClass().getClassLoader()                .getResourceAsStream(getConfigFile());            inputSource = new InputSource                (getClass().getClassLoader()                 .getResource(getConfigFile()).toString());        } catch (Exception e) {            // Ignore        }    }    // 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) {            // Ignore        }    }    if ((inputStream == null) && (file != null)) {        log.warn("Can't load server.xml from " + 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);        inputStream.close();    } catch (Exception e) {        log.warn("Catalina.start using "                           + getConfigFile() + ": " , e);        return;    }    // Stream redirection    initStreams();    // Start the new server    try {        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");}

Here, we analyze the generation, Qing, and Qing Dynasties single 2. The steps are as follows:
1) The initDirs method is used to check catalina. home and catalina. base.
2) The initNaming method sets java. naming. factory. url. pkgs and java. naming. factory. initial for the system. When creating a JNDI Context, use Context. INITIAL_CONTEXT_FACTORY ("java. naming. factory. initial ") attribute to specify the factory class for creating the JNDI Context; Context. URL_PKG_PREFIXES ("java. naming. factory. url. pkgs ") used to create the corresponding JNDI context when the query url contains the scheme method id, for example, query similar queries such as" java:/jdbc/test1 ", that is, the colon ": "marked shceme. The Context. URL_PKG_PREFIXES attribute value has multiple java package paths, separated by a colon (:). These package paths include the JNDI implementation classes. When the url "java:" contains the scheme ID in the JNDI Context, the InitialContext class will give priority to Context. whether scheme + "exists in the package path specified by the URL_PKG_PREFIXES attribute ". "+ scheme +" URLContextFactory "factory class (ObjectFactory interface must be implemented). If this factory class exists, call the getObjectInstance method of this factory class to obtain the jndi context corresponding to the scheme solution ID, search for the corresponding url in this context.
3) The createStartDigester method creates and configures the Digester instance to be started, and sets a series of Rule, which is mapped to server. xml.
4) use FileInputStream to obtain the input stream of the conf/server. xml configuration file.
5) encapsulate FileInputStream as InputSource and call the parse method of Digester for parsing.
6) initStreams redirects the output stream and error stream.
7) initialize the server. The specific implementation is not analyzed in this article.

Summary

From the above analysis, we can see that the tomcat Loading Method of server. xml configuration files is very traditional, and FileInputStream is used for loading. Resolution of the server. xml configuration file will be added later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.