tomcat启动过程:main(String args[])总览

来源:互联网 发布:京东数据分析笔试 编辑:程序博客网 时间:2024/04/18 17:22

BootStrap.class -----------------------------------------------------------------


启动tomcat---->运行BootStrap.class的main();方法。 
-->main();方法 
调用BootStrap.class的init();方法,初始化 
------>init();方法 
--------->setCatalinaHome();方法设置tomcat目录, 
--------->setCatalinaBase();方法设置tomcat目录, 
--------->initClassLoaders();初始化类加载器, 
--------->Thread.currentThread().setContextClassLoader(catalinaLoader);设置上文类加载器。

--------->SecurityClassLoad.securityClassLoad(catalinaLoader);对类进行预加载。

---------> Class startupClass =  catalinaLoader.loadClass ("org.apache.catalina.startup.Catalina");
              Object startupInstance = startupClass.newInstance();

              获取Catalina.class的实例。

 

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

               调用Catalina.setParentClassLoader(sharedLoader);方法设置父加载器。

------>加载参数如Server.xml等配置文件。

------>调用start()方法

------------>调用Catalina.start()方法。

                Method method = catalinaDaemon.getClass().getMethod("start", (Class [] )null);
                method.invoke(catalinaDaemon, (Object [])null);

                Catalina.start()方法会去启动Server

完成启动。


附BootStrap.class源码------------------------------------。
/* 
* Copyright 1999,2004 The Apache Software Foundation. 

* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 

*      http://www.apache.org/licenses/LICENSE-2.0 

* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 


package org.apache.catalina.startup; 


import java.io.File; 
import java.lang.reflect.Method; 
import java.lang.reflect.ParameterizedType; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.StringTokenizer; 

import javax.management.MBeanServer; 
import javax.management.MBeanServerFactory; 
import javax.management.ObjectName; 

import org.apache.catalina.security.SecurityClassLoad; 
import org.apache.juli.logging.Log; 
import org.apache.juli.logging.LogFactory; 

import com.zwb.test.uitl.TestUtils; 


/** 
* Boostrap loader for Catalina.  This application constructs a class loader 
* for use in loading the Catalina internal classes (by accumulating all of the 
* JAR files found in the "server" directory under "catalina.home"), and 
* starts the regular execution of the container.  The purpose of this 
* roundabout approach is to keep the Catalina internal classes (and any 
* other classes they depend on, such as an XML parser) out of the system 
* class path and therefore not visible to application level classes. 

* @author Craig R. McClanahan 
* @author Remy Maucherat 
* @version $Revision: 304108 $ $Date: 2005-09-29 07:55:15 +0200 (jeu., 29 sept. 2005) $ 
*/ 

/ * 
* @author Craig R. McClanahan 
* @author Remy Maucherat 
* @version $Revision: 304108 $ $Date: 2005-09-29 07:55:15 +0200 (jeu., 29 sept. 2005) $ 
*/ 
public final class Bootstrap { 

    private static Log log = LogFactory.getLog(Bootstrap.class); 
    
    // -------------------------------------------------------------- Constants 


    protected static final String CATALINA_HOME_TOKEN = "${catalina.home}"; 
    protected static final String CATALINA_BASE_TOKEN = "${catalina.base}"; 


    // ------------------------------------------------------- Static Variables 


    /** 
     * Daemon object used by main. 
     */ 
    private static Bootstrap daemon = null; 


    // -------------------------------------------------------------- Variables 


    /** 
     * Daemon reference. 
     */ 
    private Object catalinaDaemon = null; 


    protected ClassLoader commonLoader = null; 
    protected ClassLoader catalinaLoader = null; 
    protected ClassLoader sharedLoader = null; 

    
    public Bootstrap(){ 
    System.out.println("BootStap.class Bootstrap() constructor method"); 
    } 

    // -------------------------------------------------------- Private Methods 


    private void initClassLoaders() { 
    System.out.println("BootStap.class initClassLoaders() method"); 

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


    private ClassLoader createClassLoader(String name, ClassLoader parent) 
        throws Exception { 
    System.out.println("BootStap.class createClassLoader(String name, ClassLoader parent) method"); 
        String value = CatalinaProperties.getProperty(name + ".loader"); 
        if ((value == null) || (value.equals(""))) 
            return parent; 

        ArrayList repositoryLocations = new ArrayList(); 
        ArrayList repositoryTypes = new ArrayList(); 
        int i; 

        StringTokenizer tokenizer = new StringTokenizer(value, ","); 
        while (tokenizer.hasMoreElements()) { 
            String repository = tokenizer.nextToken(); 
            // Local repository 
            boolean replace = false; 
            String before = repository; 
            while ((i=repository.indexOf(CATALINA_HOME_TOKEN))>=0) { 
            
                replace=true; 
                if (i>0) { 
                repository = repository.substring(0,i) + getCatalinaHome() 
                    + repository.substring(i+CATALINA_HOME_TOKEN.length()); 
                } else { 
                    repository = getCatalinaHome() 
                        + repository.substring(CATALINA_HOME_TOKEN.length()); 
                } 
            } 
            while ((i=repository.indexOf(CATALINA_BASE_TOKEN))>=0) { 
                replace=true; 
                if (i>0) { 
                repository = repository.substring(0,i) + getCatalinaBase() 
                    + repository.substring(i+CATALINA_BASE_TOKEN.length()); 
                } else { 
                    repository = getCatalinaBase() 
                        + repository.substring(CATALINA_BASE_TOKEN.length()); 
                } 
            } 
            if (replace && log.isDebugEnabled()) 
                log.debug("Expanded " + before + " to " + replace); 

            // Check for a JAR URL repository 
            try { 
            
                URL url=new URL(repository); 
                repositoryLocations.add(repository); 
                repositoryTypes.add(ClassLoaderFactory.IS_URL); 
                continue; 
            } catch (MalformedURLException e) { 
                e.printStackTrace(); 
            } 

            if (repository.endsWith("*.jar")) { 
                repository = repository.substring 
                    (0, repository.length() - "*.jar".length()); 
               
                repositoryLocations.add(repository); 
                repositoryTypes.add(ClassLoaderFactory.IS_GLOB); 
            } else if (repository.endsWith(".jar")) { 
            
                repositoryLocations.add(repository); 
                repositoryTypes.add(ClassLoaderFactory.IS_JAR); 
            } else { 
            
                repositoryLocations.add(repository); 
                repositoryTypes.add(ClassLoaderFactory.IS_DIR); 
            } 
        } 
        
       
        
        String[] locations = (String[]) repositoryLocations.toArray(new String[0]); 
        Integer[] types = (Integer[]) repositoryTypes.toArray(new Integer[0]); 

        ClassLoader classLoader = ClassLoaderFactory.createClassLoader 
            (locations, types, parent); 

        // Retrieving MBean server 
        MBeanServer mBeanServer = null; 
        if (MBeanServerFactory.findMBeanServer(null).size() > 0) { 
            mBeanServer = 
                (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0); 
        } else { 
            mBeanServer = MBeanServerFactory.createMBeanServer(); 
        } 

        // Register the server classloader 
       
        ObjectName objectName = 
            new ObjectName("Catalina:type=ServerClassLoader,name=" + name); 
        mBeanServer.registerMBean(classLoader, objectName); 

        return classLoader; 

    } 


    /** 
     * Initialize daemon. 
     */ 
    public void init() 
        throws Exception 
    { 

    System.out.println("BootStap.class init() method"); 
        // Set Catalina path 
        setCatalinaHome(); 
        setCatalinaBase(); 

        initClassLoaders(); 

        Thread.currentThread().setContextClassLoader(catalinaLoader); 

        SecurityClassLoad.securityClassLoad(catalinaLoader); 

        // Load our startup class and call its process() method 
        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; 

    } 


    /** 
     * Load daemon. 
     */ 
    private void load(String[] arguments) 
        throws Exception { 

    System.out.println("BootStap.class  load(String[] arguments) method"); 
        // 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); 

    } 


    // ----------------------------------------------------------- Main Program 


    /** 
     * Load the Catalina daemon. 
     */ 
    public void init(String[] arguments) 
        throws Exception { 
    System.out.println("BootStap.class   init(String[] arguments) method"); 
        init(); 
        load(arguments); 

    } 


    /** 
     * Start the Catalina daemon. 
     */ 
    public void start() 
        throws Exception { 
    System.out.println("BootStap.class  start() method"); 
        if( catalinaDaemon==null ) init(); 

        Method method = catalinaDaemon.getClass().getMethod("start", (Class [] )null); 
        method.invoke(catalinaDaemon, (Object [])null); 

    } 


    /** 
     * Stop the Catalina Daemon. 
     */ 
    public void stop() 
        throws Exception { 

        Method method = catalinaDaemon.getClass().getMethod("stop", (Class [] ) null); 
        method.invoke(catalinaDaemon, (Object [] ) null); 

    } 


    /** 
     * Stop the stand lone server. 
     */ 
    public void stopServer() 
        throws Exception { 
    System.out.println("BootStap.class  stopServer() method"); 
        Method method = 
            catalinaDaemon.getClass().getMethod("stopServer", (Class []) null); 
        method.invoke(catalinaDaemon, (Object []) null); 

    } 


   /** 
     * Stop the standlone server. 
     */ 
    public void stopServer(String[] arguments) 
        throws Exception { 
    System.out.println("BootStap.class  stopServer(String[] arguments) method"); 
        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("stopServer", paramTypes); 
        method.invoke(catalinaDaemon, param); 

    } 


    /** 
     * Set flag. 
     */ 
    public void setAwait(boolean await) 
        throws Exception { 
    System.out.println("BootStap.class setAwait(boolean await) method"); 
        Class paramTypes[] = new Class[1]; 
        paramTypes[0] = Boolean.TYPE; 
        Object paramValues[] = new Object[1]; 
        paramValues[0] = new Boolean(await); 
        Method method = 
            catalinaDaemon.getClass().getMethod("setAwait", paramTypes); 
        method.invoke(catalinaDaemon, paramValues); 

    } 

    public boolean getAwait() 
        throws Exception 
    { 
    System.out.println("BootStap.class getAwait() method"); 
        Class paramTypes[] = new Class[0]; 
        Object paramValues[] = new Object[0]; 
        Method method = 
            catalinaDaemon.getClass().getMethod("getAwait", paramTypes); 
        Boolean b=(Boolean)method.invoke(catalinaDaemon, paramValues); 
        return b.booleanValue(); 
    } 


    /** 
     * Destroy the Catalina Daemon. 
     */ 
    public void destroy() { 
    System.out.println("BootStap.class destroy()  method"); 
    
        // FIXME 

    } 


    /** 
     * Main method, used for testing only. 
     * 
     * @param args Command line arguments to be processed 
     */ 
    public static void main(String args[]) { 
  
    System.out.println("BootStap.class main(String args[])  method"); 
        if (daemon == null) { 
            daemon = new Bootstrap(); 
            try { 
                daemon.init(); 
            } catch (Throwable t) { 
                t.printStackTrace(); 
                return; 
            } 
        } 

        try { 
            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."); 
            } 
        } catch (Throwable t) { 
            t.printStackTrace(); 
        } 

    } 

    public void setCatalinaHome(String s) { 
    
    System.out.println("BootStap.class setCatalinaHome(String s)  method"); 
        System.setProperty( "catalina.home", s ); 
    } 

    public void setCatalinaBase(String s) { 
    System.out.println("BootStap.class setCatalinaBase(String s)  method"); 
        System.setProperty( "catalina.base", s ); 
    } 


    /** 
     * Set the <code>catalina.base</code> System property to the current 
     * working directory if it has not been set. 
     */ 
    private void setCatalinaBase() { 
    System.out.println("BootStap.class  setCatalinaBase() method"); 
        if (System.getProperty("catalina.base") != null) 
            return; 
        if (System.getProperty("catalina.home") != null) 
            System.setProperty("catalina.base", 
                               System.getProperty("catalina.home")); 
        else 
            System.setProperty("catalina.base", 
                               System.getProperty("user.dir")); 

    } 


    /** 
     * Set the <code>catalina.home</code> System property to the current 
     * working directory if it has not been set. 
     */ 
    private void setCatalinaHome() { 
    System.out.println("BootStap.class setCatalinaHome() method"); 
        if (System.getProperty("catalina.home") != null){ 
        System.out.println("123"); 
            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")); 
        } 

    } 


    /** 
     * Get the value of the catalina.home environment variable. 
     */ 
    public static String getCatalinaHome() { 
    System.out.println("BootStap.class getCatalinaHome() method"); 
        return System.getProperty("catalina.home", 
                                  System.getProperty("user.dir")); 
    } 


    /** 
     * Get the value of the catalina.base environment variable. 
     */ 
    public static String getCatalinaBase() { 
    System.out.println("BootStap.class getCatalinaBase() method"); 
        return System.getProperty("catalina.base", getCatalinaHome()); 
    } 


}

0 0
原创粉丝点击