JettyModule.java 3.48 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1
/**
Matt Tucker's avatar
Matt Tucker committed
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
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14
package org.jivesoftware.messenger.container.spi;

import org.jivesoftware.messenger.container.*;
15
import org.jivesoftware.messenger.JiveGlobals;
Matt Tucker's avatar
Matt Tucker committed
16 17 18 19 20
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import java.io.File;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.WebApplicationContext;
21
import org.mortbay.log.*;
Matt Tucker's avatar
Matt Tucker committed
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

/**
 * A simple wrapper that allows Jetty to run inside the Messenger
 * container. Jetty settings are extracted from the ModuleContext.
 * The Jetty module is primarily designed to host the JSP web
 * administration interface to the server when running in standalone
 * mode without an external servlet container.
 *
 * @author Iain Shigeoka
 */
public class JettyModule implements Module {

    private Server jetty = null;
    private WebApplicationContext webAppContext = null;
    private Container serverContainer = null;
    private ServiceRegistration reg = null;
    private String port = null;

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

    public String getName() {
47
        return "Admin Console";
Matt Tucker's avatar
Matt Tucker committed
48 49
    }

50
    public void initialize(Container container) {
Matt Tucker's avatar
Matt Tucker committed
51 52
        try {
            // Configure logging to a file, creating log dir if needed
53 54 55 56 57 58 59 60 61 62
//            File logDir = new File(JiveGlobals.getMessengerHome(), "logs");
//            if (!logDir.exists()) {
//                logDir.mkdirs();
//            }
//            File logFile = new File(logDir, "admin_console.log");
//            OutputStreamLogSink logSink = new OutputStreamLogSink(logFile.toString());
//            logSink.start();
//            LogImpl log = (LogImpl)Factory.getFactory().getInstance("");
//            log.reset();
//            log.add(logSink);
Matt Tucker's avatar
Matt Tucker committed
63 64 65 66

            jetty = new Server();

            // Configure HTTP socket listener
67
            port = JiveGlobals.getProperty("embedded-web.port", "9090");
Matt Tucker's avatar
Matt Tucker committed
68 69 70 71
            jetty.addListener(port);
            this.serverContainer = container;

            // Add web-app
72 73 74 75
            // TODO this shouldn't be hardcoded to look for the "admin" plugin.
            webAppContext = jetty.addWebApplication("/", JiveGlobals.getMessengerHome() +
                    File.separator + "plugins" + File.separator +  "admin" +
                    File.separator + "webapp");
Matt Tucker's avatar
Matt Tucker committed
76
            webAppContext.setWelcomeFiles(new String[]{"index.jsp"});
Matt Tucker's avatar
Matt Tucker committed
77 78 79 80 81 82 83 84 85
        }
        catch (Exception e) {
            Log.error("Trouble initializing Jetty", e);
        }
    }

    public void start() {
        try {
            jetty.start();
Matt Tucker's avatar
Matt Tucker committed
86

Matt Tucker's avatar
Matt Tucker committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
            ServiceItem serverItem = new ServiceItem(null, this, null);
            reg = serverContainer.getServiceLookup().register(serverItem);
            Log.info("Started embedded web server on port: " + port);
        }
        catch (Exception e) {
            Log.error("Trouble starting Jetty", e);
            stop();
        }
    }

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

    public void destroy() {
    }
}