FileDocCategorySizeDatePackage
Bootstrap.javaAPI DocGlassfish v2 API14195Fri May 04 22:32:28 BST 2007org.apache.catalina.startup

Bootstrap

public final class Bootstrap extends Object
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: 1.6 $ $Date: 2007/05/05 05:32:28 $

Fields Summary
protected static final String
CATALINA_HOME_TOKEN
protected static final String
CATALINA_BASE_TOKEN
private static Bootstrap
daemon
Daemon object used by main.
private static com.sun.org.apache.commons.logging.Log
log
protected int
debug
Debugging detail level for processing the startup.
private Object
catalinaDaemon
Daemon reference.
protected ClassLoader
commonLoader
protected ClassLoader
catalinaLoader
protected ClassLoader
sharedLoader
Constructors Summary
Methods Summary
private java.lang.ClassLoadercreateClassLoader(java.lang.String name, java.lang.ClassLoader parent)


        String value = CatalinaProperties.getProperty(name + ".loader");
        if ((value == null) || (value.equals("")))
            return parent;

        ArrayList unpackedList = new ArrayList();
        ArrayList packedList = new ArrayList();
        ArrayList urlList = new ArrayList();

        StringTokenizer tokenizer = new StringTokenizer(value, ",");
        while (tokenizer.hasMoreElements()) {
            String repository = tokenizer.nextToken();
            // Check for a JAR URL repository
            try {
                urlList.add(new URL(repository));
                continue;
            } catch (MalformedURLException e) {
                // Ignore
            }
            // Local repository
            boolean packed = false;
            if (repository.startsWith(CATALINA_HOME_TOKEN)) {
                repository = getCatalinaHome() 
                    + repository.substring(CATALINA_HOME_TOKEN.length());
            } else if (repository.startsWith(CATALINA_BASE_TOKEN)) {
                repository = getCatalinaBase() 
                    + repository.substring(CATALINA_BASE_TOKEN.length());
            }
            if (repository.endsWith("*.jar")) {
                packed = true;
                repository = repository.substring
                    (0, repository.length() - "*.jar".length());
            }
            if (packed) {
                packedList.add(new File(repository));
            } else {
                unpackedList.add(new File(repository));
            }
        }

        File[] unpacked = (File[]) unpackedList.toArray(new File[0]);
        File[] packed = (File[]) packedList.toArray(new File[0]);
        URL[] urls = (URL[]) urlList.toArray(new URL[0]);

        return ClassLoaderFactory.createClassLoader
            (unpacked, packed, urls, parent);

    
public voiddestroy()
Destroy the Catalina Daemon.


        // FIXME

    
public booleangetAwait()

        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();
    
public static java.lang.StringgetCatalinaBase()
Get the value of the catalina.base environment variable.

        return System.getProperty("catalina.base", getCatalinaHome());
    
public static java.lang.StringgetCatalinaHome()
Get the value of the catalina.home environment variable.

        return System.getProperty("catalina.home",
                                  System.getProperty("user.dir"));
    
public voidinit()
Initialize daemon.


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

    
public voidinit(java.lang.String[] arguments)
Load the Catalina daemon.


        // Read the arguments
        if (arguments != null) {
            for (int i = 0; i < arguments.length; i++) {
                if (arguments[i].equals("-debug")) {
                    debug = 1;
                }
            }
        }

        init();
        load(arguments);

    
private voidinitClassLoaders()



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


       
        try {
            ClassLoaderFactory.setDebug(debug);
            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 voidload(java.lang.String[] arguments)
Load daemon.


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

    
public static voidmain(java.lang.String[] args)
Main method, used for testing only.

param
args Command line arguments to be processed


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

    
public voidsetAwait(boolean await)
Set flag.


        Class paramTypes[] = new Class[1];
        paramTypes[0] = Boolean.TYPE;
        Object paramValues[] = new Object[1];
        paramValues[0] = Boolean.valueOf(await);
        Method method = 
            catalinaDaemon.getClass().getMethod("setAwait", paramTypes);
        method.invoke(catalinaDaemon, paramValues);

    
public voidsetCatalinaBase(java.lang.String s)

        System.setProperty( "catalina.base", s );
    
private voidsetCatalinaBase()
Set the catalina.base System property to the current working directory if it has not been set.


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

    
public voidsetCatalinaHome(java.lang.String s)

        System.setProperty( "catalina.home", s );
    
private voidsetCatalinaHome()
Set the catalina.home System property to the current working directory if it has not been set.


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

    
public voidstart()
Start the Catalina daemon.

        if( catalinaDaemon==null ) init();

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

    
public voidstop()
Stop the Catalina Daemon.


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

    
public voidstopServer()
Stop the standlone server.


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