ServerStarter.java 7.25 KB
Newer Older
1 2 3 4 5
/*
 * $RCSfile$
 * $Revision: 1089 $
 * $Date: 2005-03-07 02:36:27 -0300 (Mon, 07 Mar 2005) $
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * 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.
19 20
 */

21
package org.jivesoftware.openfire.starter;
22

Matt Tucker's avatar
Matt Tucker committed
23 24 25
import java.io.*;
import java.util.jar.Pack200;
import java.util.jar.JarOutputStream;
26

27
import  org.jivesoftware.util.Log;
28 29 30 31
/**
 * Starts the core XMPP server. A bootstrap class that configures classloaders
 * to ensure easy, dynamic server startup.
 *
32
 * This class should be for standalone mode only. Openfire servers launched
33 34 35 36
 * through a J2EE container (servlet/EJB) will use those environment's
 * classloading facilities to ensure proper startup.<p>
 *
 * Tasks:<ul>
Matt Tucker's avatar
Matt Tucker committed
37
 *      <li>Unpack any pack files in the lib directory (Pack200 encoded JAR files).</li>
38 39 40 41 42
 *      <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>
 *
43
 * Note: if the enviroment property <tt>openfire.lib.dir</tt> is specified
44
 * ServerStarter will attempt to use this value as the value for openfire's lib
45 46 47 48 49 50 51 52 53 54
 * directory. If the property is not specified the default value of ../lib will be used.
 *
 * @author Iain Shigeoka
 */
public class ServerStarter {

    /**
     * Default to this location if one has not been specified
     */
    private static final String DEFAULT_LIB_DIR = "../lib";
55
    private static final String DEFAULT_ADMIN_LIB_DIR = "../plugins/admin/webapp/WEB-INF/lib";
56 57 58 59 60 61 62 63 64 65 66

    public static void main(String [] args) {
        new ServerStarter().start();
    }

    /**
     * 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() {
67
        // Setup the classpath using JiveClassLoader
68 69 70 71
        try {
            // Load up the bootstrap container
            final ClassLoader parent = findParentClassLoader();

Gaston Dombiak's avatar
Gaston Dombiak committed
72
            String libDirString = System.getProperty("openfire.lib.dir");
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

            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. Using default " + DEFAULT_LIB_DIR);
                    libDir = new File(DEFAULT_LIB_DIR);
                }
            }
            else {
                libDir = new File(DEFAULT_LIB_DIR);
            }

Matt Tucker's avatar
Matt Tucker committed
89
            // Unpack any pack files.
90 91
            unpackArchives(libDir, true);

92 93 94 95 96 97 98 99
            String adminLibDirString = System.getProperty("openfireHome");
            if (adminLibDirString == null) {
                adminLibDirString = DEFAULT_ADMIN_LIB_DIR;
            }
            else {
                adminLibDirString = adminLibDirString+"/plugins/admin/webapp/WEB-INF/lib";
            }
            File adminLibDir = new File(adminLibDirString);
100 101 102
            if (adminLibDir.exists()) {
                unpackArchives(adminLibDir, false);
            }
103 104 105 106
            else {
                Log.warn("Admin Lib Directory " + adminLibDirString +
                    " does not exist. Web admin console may not work.");
            }
Matt Tucker's avatar
Matt Tucker committed
107

108
            ClassLoader loader = new JiveClassLoader(parent, libDir);
Matt Tucker's avatar
Matt Tucker committed
109

110 111
            Thread.currentThread().setContextClassLoader(loader);
            Class containerClass = loader.loadClass(
112
                    "org.jivesoftware.openfire.XMPPServer");
113 114 115
            containerClass.newInstance();
        }
        catch (Exception e) {
116
            e.printStackTrace();
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        }
    }

    /**
     * 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;
    }
Matt Tucker's avatar
Matt Tucker committed
135

Matt Tucker's avatar
Matt Tucker committed
136 137 138 139 140 141
    /**
     * Converts any pack files in a directory into standard JAR files. Each
     * pack file will be deleted after being converted to a JAR. If no
     * pack files are found, this method does nothing.
     *
     * @param libDir the directory containing pack files.
142
     * @param printStatus true if status ellipses should be printed when unpacking.
Matt Tucker's avatar
Matt Tucker committed
143
     */
144
    private void unpackArchives(File libDir, boolean printStatus) {
Matt Tucker's avatar
Matt Tucker committed
145 146 147 148 149 150
        // Get a list of all packed files in the lib directory.
        File [] packedFiles = libDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".pack");
            }
        });
151 152 153 154 155 156

        if (packedFiles == null) {
            // Do nothing since no .pack files were found
            return;
        }

Matt Tucker's avatar
Matt Tucker committed
157 158 159 160 161 162
        // Unpack each.
        boolean unpacked = false;
        for (File packedFile : packedFiles) {
            try {
                String jarName = packedFile.getName().substring(0,
                        packedFile.getName().length() - ".pack".length());
163
                // Delete JAR file with same name if it exists (could be due to upgrade
164
                // from old Openfire release).
165 166 167 168 169
                File jarFile = new File(libDir, jarName);
                if (jarFile.exists()) {
                    jarFile.delete();
                }
                
Matt Tucker's avatar
Matt Tucker committed
170 171 172 173
                InputStream in = new BufferedInputStream(new FileInputStream(packedFile));
                JarOutputStream out = new JarOutputStream(new BufferedOutputStream(
                        new FileOutputStream(new File(libDir, jarName))));
                Pack200.Unpacker unpacker = Pack200.newUnpacker();
Matt Tucker's avatar
Matt Tucker committed
174
                // Print something so the user knows something is happening.
175 176 177
                if (printStatus) {
                    System.out.print(".");
                }
Matt Tucker's avatar
Matt Tucker committed
178 179 180 181 182
                // Call the unpacker
                unpacker.unpack(in, out);

                in.close();
                out.close();
Matt Tucker's avatar
Matt Tucker committed
183
                packedFile.delete();
Matt Tucker's avatar
Matt Tucker committed
184 185 186
                unpacked = true;
            }
            catch (Exception e) {
187
                e.printStackTrace();
Matt Tucker's avatar
Matt Tucker committed
188 189
            }
        }
Matt Tucker's avatar
Matt Tucker committed
190
        // Print newline if unpacking happened.
191
        if (unpacked && printStatus) {
Matt Tucker's avatar
Matt Tucker committed
192
            System.out.println();
Matt Tucker's avatar
Matt Tucker committed
193 194
        }
    }
195
}