1 25 package org.ofbiz.jetty.container; 26 27 import java.io.IOException ; 28 import java.net.UnknownHostException ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import org.apache.log4j.Logger; 36 import org.apache.log4j.Priority; 37 import org.mortbay.http.NCSARequestLog; 38 import org.mortbay.http.SocketListener; 39 import org.mortbay.http.SunJsseListener; 40 import org.mortbay.http.ajp.AJP13Listener; 41 import org.mortbay.jetty.Server; 42 import org.mortbay.jetty.servlet.WebApplicationContext; 43 import org.mortbay.jetty.servlet.SessionManager; 44 import org.mortbay.jetty.servlet.HashSessionManager; 45 import org.mortbay.util.Frame; 46 import org.mortbay.util.Log; 47 import org.mortbay.util.LogSink; 48 import org.mortbay.util.MultiException; 49 import org.mortbay.util.ThreadedServer; 50 51 import org.ofbiz.base.component.ComponentConfig; 52 import org.ofbiz.base.util.Debug; 53 import org.ofbiz.base.util.UtilURL; 54 import org.ofbiz.base.util.SSLUtil; 55 import org.ofbiz.base.container.Container; 56 import org.ofbiz.base.container.ContainerException; 57 import org.ofbiz.base.container.ContainerConfig; 58 59 67 public class JettyContainer implements Container { 68 69 public static final String module = JettyContainer.class.getName(); 70 71 protected String configFile = null; 72 private Map servers = new HashMap (); 73 74 77 public void init(String [] args, String configFile) { 78 this.configFile = configFile; 79 } 80 81 private void initJetty() throws ContainerException { 82 SSLUtil.loadJsseProperties(); 84 85 Log log = Log.instance(); 87 log.disableLog(); 88 Log4jSink sink = new Log4jSink(); 89 log.add(sink); 90 sink.setOptions(UtilURL.fromResource("debug.properties").toExternalForm()); 91 try { 92 sink.start(); 93 } catch (Exception e) { 94 Debug.logWarning(e, module); 95 } 96 97 ContainerConfig.Container jc = ContainerConfig.getContainer("jetty-container", configFile); 99 100 Iterator sci = jc.properties.values().iterator(); 102 while (sci.hasNext()) { 103 ContainerConfig.Container.Property prop = (ContainerConfig.Container.Property) sci.next(); 104 servers.put(prop.name, createServer(prop)); 105 } 106 107 Collection componentConfigs = ComponentConfig.getAllComponents(); 109 if (componentConfigs != null) { 110 Iterator components = componentConfigs.iterator(); 111 while (components.hasNext()) { 112 ComponentConfig component = (ComponentConfig) components.next(); 113 Iterator appInfos = component.getWebappInfos().iterator(); 114 while (appInfos.hasNext()) { 115 ComponentConfig.WebappInfo appInfo = (ComponentConfig.WebappInfo) appInfos.next(); 116 List virtualHosts = appInfo.getVirtualHosts(); 117 Map initParameters = appInfo.getInitParameters(); 118 Server server = (Server) servers.get(appInfo.server); 119 if (server == null) { 120 Debug.logWarning("Server with name [" + appInfo.server + "] not found; not mounting [" + appInfo.name + "]", module); 121 } else { 122 try { 123 String location = component.getRootLocation() + appInfo.location; 125 location = location.replace('\\', '/'); 126 if (!location.endsWith("/")) { 127 location = location + "/"; 128 } 129 130 WebApplicationContext ctx = server.addWebApplication(appInfo.mountPoint, location); 132 ctx.setAttribute("_serverId", appInfo.server); 133 134 SessionManager sm = new HashSessionManager(); 136 ctx.getWebApplicationHandler().setSessionManager(sm); 137 138 Iterator vh = virtualHosts.iterator(); 140 while (vh.hasNext()) { 141 ctx.addVirtualHost((String )vh.next()); 142 } 143 144 Iterator ip = initParameters.keySet().iterator(); 146 while (ip.hasNext()) { 147 String paramName = (String ) ip.next(); 148 ctx.setInitParameter(paramName, (String ) initParameters.get(paramName)); 149 } 150 151 } catch (IOException e) { 152 Debug.logError(e, "Problem mounting application [" + appInfo.name + " / " + appInfo.location + "]", module); 153 } 154 } 155 } 156 } 157 } 158 } 159 160 private Server createServer(ContainerConfig.Container.Property serverConfig) throws ContainerException { 161 Server server = new Server(); 162 163 Iterator properties = serverConfig.properties.values().iterator(); 165 while (properties.hasNext()) { 166 ContainerConfig.Container.Property props = 167 (ContainerConfig.Container.Property) properties.next(); 168 169 if ("listener".equals(props.value)) { 170 if ("default".equals(props.getProperty("type").value)) { 171 SocketListener listener = new SocketListener(); 172 setListenerOptions(listener, props); 173 if (props.getProperty("identify-listener") != null) { 174 boolean identifyListener = "true".equalsIgnoreCase(props.getProperty("identify-listener").value); 175 listener.setIdentifyListener(identifyListener); 176 } 177 if (props.getProperty("buffer-size") != null) { 178 int value = 0; 179 try { 180 value = Integer.parseInt(props.getProperty("buffer-size").value); 181 } catch (NumberFormatException e) { 182 value = 0; 183 } 184 if (value > 0) { 185 listener.setBufferSize(value); 186 } 187 } 188 if (props.getProperty("low-resource-persist-time") != null) { 189 int value = 0; 190 try { 191 value = Integer.parseInt(props.getProperty("low-resource-persist-time").value); 192 } catch (NumberFormatException e) { 193 value = 0; 194 } 195 if (value > 0) { 196 listener.setLowResourcePersistTimeMs(value); 197 } 198 } 199 server.addListener(listener); 200 } else if ("sun-jsse".equals(props.getProperty("type").value)) { 201 SunJsseListener listener = new SunJsseListener(); 202 setListenerOptions(listener, props); 203 if (props.getProperty("keystore") != null) { 204 listener.setKeystore(props.getProperty("keystore").value); 205 } 206 if (props.getProperty("password") != null) { 207 listener.setPassword(props.getProperty("password").value); 208 } 209 if (props.getProperty("key-password") != null) { 210 listener.setKeyPassword(props.getProperty("key-password").value); 211 } 212 if (props.getProperty("need-client-auth") != null) { 213 boolean needClientAuth = "true".equalsIgnoreCase(props.getProperty("need-client-auth").value); 214 listener.setNeedClientAuth(needClientAuth); 215 } 216 if (props.getProperty("identify-listener") != null) { 217 boolean identifyListener = "true".equalsIgnoreCase(props.getProperty("identify-listener").value); 218 listener.setIdentifyListener(identifyListener); 219 } 220 if (props.getProperty("buffer-size") != null) { 221 int value = 0; 222 try { 223 value = Integer.parseInt(props.getProperty("buffer-size").value); 224 } catch (NumberFormatException e) { 225 value = 0; 226 } 227 if (value > 0) { 228 listener.setBufferSize(value); 229 } 230 } 231 if (props.getProperty("low-resource-persist-time") != null) { 232 int value = 0; 233 try { 234 value = Integer.parseInt(props.getProperty("low-resource-persist-time").value); 235 } catch (NumberFormatException e) { 236 value = 0; 237 } 238 if (value > 0) { 239 listener.setLowResourcePersistTimeMs(value); 240 } 241 } 242 server.addListener(listener); 243 } else if ("ibm-jsse".equals(props.getProperty("type").value)) { 244 throw new ContainerException("Listener not supported yet [" + props.getProperty("type").value + "]"); 245 } else if ("nio".equals(props.getProperty("type").value)) { 246 throw new ContainerException("Listener not supported yet [" + props.getProperty("type").value + "]"); 247 } else if ("ajp13".equals(props.getProperty("type").value)) { 248 AJP13Listener listener = new AJP13Listener(); 249 setListenerOptions(listener, props); 250 if (props.getProperty("identify-listener") != null) { 251 boolean identifyListener = "true".equalsIgnoreCase(props.getProperty("identify-listener").value); 252 listener.setIdentifyListener(identifyListener); 253 } 254 if (props.getProperty("buffer-size") != null) { 255 int value = 0; 256 try { 257 value = Integer.parseInt(props.getProperty("buffer-size").value); 258 } catch (NumberFormatException e) { 259 value = 0; 260 } 261 if (value > 0) { 262 listener.setBufferSize(value); 263 } 264 } 265 server.addListener(listener); 266 } 267 } else if ("request-log".equals(props.value)) { 268 NCSARequestLog rl = new NCSARequestLog(); 269 270 if (props.getProperty("filename") != null) { 271 rl.setFilename(props.getProperty("filename").value); 272 } 273 274 if (props.getProperty("append") != null) { 275 rl.setAppend("true".equalsIgnoreCase(props.getProperty("append").value)); 276 } 277 278 if (props.getProperty("buffered") != null) { 279 rl.setBuffered("true".equalsIgnoreCase(props.getProperty("buffered").value)); 280 } 281 282 if (props.getProperty("extended") != null) { 283 rl.setExtended("true".equalsIgnoreCase(props.getProperty("extended").value)); 284 } 285 286 if (props.getProperty("timezone") != null) { 287 rl.setLogTimeZone(props.getProperty("timezone").value); 288 } 289 290 if (props.getProperty("date-format") != null) { 291 rl.setLogDateFormat(props.getProperty("date-format").value); 292 } 293 294 if (props.getProperty("retain-days") != null) { 295 int days = 90; 296 try { 297 days = Integer.parseInt(props.getProperty("retain-days").value); 298 } catch (NumberFormatException e) { 299 days = 90; 300 } 301 rl.setRetainDays(days); 302 } 303 server.setRequestLog(rl); 304 } 305 } 306 return server; 307 } 308 309 private void setListenerOptions(ThreadedServer listener, ContainerConfig.Container.Property listenerProps) throws ContainerException { 310 String systemHost = null; 311 if ("default".equals(listenerProps.getProperty("type").value)) { 312 systemHost = System.getProperty(listenerProps.name + ".host"); 313 } 314 if (listenerProps.getProperty("host") != null && systemHost == null) { 315 try { 316 listener.setHost(listenerProps.getProperty("host").value); 317 } catch (UnknownHostException e) { 318 throw new ContainerException(e); 319 } 320 } else { 321 String host = "0.0.0.0"; 322 if (systemHost != null) { 323 host = systemHost; 324 } 325 try { 326 listener.setHost(host); 327 } catch (UnknownHostException e) { 328 throw new ContainerException(e); 329 } 330 } 331 332 String systemPort = null; 333 if ("default".equals(listenerProps.getProperty("type").value)) { 334 systemPort = System.getProperty(listenerProps.name + ".port"); 335 } 336 if (listenerProps.getProperty("port") != null && systemPort == null) { 337 int value = 8080; 338 try { 339 value = Integer.parseInt(listenerProps.getProperty("port").value); 340 } catch (NumberFormatException e) { 341 value = 8080; 342 } 343 if (value == 0) value = 8080; 344 345 listener.setPort(value); 346 } else { 347 int port = 8080; 348 if (systemPort != null) { 349 try { 350 port = Integer.parseInt(systemPort); 351 } catch (NumberFormatException e) { 352 port = 8080; 353 } 354 } 355 listener.setPort(port); 356 } 357 358 if (listenerProps.getProperty("min-threads") != null) { 359 int value = 0; 360 try { 361 value = Integer.parseInt(listenerProps.getProperty("min-threads").value); 362 } catch (NumberFormatException e) { 363 value = 0; 364 } 365 if (value > 0) { 366 listener.setMinThreads(value); 367 } 368 } 369 370 if (listenerProps.getProperty("max-threads") != null) { 371 int value = 0; 372 try { 373 value = Integer.parseInt(listenerProps.getProperty("max-threads").value); 374 } catch (NumberFormatException e) { 375 value = 0; 376 } 377 if (value > 0) { 378 listener.setMaxThreads(value); 379 } 380 } 381 382 if (listenerProps.getProperty("max-idle-time") != null) { 383 int value = 0; 384 try { 385 value = Integer.parseInt(listenerProps.getProperty("max-idle-time").value); 386 } catch (NumberFormatException e) { 387 value = 0; 388 } 389 if (value > 0) { 390 listener.setMaxIdleTimeMs(value); 391 } 392 } 393 394 if (listenerProps.getProperty("linger-time") != null) { 395 int value = 0; 396 try { 397 value = Integer.parseInt(listenerProps.getProperty("linger-time").value); 398 } catch (NumberFormatException e) { 399 value = 0; 400 } 401 if (value > 0) { 402 listener.setLingerTimeSecs(value); 403 } 404 } 405 } 406 407 410 public boolean start() throws ContainerException { 411 this.initJetty(); 413 if (servers != null) { 414 Iterator i = servers.values().iterator(); 415 while (i.hasNext()) { 416 Server server = (Server) i.next(); 417 try { 418 server.start(); 419 } catch (MultiException e) { 420 Debug.logError(e, "Jetty Server Multi-Exception", module); 421 throw new ContainerException(e); 422 } 423 } 424 } 425 return true; 426 } 427 428 431 public void stop() throws ContainerException { 432 if (servers != null) { 433 Iterator i = servers.values().iterator(); 434 while(i.hasNext()) { 435 Server server = (Server) i.next(); 436 try { 437 server.stop(); 438 } catch (InterruptedException e) { 439 Debug.logWarning(e, module); 440 } 441 } 442 } 443 } 444 } 445 446 class Log4jSink implements LogSink { 448 449 private String _options; 450 private transient boolean _started; 451 452 public void setOptions(String filename) { 453 _options=filename; 454 } 455 456 public String getOptions() { 457 return _options; 458 } 459 460 public void start() throws Exception { 461 _started=true; 462 } 463 464 public void stop() { 465 _started=false; 466 } 467 468 public boolean isStarted() { 469 return _started; 470 } 471 472 public void log(String tag, Object msg, Frame frame, long time) { 473 String method = frame.getMethod(); 474 int lb = method.indexOf('('); 475 int ld = (lb > 0) ? method.lastIndexOf('.', lb) : method.lastIndexOf('.'); 476 if (ld < 0) ld = lb; 477 String class_name = (ld > 0) ? method.substring(0,ld) : method; 478 479 Logger log = Logger.getLogger(class_name); 480 481 Priority priority = Priority.INFO; 482 483 if (Log.DEBUG.equals(tag)) { 484 priority = Priority.DEBUG; 485 } else if (Log.WARN.equals(tag) || Log.ASSERT.equals(tag)) { 486 priority = Priority.ERROR; 487 } else if (Log.FAIL.equals(tag)) { 488 priority = Priority.FATAL; 489 } 490 491 if (!log.isEnabledFor(priority)) { 492 return; 493 } 494 495 log.log(Log4jSink.class.getName(), priority, "" + msg, null); 496 } 497 498 public synchronized void log(String s) { 499 Logger.getRootLogger().log("jetty.log4jSink", Priority.INFO, s, null); 500 } 501 } 502 | Popular Tags |