Tomcat啓動(一)

 以下是Catalina.load()的UML序列

 

 

1.設置catalina.home系統屬性

 private void setCatalinaHome() {

        if (System.getProperty("catalina.home") != null)//如果已經設置,直接返回
            return;
        File bootstrapJar = 
            new File(System.getProperty("user.dir"), "bootstrap.jar");
        if (bootstrapJar.exists()) {
            try {
                System.setProperty
                    ("catalina.home", 
                     (new File(System.getProperty("user.dir"), ".."))
                     .getCanonicalPath());
            } catch (Exception e) {
                // Ignore
                System.setProperty("catalina.home",
                                   System.getProperty("user.dir"));
            }
        } else {
            System.setProperty("catalina.home",
                               System.getProperty("user.dir"));//設置爲系統所在的路徑
        }

    }
2.設置catalina.base系統屬性
 private void setCatalinaBase() {

        if (System.getProperty("catalina.base") != null)//如果已經設置直接返回
            return;
        if (System.getProperty("catalina.home") != null)//如果設置了catalina.home系統變量,那麼把值賦值給catalina.base
            System.setProperty("catalina.base",
                               System.getProperty("catalina.home"));
        else
            System.setProperty("catalina.base",
                               System.getProperty("user.dir"));

    }

3.初始化類加載器

 private void initClassLoaders() {
        try {
            commonLoader = createClassLoader("common", null);
            if( commonLoader == null ) {
                // no config file, default to this loader - we might be in a 'single' env.
                commonLoader=this.getClass().getClassLoader();
            }
            catalinaLoader = createClassLoader("server", commonLoader);
            sharedLoader = createClassLoader("shared", commonLoader);
        } catch (Throwable t) {
            log.error("Class loader creation threw exception", t);
            System.exit(1);
        }
    }

和其他的服務器一樣,Tomcat 6使用不同的類加載器來加載容器的不同部分,使在容器運行的應用程序可以訪問不同的classes和資源文件。在J2SE 2環境中,類加載器使用的父子結構的,當有一個請求類加載器去加載一個類時,這個類加載器委託它的父加載器去加載,只有父加載器找不到所要的類和資源文件時,子類加載器纔去。但是在訪問應用程序的時候,沒有使用委託,現在classes文件夾下搜索。

Bootstrap
      |
System
      |
Common
 /                   \
Webapp1 Webapp2 ...
  • Bootstrap - This class loader contains the basic runtime classes provided by the Java Virtual Machine, plus any classes from JAR files present in the System Extensions directory ($JAVA_HOME/jre/lib/ext). NOTE - Some JVMs may implement this as more than one class loader, or it may not be visible (as a class loader) at all.
  • System - This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, the standard Tomcat 6 startup scripts ($CATALINA_HOME/bin/catalina.sh or %CATALINA_HOME%\bin\catalina.bat) totally ignore the contents of the CLASSPATH environment variable itself, and instead build the System class loader from the following repositories:
    • $CATALINA_HOME/bin/bootstrap.jar - Contains the main() method that is used to initialize the Tomcat 6 server, and the class loader implementation classes it depends on.
    • $CATALINA_HOME/bin/tomcat-juli.jar - Package renamed Commons logging API, and java.util.logging LogManager.
  • Common - This class loader contains additional classes that are made visible to both Tomcat internal classes and to all web applications. Normally, application classes should NOT be placed here. All unpacked classes and resources in $CATALINA_HOME/lib, as well as classes and resources in JAR files are made visible through this class loader. By default, that includes the following:
    • annotations-api.jar - JEE annotations classes.
    • catalina.jar - Implementation of the Catalina servlet container portion of Tomcat 6.
    • catalina-ant.jar - Tomcat Catalina Ant tasks.
    • catalina-ha.jar - High availability package.
    • catalina-tribes.jar - Group communication package.
    • el-api.jar - EL 2.1 API.
    • jasper.jar - Jasper 2 Compiler and Runtime.
    • jasper-el.jar - Jasper 2 EL implementation.
    • ecj-*.jar - Eclipse JDT Java compiler.
    • jsp-api.jar - JSP 2.1 API.
    • servlet-api.jar - Servlet 2.5 API.
    • tomcat-coyote.jar - Tomcat connectors and utility classes.
    • tomcat-dbcp.jar - package renamed database connection pool based on Commons DBCP.
    • tomcat-i18n-**.jar - Optional JARs containing resource bundles for other languages. As default bundles are also included in each individual JAR, they can be safely removed if no internationalization of messages is needed.
  • WebappX - A class loader is created for each web application that is deployed in a single Tomcat 6 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.

http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html 在這裏引用

所以從一個web應用的角度看,class和資源的加載是按以下的順序進行的。
  • Bootstrap classes of your JVM
  • System class loader classes (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • $CATALINA_HOME/lib
  • $CATALINA_HOME/lib/*.jar

4.安全管理類加載

public static void securityClassLoad(ClassLoader loader)
        throws Exception {

        if( System.getSecurityManager() == null ){ //如果系統沒有設置訪問安全器直接返回
            return;
        }
        
        loadCorePackage(loader);
        loadLoaderPackage(loader);
        loadSessionPackage(loader);
        loadUtilPackage(loader);
        loadJavaxPackage(loader);
        loadCoyotePackage(loader);        
        loadHttp11Package(loader);        
    }

5.調用org.apache.catalina.startup.Catalina.setParentClassLoader方法

 /**
         * 這是使用java反射機制實例化org.apache.catalina.startup.Catalina類,並設置sharedLoader爲父加載器
         * 後面很多地方都是使用反射機制調用Catalina的方法,如start,stop
         */
        if (log.isDebugEnabled())
        	
            log.debug("Loading startup class");
        Class startupClass =
            catalinaLoader.loadClass
            ("org.apache.catalina.startup.Catalina");
        Object startupInstance = startupClass.newInstance();

        // Set the shared extensions class loader
        if (log.isDebugEnabled())
            log.debug("Setting startup class properties");
        String methodName = "setParentClassLoader";
        Class paramTypes[] = new Class[1];
        paramTypes[0] = Class.forName("java.lang.ClassLoader");
        Object paramValues[] = new Object[1];
        paramValues[0] = sharedLoader;
        Method method =
            startupInstance.getClass().getMethod(methodName, paramTypes);
        method.invoke(startupInstance, paramValues);

        catalinaDaemon = startupInstance;


以上是Tomcat的init,其中涉及到設置系統變量,類加載器的創建,設置父子類加載,安全管理類的加載。

String command = "start"; //判斷命令行參數,或啓動或停止服務器
            if (args.length > 0) {
                command = args[args.length - 1];
            }

            if (command.equals("startd")) {
                args[0] = "start";
                daemon.load(args);
                daemon.start();
            } else if (command.equals("stopd")) {
                args[0] = "stop";
                daemon.stop();
            } else if (command.equals("start")) {
                daemon.setAwait(true);
                daemon.load(args);
                daemon.start();
            } else if (command.equals("stop")) {
                daemon.stopServer(args);
            } else {
                log.warn("Bootstrap: command \"" + command + "\" does not exist.");
            }

這裏我們先看看啓動部分,首先,調用Catalina的load方法

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

進Catalina.load方法看看

long t1 = System.nanoTime();

        initDirs();	//初始化系統目錄和臨時目錄

        // Before digester - it may be needed

        initNaming();//判斷系統是否使用JNDI,如果是,設置相關的變量

        // Create and execute our Digester
        Digester digester = createStartDigester();//創建XML解析,server.xml

        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);//解析,實例化Server類等
            inputStream.close();
        } catch (Exception e) {
            log.warn("Catalina.start using "
                               + getConfigFile() + ": " , e);
            return;
        }

        // Stream redirection
        initStreams();//重新改寫系統日誌輸出


上面使用到Digester框架,會在另外的文章介紹。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章