1 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 ; 35 import java.io.IOException ; 36 37 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 62 private boolean setupMode = true; 63 64 private String homeDir; 65 private XMLProperties properties; 66 67 public static void main(String [] args) { 68 if (args.length < 2) { 69 System.err.println("Usage ServerContainer <absolute path to home folder> <config filename>"); 70 return; 71 } 72 String homeDir = args[0]; 73 XMLProperties properties = null; 74 try { 75 properties = new XMLProperties(homeDir + "/conf/" + args[1]); 76 } catch (IOException 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 getHomeDirectory() { 91 return homeDir; 92 } 93 94 void setHomeDirectory(String 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 System.setProperty("org.apache.commons.logging.LogFactory", "org.mortbay.log.Factory"); 110 File logDir = new File (homeDir, "logs"); 111 if (!logDir.exists()) { 112 logDir.mkdirs(); 113 } 114 File logFile = new File (logDir, "admin-console.log"); 115 OutputStreamLogSink logSink = new OutputStreamLogSink(logFile.toString()); 116 logSink.start(); 117 LogImpl log = (LogImpl) Factory.getFactory().getInstance(""); 118 log.setVerbose(-1); 120 log.add(logSink); 121 122 jetty = new Server(); 123 124 boolean plainStarted = false; 126 String interfaceName = properties.getProperty("adminConsole.inteface"); 129 String 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 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 String 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 String keypass = properties.getProperty("xmpp.socket.ssl.keypass"); 154 keypass = (keypass == null ? "changeit" : keypass); 155 keypass = keypass.trim(); 156 157 String 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 String 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 e) { 182 e.printStackTrace(); 183 } 184 185 if ("true".equals(properties.getProperty("setup"))) { 186 setupMode = false; 187 } 188 189 String 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 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 WebApplicationContext webAppContext = jetty.addWebApplication("/", 204 homeDir + File.separator + "webapp"); 205 webAppContext.setWelcomeFiles(new String []{"index.jsp"}); 206 207 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 msg = "Admin console listening at:\n" + 224 " port: " + adminPort + "\n" + 225 " secure port: " + adminSecurePort; 226 manager.getLog().info(msg); 227 } 229 230 File componentDir = new File (homeDir, "components"); 232 componentFinder = new ComponentFinder(this, componentDir); 233 componentFinder.start(); 234 } 235 catch (Exception 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 |