ServerStarter.java 3.34 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/*
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10 11 12 13
 */

package org.jivesoftware.messenger.starter;

14 15
import org.jivesoftware.util.Log;

Matt Tucker's avatar
Matt Tucker committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import java.io.File;

/**
 * Starts the core XMPP server. A ootstrap class that configures classloaders
 * to ensure easy, dynamic server startup.
 *
 * This class should be for standalone mode only. Jive Messenger servers launched
 * through a J2EE container (servlet/EJB) will use those environment's
 * classloading facilities to ensure proper startup.<p>
 *
 * Tasks:<ul>
 *      <li>Add all jars in the lib directory to the classpath.</li>
 *      <li>Add the config directory to the classpath for loadResource()</li>
 *      <li>Start the server</li>
 * </ul>
 *
32 33 34 35
 * Note: if the enviroment property messenger.lib.directory is specified ServerStarter will attempt
 * to use this value as the value for messenger's lib directory. If the property is not specified
 * the default value of ../lib will be used.
 *
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39
 * @author Iain Shigeoka
 */
public class ServerStarter {

40 41 42 43 44
    /**
     * Default to this location if one has not been specified
     */
    private static final String DEFAULT_LIB_DIR = "../lib";

Matt Tucker's avatar
Matt Tucker committed
45
    public static void main(String [] args) {
46
        new ServerStarter().start();
Matt Tucker's avatar
Matt Tucker committed
47 48 49 50 51 52 53 54 55 56 57 58
    }

    /**
     * Starts the server by loading and instantiating the bootstrap
     * container. Once the start method is called, the server is
     * started and the server starter should not be used again.
     */
    private void start() {
        // setup the classpath using JiveClassLoader
        try {
            // Load up the bootstrap container
            final ClassLoader parent = findParentClassLoader();
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

            String libDirString = System.getProperty("messenger.lib.dir");

            File libDir;
            if(libDirString != null) {
                // if the lib directory property has been specified and it actually
                // exists use it, else use the default

                libDir = new File(libDirString);
                if(!libDir.exists()) {
                    Log.warn("lib directory "+libDirString+
                            " does not exist defaulting to "+DEFAULT_LIB_DIR);
                    libDir = new File(DEFAULT_LIB_DIR);
                }

            } else {
                libDir = new File(DEFAULT_LIB_DIR);
            }

Matt Tucker's avatar
Matt Tucker committed
78 79 80 81
            ClassLoader loader = new JiveClassLoader(parent, libDir);
           
            Thread.currentThread().setContextClassLoader(loader);
            Class containerClass = loader.loadClass(
82
                    "org.jivesoftware.messenger.XMPPServer");
Matt Tucker's avatar
Matt Tucker committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            containerClass.newInstance();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Locates the best class loader based on context (see class description).
     *
     * @return The best parent classloader to use
     */
    private ClassLoader findParentClassLoader() {
        ClassLoader parent = Thread.currentThread().getContextClassLoader();
        if (parent == null) {
            parent = this.getClass().getClassLoader();
            if (parent == null) {
                parent = ClassLoader.getSystemClassLoader();
            }
        }
        return parent;
    }
}