AdminConsolePlugin.java 6.91 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1
/**
2

Matt Tucker's avatar
Matt Tucker committed
3
 * $RCSfile$
4

Matt Tucker's avatar
Matt Tucker committed
5
 * $Revision$
6

Matt Tucker's avatar
Matt Tucker committed
7
 * $Date$
8

Matt Tucker's avatar
Matt Tucker committed
9
 *
10

Matt Tucker's avatar
Matt Tucker committed
11
 * Copyright (C) 2004 Jive Software. All rights reserved.
12

Matt Tucker's avatar
Matt Tucker committed
13
 *
14

Matt Tucker's avatar
Matt Tucker committed
15
 * This software is published under the terms of the GNU Public License (GPL),
16

Matt Tucker's avatar
Matt Tucker committed
17
 * a copy of which is included in this distribution.
18

Matt Tucker's avatar
Matt Tucker committed
19
 */
Matt Tucker's avatar
Matt Tucker committed
20

Matt Tucker's avatar
Matt Tucker committed
21
package org.jivesoftware.messenger.container;
Matt Tucker's avatar
Matt Tucker committed
22

23
import org.jivesoftware.messenger.XMPPServer;
24
import org.jivesoftware.util.JiveGlobals;
Matt Tucker's avatar
Matt Tucker committed
25 26
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
27
import org.mortbay.http.SunJsseListener;
Matt Tucker's avatar
Matt Tucker committed
28 29
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.WebApplicationContext;
30 31 32
import org.mortbay.log.Factory;
import org.mortbay.log.LogImpl;
import org.mortbay.log.OutputStreamLogSink;
33 34
import org.mortbay.util.InetAddrPort;

35
import java.io.File;
Matt Tucker's avatar
Matt Tucker committed
36 37

/**
Matt Tucker's avatar
Matt Tucker committed
38 39
 * The admin console plugin. It starts a Jetty instance on the configured
 * port and loads the admin console web application.
Matt Tucker's avatar
Matt Tucker committed
40
 *
Matt Tucker's avatar
Matt Tucker committed
41
 * @author Matt Tucker
Matt Tucker's avatar
Matt Tucker committed
42
 */
Matt Tucker's avatar
Matt Tucker committed
43
public class AdminConsolePlugin implements Plugin {
Matt Tucker's avatar
Matt Tucker committed
44

45
    private static Server jetty = null;
46
    private String interfaceName;
47 48
    private int port;
    private int securePort;
Matt Tucker's avatar
Matt Tucker committed
49 50 51 52

    /**
     * Create a jetty module.
     */
Matt Tucker's avatar
Matt Tucker committed
53
    public AdminConsolePlugin() {
Matt Tucker's avatar
Matt Tucker committed
54 55
    }

Matt Tucker's avatar
Matt Tucker committed
56
    public void initializePlugin(PluginManager manager, File pluginDir) {
Matt Tucker's avatar
Matt Tucker committed
57 58
        try {
            // Configure logging to a file, creating log dir if needed
59
            System.setProperty("org.apache.commons.logging.LogFactory", "org.mortbay.log.Factory");
60
            File logDir = new File(JiveGlobals.getHomeDirectory(), "logs");
Matt Tucker's avatar
Matt Tucker committed
61 62 63 64 65 66
            if (!logDir.exists()) {
                logDir.mkdirs();
            }
            File logFile = new File(logDir, "admin-console.log");
            OutputStreamLogSink logSink = new OutputStreamLogSink(logFile.toString());
            logSink.start();
67
            LogImpl log = (LogImpl) Factory.getFactory().getInstance("");
Matt Tucker's avatar
Matt Tucker committed
68 69 70
            // Ignore INFO logs.
            log.setVerbose(-1);
            log.add(logSink);
Matt Tucker's avatar
Matt Tucker committed
71 72 73

            jetty = new Server();

74
            // Configure HTTP socket listener
75
            boolean plainStarted = false;
76 77
            // Setting this property to a not null value will imply that the Jetty server will only
            // accept connect requests to that IP address
78
            interfaceName = JiveGlobals.getXMLProperty("adminConsole.interface");
79
            port = JiveGlobals.getXMLProperty("adminConsole.port", 9090);
80
            InetAddrPort address = new InetAddrPort(interfaceName, port);
81
            if (port > 0) {
82
                jetty.addListener(address);
83 84
                plainStarted = true;
            }
85

86 87 88
            boolean secureStarted = false;
            try {
                securePort = JiveGlobals.getXMLProperty("adminConsole.securePort", 9091);
89 90 91 92 93
                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");
94
                    keyStoreLocation = JiveGlobals.getHomeDirectory() + File.separator + keyStoreLocation;
95 96 97 98 99 100 101 102

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

                    // Get the truststore location; default at security/truststore
                    String trustStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.truststore",
                            "resources" + File.separator + "security" + File.separator + "truststore");
103
                    trustStoreLocation = JiveGlobals.getHomeDirectory() + File.separator +
104 105 106 107 108 109 110 111 112 113
                            trustStoreLocation;

                    // Get the truststore passwprd; default is "changeit".
                    String trustpass = JiveGlobals.getProperty("xmpp.socket.ssl.trustpass", "changeit");
                    trustpass = trustpass.trim();

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

114
                    listener.setHost(interfaceName);
115 116 117 118 119
                    listener.setPort(securePort);

                    jetty.addListener(listener);
                    secureStarted = true;
                }
120 121 122 123
            }
            catch (Exception e) {
                Log.error(e);
            }
Matt Tucker's avatar
Matt Tucker committed
124 125

            // Add web-app
Matt Tucker's avatar
Matt Tucker committed
126 127
            WebApplicationContext webAppContext = jetty.addWebApplication("/",
                    pluginDir.getAbsoluteFile() + File.separator + "webapp");
Matt Tucker's avatar
Matt Tucker committed
128
            webAppContext.setWelcomeFiles(new String[]{"index.jsp"});
Matt Tucker's avatar
Matt Tucker committed
129 130

            jetty.start();
Matt Tucker's avatar
Matt Tucker committed
131

132 133 134
            String warning = LocaleUtils.getLocalizedString("admin.console.warning");
            String listening = LocaleUtils.getLocalizedString("admin.console.listening");

135
            if (!plainStarted && !secureStarted) {
136 137
                Log.info(warning);
                System.out.println(warning);
138 139
            }
            else if (!plainStarted && secureStarted) {
140
                Log.info(listening + " https://" +
141
                        XMPPServer.getInstance().getServerInfo().getName() + ":" + securePort);
142
                System.out.println(listening + " https://" +
143 144 145
                        XMPPServer.getInstance().getServerInfo().getName() + ":" + securePort);
            }
            else if (!secureStarted && plainStarted) {
146
                Log.info(listening + " http://" +
147
                        XMPPServer.getInstance().getServerInfo().getName() + ":" + port);
148
                System.out.println(listening + " http://" +
149 150 151
                        XMPPServer.getInstance().getServerInfo().getName() + ":" + port);
            }
            else {
152
                String msg = listening + ":\n" +
153 154 155 156 157 158
                        "  http://" + XMPPServer.getInstance().getServerInfo().getName() + ":" +
                        port + "\n" +
                        "  https://" + XMPPServer.getInstance().getServerInfo().getName() + ":" +
                        securePort;
                Log.info(msg);
                System.out.println(msg);
159
            }
Matt Tucker's avatar
Matt Tucker committed
160 161
        }
        catch (Exception e) {
162
            System.err.println("Error starting admin console: " + e.getMessage()); 
Matt Tucker's avatar
Matt Tucker committed
163
            Log.error("Trouble initializing admin console", e);
Matt Tucker's avatar
Matt Tucker committed
164 165 166
        }
    }

Matt Tucker's avatar
Matt Tucker committed
167
    public void destroyPlugin() {
Matt Tucker's avatar
Matt Tucker committed
168 169 170 171 172 173 174 175 176 177
        try {
            if (jetty != null) {
                jetty.stop();
                jetty = null;
            }
        }
        catch (InterruptedException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }
178 179 180 181 182 183 184 185 186

    /**
     * Returns the Jetty instance started by this plugin.
     *
     * @return the Jetty server instance.
     */
    public static Server getJettyServer() {
        return jetty;
    }
Matt Tucker's avatar
Matt Tucker committed
187
}