KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > starter > ServerStarter


1 /*
2  * $RCSfile: ServerStarter.java,v $
3  * $Revision: 1.9 $
4  * $Date: 2005/03/07 05:36:27 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.starter;
13
14 import org.jivesoftware.util.Log;
15
16 import java.io.File JavaDoc;
17
18 /**
19  * Starts the core XMPP server. A bootstrap class that configures classloaders
20  * to ensure easy, dynamic server startup.
21  *
22  * This class should be for standalone mode only. Jive Messenger servers launched
23  * through a J2EE container (servlet/EJB) will use those environment's
24  * classloading facilities to ensure proper startup.<p>
25  *
26  * Tasks:<ul>
27  * <li>Add all jars in the lib directory to the classpath.</li>
28  * <li>Add the config directory to the classpath for loadResource()</li>
29  * <li>Start the server</li>
30  * </ul>
31  *
32  * Note: if the enviroment property <tt>messenger.lib.directory</tt> is specified
33  * ServerStarter will attempt to use this value as the value for messenger's lib
34  * directory. If the property is not specified the default value of ../lib will be used.
35  *
36  * @author Iain Shigeoka
37  */

38 public class ServerStarter {
39
40     /**
41      * Default to this location if one has not been specified
42      */

43     private static final String JavaDoc DEFAULT_LIB_DIR = "../lib";
44
45     public static void main(String JavaDoc [] args) {
46         new ServerStarter().start();
47     }
48
49     /**
50      * Starts the server by loading and instantiating the bootstrap
51      * container. Once the start method is called, the server is
52      * started and the server starter should not be used again.
53      */

54     private void start() {
55         // setup the classpath using JiveClassLoader
56
try {
57             // Load up the bootstrap container
58
final ClassLoader JavaDoc parent = findParentClassLoader();
59
60             String JavaDoc libDirString = System.getProperty("messenger.lib.dir");
61
62             File JavaDoc libDir;
63             if (libDirString != null) {
64                 // If the lib directory property has been specified and it actually
65
// exists use it, else use the default
66
libDir = new File JavaDoc(libDirString);
67                 if (!libDir.exists()) {
68                     Log.warn("Lib directory " + libDirString +
69                             " does not exist. Using default " + DEFAULT_LIB_DIR);
70                     libDir = new File JavaDoc(DEFAULT_LIB_DIR);
71                 }
72             }
73             else {
74                 libDir = new File JavaDoc(DEFAULT_LIB_DIR);
75             }
76
77             ClassLoader JavaDoc loader = new JiveClassLoader(parent, libDir);
78            
79             Thread.currentThread().setContextClassLoader(loader);
80             Class JavaDoc containerClass = loader.loadClass(
81                     "org.jivesoftware.messenger.XMPPServer");
82             containerClass.newInstance();
83         }
84         catch (Exception JavaDoc e) {
85             e.printStackTrace();
86         }
87     }
88
89     /**
90      * Locates the best class loader based on context (see class description).
91      *
92      * @return The best parent classloader to use
93      */

94     private ClassLoader JavaDoc findParentClassLoader() {
95         ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
96         if (parent == null) {
97             parent = this.getClass().getClassLoader();
98             if (parent == null) {
99                 parent = ClassLoader.getSystemClassLoader();
100             }
101         }
102         return parent;
103     }
104 }
Popular Tags