ServerStarter.java 3.37 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
import java.io.File;

/**
Matt Tucker's avatar
Matt Tucker committed
19
 * Starts the core XMPP server. A bootstrap class that configures classloaders
Matt Tucker's avatar
Matt Tucker committed
20 21 22 23 24 25 26 27 28 29 30 31
 * 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>
 *
Matt Tucker's avatar
Matt Tucker committed
32 33 34
 * Note: if the enviroment property <tt>messenger.lib.directory</tt> 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.
35
 *
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

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

            File libDir;
Matt Tucker's avatar
Matt Tucker committed
63 64
            if (libDirString != null) {
                // If the lib directory property has been specified and it actually
65 66
                // exists use it, else use the default
                libDir = new File(libDirString);
Matt Tucker's avatar
Matt Tucker committed
67 68 69
                if (!libDir.exists()) {
                    Log.warn("Lib directory " + libDirString +
                            " does not exist. Using default " + DEFAULT_LIB_DIR);
70 71
                    libDir = new File(DEFAULT_LIB_DIR);
                }
Matt Tucker's avatar
Matt Tucker committed
72 73
            }
            else {
74 75 76
                libDir = new File(DEFAULT_LIB_DIR);
            }

Matt Tucker's avatar
Matt Tucker committed
77 78 79 80
            ClassLoader loader = new JiveClassLoader(parent, libDir);
           
            Thread.currentThread().setContextClassLoader(loader);
            Class containerClass = loader.loadClass(
81
                    "org.jivesoftware.messenger.XMPPServer");
Matt Tucker's avatar
Matt Tucker committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            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;
    }
}