KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > container > AdminConsolePlugin


1 /**
2
3  * $RCSfile: AdminConsolePlugin.java,v $
4
5  * $Revision: 1.18 $
6
7  * $Date: 2005/05/14 21:22:56 $
8
9  *
10
11  * Copyright (C) 2004 Jive Software. All rights reserved.
12
13  *
14
15  * This software is published under the terms of the GNU Public License (GPL),
16
17  * a copy of which is included in this distribution.
18
19  */

20
21 package org.jivesoftware.messenger.container;
22
23 import org.jivesoftware.messenger.XMPPServer;
24 import org.jivesoftware.util.JiveGlobals;
25 import org.jivesoftware.util.LocaleUtils;
26 import org.jivesoftware.util.Log;
27 import org.mortbay.http.SunJsseListener;
28 import org.mortbay.jetty.Server;
29 import org.mortbay.jetty.servlet.WebApplicationContext;
30 import org.mortbay.log.Factory;
31 import org.mortbay.log.LogImpl;
32 import org.mortbay.log.OutputStreamLogSink;
33 import org.mortbay.util.InetAddrPort;
34
35 import java.io.File JavaDoc;
36
37 /**
38  * The admin console plugin. It starts a Jetty instance on the configured
39  * port and loads the admin console web application.
40  *
41  * @author Matt Tucker
42  */

43 public class AdminConsolePlugin implements Plugin {
44
45     private static Server jetty = null;
46     private String JavaDoc interfaceName;
47     private int port;
48     private int securePort;
49
50     /**
51      * Create a jetty module.
52      */

53     public AdminConsolePlugin() {
54     }
55
56     public void initializePlugin(PluginManager manager, File JavaDoc pluginDir) {
57         try {
58             // Configure logging to a file, creating log dir if needed
59
System.setProperty("org.apache.commons.logging.LogFactory", "org.mortbay.log.Factory");
60             File JavaDoc logDir = new File JavaDoc(JiveGlobals.getHomeDirectory(), "logs");
61             if (!logDir.exists()) {
62                 logDir.mkdirs();
63             }
64             File JavaDoc logFile = new File JavaDoc(logDir, "admin-console.log");
65             OutputStreamLogSink logSink = new OutputStreamLogSink(logFile.toString());
66             logSink.start();
67             LogImpl log = (LogImpl) Factory.getFactory().getInstance("");
68             // Ignore INFO logs.
69
log.setVerbose(-1);
70             log.add(logSink);
71
72             jetty = new Server();
73
74             // Configure HTTP socket listener
75
boolean plainStarted = false;
76             // Setting this property to a not null value will imply that the Jetty server will only
77
// 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                 plainStarted = true;
84             }
85
86             boolean secureStarted = false;
87             try {
88                 securePort = JiveGlobals.getXMLProperty("adminConsole.securePort", 9091);
89                 if (securePort > 0) {
90                     SunJsseListener listener = new SunJsseListener();
91                     // Get the keystore location. The default location is security/keystore
92
String JavaDoc keyStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.keystore",
93                             "resources" + File.separator + "security" + File.separator + "keystore");
94                     keyStoreLocation = JiveGlobals.getHomeDirectory() + File.separator + keyStoreLocation;
95
96                     // Get the keystore password. The default password is "changeit".
97
String JavaDoc keypass = JiveGlobals.getProperty("xmpp.socket.ssl.keypass", "changeit");
98                     keypass = keypass.trim();
99
100                     // Get the truststore location; default at security/truststore
101
String JavaDoc trustStoreLocation = JiveGlobals.getProperty("xmpp.socket.ssl.truststore",
102                             "resources" + File.separator + "security" + File.separator + "truststore");
103                     trustStoreLocation = JiveGlobals.getHomeDirectory() + File.separator +
104                             trustStoreLocation;
105
106                     // Get the truststore passwprd; default is "changeit".
107
String JavaDoc trustpass = JiveGlobals.getProperty("xmpp.socket.ssl.trustpass", "changeit");
108                     trustpass = trustpass.trim();
109
110                     listener.setKeystore(keyStoreLocation);
111                     listener.setKeyPassword(keypass);
112                     listener.setPassword(keypass);
113
114                     listener.setHost(interfaceName);
115                     listener.setPort(securePort);
116
117                     jetty.addListener(listener);
118                     secureStarted = true;
119                 }
120             }
121             catch (Exception JavaDoc e) {
122                 Log.error(e);
123             }
124
125             // Add web-app
126
WebApplicationContext webAppContext = jetty.addWebApplication("/",
127                     pluginDir.getAbsoluteFile() + File.separator + "webapp");
128             webAppContext.setWelcomeFiles(new String JavaDoc[]{"index.jsp"});
129
130             jetty.start();
131
132             String JavaDoc warning = LocaleUtils.getLocalizedString("admin.console.warning");
133             String JavaDoc listening = LocaleUtils.getLocalizedString("admin.console.listening");
134
135             if (!plainStarted && !secureStarted) {
136                 Log.info(warning);
137                 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                         XMPPServer.getInstance().getServerInfo().getName() + ":" + securePort);
144             }
145             else if (!secureStarted && plainStarted) {
146                 Log.info(listening + " http://" +
147                         XMPPServer.getInstance().getServerInfo().getName() + ":" + port);
148                 System.out.println(listening + " http://" +
149                         XMPPServer.getInstance().getServerInfo().getName() + ":" + port);
150             }
151             else {
152                 String JavaDoc msg = listening + ":\n" +
153                         " http://" + XMPPServer.getInstance().getServerInfo().getName() + ":" +
154                         port + "\n" +
155                         " https://" + XMPPServer.getInstance().getServerInfo().getName() + ":" +
156                         securePort;
157                 Log.info(msg);
158                 System.out.println(msg);
159             }
160         }
161         catch (Exception JavaDoc e) {
162             System.err.println("Error starting admin console: " + e.getMessage());
163             Log.error("Trouble initializing admin console", e);
164         }
165     }
166
167     public void destroyPlugin() {
168         try {
169             if (jetty != null) {
170                 jetty.stop();
171                 jetty = null;
172             }
173         }
174         catch (InterruptedException JavaDoc e) {
175             Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
176         }
177     }
178
179     /**
180      * Returns the Jetty instance started by this plugin.
181      *
182      * @return the Jetty server instance.
183      */

184     public static Server getJettyServer() {
185         return jetty;
186     }
187 }
Popular Tags