KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > whack > container > ServerContainer


1 /**
2  * $RCSfile: ServerContainer.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/05/28 04:54:35 $
5  *
6  * Copyright 2005 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.whack.container;
22
23 import org.jivesoftware.util.XMLProperties;
24 import org.jivesoftware.whack.ExternalComponentManager;
25 import org.mortbay.http.SunJsseListener;
26 import org.mortbay.jetty.Server;
27 import org.mortbay.jetty.servlet.WebApplicationContext;
28 import org.mortbay.log.Factory;
29 import org.mortbay.log.LogImpl;
30 import org.mortbay.log.OutputStreamLogSink;
31 import org.mortbay.util.InetAddrPort;
32 import org.xmpp.component.ComponentManager;
33
34 import java.io.File JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 /**
38  * Starts the web server and components finder. A bootstrap class that will start the Jetty server
39  * for which it requires to receive two parameters when launched. The first parameter is the
40  * absolute path to the root folder that contains:
41  * <pre><ul>
42  * <li><tt>conf</tt> - folder that holds Whack's configuration file</li>
43  * <li><tt>components</tt> - folder that holds the components' jar files</li>
44  * <li><tt>resources/security</tt> - folder that holds the key stores for the https protocol</li>
45  * <li><tt>webapp</tt> - folder that holds the JSP pages of the admin console</li>
46  * </ul></pre>
47  * The second parameter is the name of the configuration file that holds Whack's configuration.
48  * This file must be located in the <tt>conf</tt> folder under the root folder.
49  *
50  * @author Gaston Dombiak
51  */

52 public class ServerContainer {
53
54     private static final ServerContainer instance = new ServerContainer();
55     private Server jetty;
56     private ExternalComponentManager manager;
57     private ComponentFinder componentFinder;
58
59     /**
60      * True if in setup mode
61      */

62     private boolean setupMode = true;
63
64     private String JavaDoc homeDir;
65     private XMLProperties properties;
66
67     public static void main(String JavaDoc[] args) {
68         if (args.length < 2) {
69             System.err.println("Usage ServerContainer <absolute path to home folder> <config filename>");
70             return;
71         }
72         String JavaDoc homeDir = args[0];
73         XMLProperties properties = null;
74         try {
75             properties = new XMLProperties(homeDir + "/conf/" + args[1]);
76         } catch (IOException JavaDoc e) {
77             e.printStackTrace();
78             return;
79         }
80         instance.setHomeDirectory(homeDir);
81         instance.setProperties(properties);
82         instance.start();
83
84     }
85
86     public static ServerContainer getInstance() {
87         return instance;
88     }
89
90     public String JavaDoc getHomeDirectory() {
91         return homeDir;
92     }
93
94     void setHomeDirectory(String JavaDoc homeDir) {
95         this.homeDir = homeDir;
96     }
97
98     public XMLProperties getProperties() {
99         return properties;
100     }
101
102     void setProperties(XMLProperties properties) {
103         this.properties = properties;
104     }
105
106     public void start() {
107         try {
108             // Configure logging to a file, creating log dir if needed
109
System.setProperty("org.apache.commons.logging.LogFactory", "org.mortbay.log.Factory");
110             File JavaDoc logDir = new File JavaDoc(homeDir, "logs");
111             if (!logDir.exists()) {
112                 logDir.mkdirs();
113             }
114             File JavaDoc logFile = new File JavaDoc(logDir, "admin-console.log");
115             OutputStreamLogSink logSink = new OutputStreamLogSink(logFile.toString());
116             logSink.start();
117             LogImpl log = (LogImpl) Factory.getFactory().getInstance("");
118             // Ignore INFO logs.
119
log.setVerbose(-1);
120             log.add(logSink);
121
122             jetty = new Server();
123
124             // Configure HTTP socket listener
125
boolean plainStarted = false;
126             // Setting this property to a not null value will imply that the Jetty server will only
127
// accept connect requests to that IP address
128
String JavaDoc interfaceName = properties.getProperty("adminConsole.inteface");
129             String JavaDoc port = properties.getProperty("adminConsole.port");
130             int adminPort = (port == null ? 9090 : Integer.parseInt(port));
131             InetAddrPort address = new InetAddrPort(interfaceName, adminPort);
132             if (adminPort > 0) {
133                 jetty.addListener(address);
134                 plainStarted = true;
135             }
136
137             boolean secureStarted = false;
138             String JavaDoc securePortProperty = properties.getProperty("adminConsole.securePort");
139             int adminSecurePort = 9091;
140             try {
141                 adminSecurePort = (securePortProperty == null ? 9091 : Integer.parseInt(securePortProperty));
142                 if (adminSecurePort > 0) {
143                     SunJsseListener listener = new SunJsseListener();
144                     // Get the keystore location. The default location is security/keystore
145
String JavaDoc keyStoreLocation = properties.getProperty("xmpp.socket.ssl.keystore");
146                     keyStoreLocation = (keyStoreLocation == null ?
147                             "resources" + File.separator + "security" + File.separator +
148                             "keystore" :
149                             keyStoreLocation);
150                     keyStoreLocation = homeDir + File.separator + keyStoreLocation;
151
152                     // Get the keystore password. The default password is "changeit".
153
String JavaDoc keypass = properties.getProperty("xmpp.socket.ssl.keypass");
154                     keypass = (keypass == null ? "changeit" : keypass);
155                     keypass = keypass.trim();
156
157                     // Get the truststore location; default at security/truststore
158
String JavaDoc trustStoreLocation = properties.getProperty("xmpp.socket.ssl.truststore");
159                     trustStoreLocation = (trustStoreLocation == null ?
160                             "resources" + File.separator + "security" + File.separator +
161                             "truststore" :
162                             trustStoreLocation);
163                     trustStoreLocation = homeDir + File.separator + trustStoreLocation;
164
165                     // Get the truststore passwprd; default is "changeit".
166
String JavaDoc trustpass = properties.getProperty("xmpp.socket.ssl.trustpass");
167                     trustpass = (trustpass == null ? "changeit" : trustpass);
168                     trustpass = trustpass.trim();
169
170                     listener.setKeystore(keyStoreLocation);
171                     listener.setKeyPassword(keypass);
172                     listener.setPassword(keypass);
173
174                     listener.setHost(interfaceName);
175                     listener.setPort(adminSecurePort);
176
177                     jetty.addListener(listener);
178                     secureStarted = true;
179                 }
180             }
181             catch (Exception JavaDoc e) {
182                 e.printStackTrace();
183             }
184
185             if ("true".equals(properties.getProperty("setup"))) {
186                 setupMode = false;
187             }
188
189             // Start the ExternalComponentManager
190
String JavaDoc xmppServerHost = properties.getProperty("xmppServer.host");
191             port = properties.getProperty("xmppServer.port");
192             int xmppServerPort = (port == null ? 10015 : Integer.parseInt(port));
193             manager = new ExternalComponentManager(xmppServerHost, xmppServerPort);
194             String JavaDoc serverDomain = properties.getProperty("xmppServer.domain");
195             if (serverDomain != null) {
196                 manager.setServerName(serverDomain);
197             }
198             if (properties.getProperty("xmppServer.defaultSecretKey") != null) {
199                 manager.setDefaultSecretKey(properties.getProperty("xmppServer.defaultSecretKey"));
200             }
201
202             // Add web-app
203
WebApplicationContext webAppContext = jetty.addWebApplication("/",
204                     homeDir + File.separator + "webapp");
205             webAppContext.setWelcomeFiles(new String JavaDoc[]{"index.jsp"});
206
207             // Start the http server
208
jetty.start();
209
210             if (!plainStarted && !secureStarted) {
211                 manager.getLog().info("Warning: admin console not started due to configuration settings.");
212                 System.out.println("Warning: admin console not started due to configuration settings.");
213             }
214             else if (!plainStarted && secureStarted) {
215                 manager.getLog().info("Admin console listening at secure port: " + adminSecurePort);
216                 System.out.println("Admin console listening at secure port: " + adminSecurePort);
217             }
218             else if (!secureStarted && plainStarted) {
219                 manager.getLog().info("Admin console listening at port: " + adminPort);
220                 System.out.println("Admin console listening at port: " + adminPort);
221             }
222             else {
223                 String JavaDoc msg = "Admin console listening at:\n" +
224                         " port: " + adminPort + "\n" +
225                         " secure port: " + adminSecurePort;
226                 manager.getLog().info(msg);
227                 //System.out.println(msg);
228
}
229
230             // Load detected components.
231
File JavaDoc componentDir = new File JavaDoc(homeDir, "components");
232             componentFinder = new ComponentFinder(this, componentDir);
233             componentFinder.start();
234         }
235         catch (Exception JavaDoc e) {
236             e.printStackTrace();
237         }
238     }
239
240     public boolean isSetupMode() {
241         return setupMode;
242     }
243
244     public ComponentManager getManager() {
245         return manager;
246     }
247 }
248
Popular Tags