AdminConsolePlugin.java 9.89 KB
Newer Older
1 2 3 4
/**
 * $Revision: 3034 $
 * $Date: 2005-11-04 21:02:33 -0300 (Fri, 04 Nov 2005) $
 *
5
 * Copyright (C) 2004-2006 Jive Software. All rights reserved.
6 7 8 9 10 11 12 13 14 15
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.container;

import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
16
import org.jivesoftware.wildfire.XMPPServer;
17
import org.mortbay.http.HttpContext;
18 19
import org.mortbay.http.HttpListener;
import org.mortbay.http.SunJsseListener;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
import org.mortbay.jetty.Server;
import org.mortbay.log.Factory;
import org.mortbay.log.LogImpl;
import org.mortbay.log.OutputStreamLogSink;
import org.mortbay.util.InetAddrPort;

import java.io.File;

/**
 * The admin console plugin. It starts a Jetty instance on the configured
 * port and loads the admin console web application.
 *
 * @author Matt Tucker
 */
public class AdminConsolePlugin implements Plugin {

    private static Server jetty = null;
    private int port;
    private int securePort;

    private File pluginDir;
    private HttpContext context = null;
    private HttpListener plainListener = null;
    private HttpListener secureListener = null;

    /**
     * Create a jetty module.
     */
    public AdminConsolePlugin() {
    }

    public void restartListeners() {
        try {
            String restarting = LocaleUtils.getLocalizedString("admin.console.restarting");
            System.out.println(restarting);
            Log.info(restarting);

            jetty.stop();
            if (plainListener != null) {
                jetty.removeListener(plainListener);
                plainListener = null;
            }
            if (secureListener != null) {
                jetty.removeListener(secureListener);
                secureListener = null;
            }
            jetty.removeContext(context);
            loadListeners();

69 70 71 72 73 74 75 76 77 78
            // Add web-app. Check to see if we're in development mode. If so, we don't
            // add the normal web-app location, but the web-app in the project directory.
            if (Boolean.getBoolean("developmentMode")) {
                System.out.println(LocaleUtils.getLocalizedString("admin.console.devmode"));
                context = jetty.addWebApplication("/",
                    pluginDir.getParentFile().getParentFile().getParent() + File.separator + "src" +
                            File.separator + "web");
            }
            else {
                context = jetty.addWebApplication("/",
79
                    pluginDir.getAbsoluteFile() + File.separator + "webapp");
80
            }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            context.setWelcomeFiles(new String[]{"index.jsp"});

            jetty.start();

            printListenerMessages();
        }
        catch (Exception e) {
            Log.error(e);
        }
    }

    private void loadListeners() throws Exception {
        // Configure HTTP socket listener. Setting the interface property to a
        // non null value will imply that the Jetty server will only
        // accept connect requests to that IP address.
96
        String interfaceName = JiveGlobals.getXMLProperty("network.interface");
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        port = JiveGlobals.getXMLProperty("adminConsole.port", 9090);
        InetAddrPort address = new InetAddrPort(interfaceName, port);
        if (port > 0) {
            plainListener = jetty.addListener(address);
        }

        try {
            securePort = JiveGlobals.getXMLProperty("adminConsole.securePort", 9091);
            if (securePort > 0) {
                SunJsseListener listener = new SunJsseListener();
                // Get the keystore location. The default location is security/keystore
                String keyStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.keystore",
                        "resources" + File.separator + "security" + File.separator + "keystore");
                // The location is relative to the home directory of the application.
                keyStoreLocation = JiveGlobals.getHomeDirectory() + File.separator + keyStoreLocation;

                // Get the keystore password. The default password is "changeit".
                String keypass = JiveGlobals.getProperty("xmpp.socket.ssl.keypass", "changeit");
                keypass = keypass.trim();

                listener.setKeystore(keyStoreLocation);
                listener.setKeyPassword(keypass);
                listener.setPassword(keypass);

                listener.setHost(interfaceName);
                listener.setPort(securePort);

                secureListener = jetty.addListener(listener);
            }
        }
        catch (Exception e) {
            Log.error(e);
        }
    }

    public void initializePlugin(PluginManager manager, File pluginDir) {
        this.pluginDir = pluginDir;
        try {
            // Configure logging to a file, creating log dir if needed
            System.setProperty("org.apache.commons.logging.LogFactory", "org.mortbay.log.Factory");
137 138 139 140 141 142 143 144 145 146 147 148 149 150
            File logDir = null;
            String logDirectory = JiveGlobals.getXMLProperty("log.directory");
            // Check if the "log.directory" was defined
            if (logDirectory != null) {
                // Remove last separator character (if any)
                if (!logDirectory.endsWith(File.separator)) {
                    logDirectory = logDirectory + File.separator;
                }
                logDir = new File(logDirectory);
            }
            else {
                // Create log file in the default directory
                logDir = new File(JiveGlobals.getHomeDirectory(), "logs");
            }
151 152 153 154 155 156
            if (!logDir.exists()) {
                logDir.mkdirs();
            }
            File logFile = new File(logDir, "admin-console.log");
            OutputStreamLogSink logSink = new OutputStreamLogSink(logFile.toString());
            logSink.start();
157 158 159 160 161 162 163 164 165 166 167 168 169 170
            // In some cases, commons-logging settings can be stomped by other
            // libraries in the classpath. Make sure that hasn't happened before
            // setting configuration.
            Object logImpl = Factory.getFactory().getInstance("");
            if (logImpl instanceof LogImpl) {
                LogImpl log = (LogImpl)logImpl;
                // Ignore INFO logs unless debugging turned on.
                if (!Log.isDebugEnabled()) {
                    log.setVerbose(-1);
                }
                else {
                    log.setVerbose(1);
                }
                log.add(logSink);
171 172 173 174 175 176
            }

            jetty = new Server();

            loadListeners();

177 178 179 180 181 182 183 184 185 186
            // Add web-app. Check to see if we're in development mode. If so, we don't
            // add the normal web-app location, but the web-app in the project directory.
            if (Boolean.getBoolean("developmentMode")) {
                System.out.println(LocaleUtils.getLocalizedString("admin.console.devmode"));
                context = jetty.addWebApplication("/",
                    pluginDir.getParentFile().getParentFile().getParent() + File.separator + "src" +
                            File.separator + "web");
            }
            else {
                context = jetty.addWebApplication("/",
187
                    pluginDir.getAbsoluteFile() + File.separator + "webapp");
188
            }
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            context.setWelcomeFiles(new String[]{"index.jsp"});

            jetty.start();

            printListenerMessages();
        }
        catch (Exception e) {
            System.err.println("Error starting admin console: " + e.getMessage()); 
            Log.error("Trouble initializing admin console", e);
        }
    }

    public void destroyPlugin() {
        plainListener = null;
        secureListener = null;
        try {
            if (jetty != null) {
                jetty.stop();
                jetty = null;
            }
        }
        catch (InterruptedException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }

    /**
     * Returns the Jetty instance started by this plugin.
     *
     * @return the Jetty server instance.
     */
    public static Server getJettyServer() {
        return jetty;
    }

    /**
     * Writes out startup messages for the listeners.
     */
    private void printListenerMessages() {
        String warning = LocaleUtils.getLocalizedString("admin.console.warning");
        String listening = LocaleUtils.getLocalizedString("admin.console.listening");

        if (plainListener == null && secureListener == null) {
            Log.info(warning);
            System.out.println(warning);
        }
235
        else if (plainListener == null) {
236 237 238 239 240
            Log.info(listening + " https://" +
                    XMPPServer.getInstance().getServerInfo().getName() + ":" + securePort);
            System.out.println(listening + " https://" +
                    XMPPServer.getInstance().getServerInfo().getName() + ":" + securePort);
        }
241
        else if (secureListener == null) {
242 243 244 245 246 247
            Log.info(listening + " http://" +
                    XMPPServer.getInstance().getServerInfo().getName() + ":" + port);
            System.out.println(listening + " http://" +
                    XMPPServer.getInstance().getServerInfo().getName() + ":" + port);
        }
        else {
248
            String msg = listening + ":" + System.getProperty("line.separator") +
249
                    "  http://" + XMPPServer.getInstance().getServerInfo().getName() + ":" +
250
                    port + System.getProperty("line.separator") +
251 252 253 254 255 256 257
                    "  https://" + XMPPServer.getInstance().getServerInfo().getName() + ":" +
                    securePort;
            Log.info(msg);
            System.out.println(msg);
        }
    }
}