1 26 27 package org.objectweb.jonas.web.jetty50; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.net.URL ; 32 import java.net.URLClassLoader ; 33 import java.util.Map ; 34 import java.util.StringTokenizer ; 35 import java.util.Vector ; 36 37 import javax.management.MalformedObjectNameException ; 38 import javax.management.ObjectName ; 39 import javax.naming.Context ; 40 import javax.naming.NamingException ; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 import org.objectweb.jonas.jmx.JmxService; 45 import org.objectweb.jonas.jmx.JonasObjectName; 46 import org.objectweb.jonas.service.ServiceException; 47 import org.objectweb.jonas.service.ServiceManager; 48 import org.objectweb.jonas.web.AbsJWebContainerServiceImpl; 49 import org.objectweb.jonas.web.JWebContainerServiceException; 50 51 import org.mortbay.http.HttpContext; 52 import org.mortbay.http.HttpListener; 53 import org.mortbay.jetty.Server; 54 import org.mortbay.jetty.servlet.WebApplicationContext; 55 56 62 public class JettyJWebContainerServiceImpl extends AbsJWebContainerServiceImpl { 63 64 67 private static final String JETTY_DEFAULT_WEB_XML_FILE = AbsJWebContainerServiceImpl.JONAS_BASE + File.separator + "conf" + File.separator + "jetty5-webdefault.xml"; 68 69 72 private static String config = null; 73 74 77 private static final String JETTY_CONFIG = "config"; 78 79 82 private Server jettyServer = null; 83 84 89 protected void doInit(javax.naming.Context ctx) throws ServiceException { 90 91 super.doInit(ctx); 92 93 String strJettyHome = System.getProperty("jetty.home"); 94 if (strJettyHome != null) { 95 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 96 getLogger().log(BasicLevel.DEBUG, ""); 97 } 98 99 try { 100 config = (String ) ctx.lookup(JETTY_CONFIG); 101 } catch (NamingException neExc) { 102 String jonasbase = System.getProperties().getProperty("jonas.base"); 104 config = jonasbase + "/conf/jetty5.xml"; 105 } 106 getLogger().log(BasicLevel.LEVEL_DEBUG, "using configuration file " + config); 107 108 jettyServer = new Server(); 109 } 110 111 } 112 113 117 public void doStart() throws ServiceException { 118 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 119 getLogger().log(BasicLevel.DEBUG, ""); 120 } 121 122 if (jettyServer != null) { 123 JmxService srvcJMX = null; 124 125 if (config != null) { 127 try { 128 jettyServer.configure(config); 129 jettyServer.start(); 130 } catch (Exception eExc) { 131 getLogger().log(BasicLevel.LEVEL_ERROR, 132 "error has occured while starting Jetty server using configuration file " + config, eExc); 133 } 134 } 135 136 ServiceManager sm = null; 137 try { 138 sm = ServiceManager.getInstance(); 139 } catch (Exception e) { 140 String err = "Cannot get ServiceManager instance."; 141 getLogger().log(BasicLevel.ERROR, err); 142 throw new ServiceException(err, e); 143 } 144 145 try { 146 srvcJMX = (JmxService) sm.getJmxService(); 147 if (srvcJMX != null) { 148 Object obj; 150 ObjectName objname; 151 obj = new JettyJonasServerMBean(jettyServer); 156 objname = JonasObjectName.wwwService(); 157 srvcJMX.getJmxServer().registerMBean(obj, objname); 158 } 159 } catch (ServiceException seExc) { 160 getLogger().log( 162 BasicLevel.LEVEL_DEBUG, 163 "cannot start Jetty server using configuration file " + config 164 + " using JMX. Will start without JMX.", seExc); 165 } catch (Exception eExc) { 166 getLogger().log(BasicLevel.LEVEL_ERROR, "cannot start Jetty server using configuration file " + config, 167 eExc); 168 throw new ServiceException("Cannot start Jetty server using configuration file " + config, eExc); 169 } 170 171 super.doStart(); 173 } else { 174 throw new ServiceException("Cannot start Jetty server."); 175 } 176 } 177 178 182 protected void doStop() throws ServiceException { 183 super.doStop(); 185 186 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 188 getLogger().log(BasicLevel.DEBUG, ""); 189 } 190 if (isStarted()) { 191 JmxService srvcJMX = null; 192 193 ServiceManager sm = null; 194 try { 195 sm = ServiceManager.getInstance(); 196 } catch (Exception e) { 197 String err = "Cannot get ServiceManager instance."; 198 getLogger().log(BasicLevel.ERROR, err); 199 throw new ServiceException(err, e); 200 } 201 202 try { 203 srvcJMX = (JmxService) sm.getJmxService(); 204 if (srvcJMX != null) { 205 ObjectName objname; 206 207 objname = new ObjectName ("jonas:type=service,name=jetty"); 208 srvcJMX.getJmxServer().unregisterMBean(objname); 209 jettyServer = null; 210 objname = new ObjectName ("jonas:type=jetty,name=jettylog"); 211 srvcJMX.getJmxServer().unregisterMBean(objname); 212 objname = new ObjectName ("jonas:type=jetty,name=jettycode"); 213 srvcJMX.getJmxServer().unregisterMBean(objname); 214 } 215 } catch (ServiceException seExc) { 216 getLogger().log(BasicLevel.LEVEL_ERROR, "JMX Service not available", seExc); 217 } catch (Exception eExc) { 218 getLogger().log(BasicLevel.LEVEL_ERROR, "Cannot stop Jetty server", eExc); 219 throw new ServiceException("Cannot stop Jetty server", eExc); 220 } 221 222 if (jettyServer != null) { 223 try { 224 jettyServer.stop(); 225 jettyServer.destroy(); 226 jettyServer = null; 227 } catch (Exception eExc) { 228 getLogger().log(BasicLevel.LEVEL_ERROR, 229 "error has occured while stopping Jetty server using configuration file " + config, eExc); 230 } 231 } 232 } 233 } 234 235 243 protected void doRegisterWar(Context ctx) throws JWebContainerServiceException { 244 URL unpackedWarURL = null; 255 String contextRoot = null; 256 boolean java2DelegationModel = true; 257 try { 258 unpackedWarURL = (URL ) ctx.lookup("unpackedWarURL"); 259 contextRoot = (String ) ctx.lookup("contextRoot"); 260 Boolean bool = (Boolean ) ctx.lookup("java2DelegationModel"); 261 java2DelegationModel = bool.booleanValue(); 262 } catch (NamingException e) { 263 String err = "Error while getting parameter from context param "; 264 getLogger().log(BasicLevel.ERROR, err + e.getMessage()); 265 throw new JWebContainerServiceException(err, e); 266 } 267 268 ClassLoader webClassLoader = null; 269 try { 270 webClassLoader = (ClassLoader ) ctx.lookup("parentCL"); 271 } catch (NamingException e) { 272 String err = "error while getting parameter from context param "; 273 getLogger().log(BasicLevel.ERROR, err + e.getMessage()); 274 throw new JWebContainerServiceException(err, e); 275 } 276 277 String hostName = null; 278 try { 279 hostName = (String ) ctx.lookup("hostName"); 280 } catch (NamingException e) { 281 hostName = ""; 282 } 283 284 String earAppName = null; 285 try { 286 earAppName = (String ) ctx.lookup("earAppName"); 287 } catch (NamingException e) { 288 earAppName = null; 290 } 291 292 299 if (contextRoot.equals("/")) { 300 contextRoot = ""; 301 } else if (contextRoot.equalsIgnoreCase("ROOT")) { 302 contextRoot = ""; 304 } 305 306 File fWar = new File (unpackedWarURL.getFile()); 308 String fileName = fWar.getAbsolutePath(); 309 310 if (jettyServer != null) { 311 try { 312 WebApplicationContext contextWebApp; 313 314 if ((hostName == null) || (hostName.length() == 0)) { 315 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 317 getLogger().log(BasicLevel.DEBUG, 318 "Jetty server installing web app " + fileName + " and context " + contextRoot); 319 } 320 contextWebApp = jettyServer.addWebApplication(contextRoot, fileName); 321 } else { 322 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 324 getLogger().log( 325 BasicLevel.DEBUG, 326 "Jetty server installing web app " + fileName + " on host " + hostName + " and context " 327 + contextRoot); 328 } 329 contextWebApp = jettyServer.addWebApplication(hostName, contextRoot, fileName); 330 331 } 332 333 contextWebApp.setAttribute("J2EEDomainName", getDomainName()); 334 contextWebApp.setAttribute("J2EEServerName", getJonasServerName()); 335 contextWebApp.setAttribute("J2EEApplicationName", earAppName); 336 337 File webDefaults = new File (JETTY_DEFAULT_WEB_XML_FILE); 339 if (webDefaults.exists()) { 340 contextWebApp.setDefaultsDescriptor(webDefaults.toURL().toExternalForm()); 341 } else { 342 getLogger().log(BasicLevel.WARN, "The file '" + JETTY_DEFAULT_WEB_XML_FILE + "' is not present. Check that your JONAS_BASE is up-to-date."); 343 } 344 345 contextWebApp.setAttribute("org.apache.catalina.jsp_classpath", getJOnASClassPath(webClassLoader)); 347 348 contextWebApp.setParentClassLoader(webClassLoader); 350 351 contextWebApp.setClassLoaderJava2Compliant(java2DelegationModel); 353 354 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 355 getLogger().log(BasicLevel.DEBUG, 356 "Webapp class loader java 2 delegation model set to " + java2DelegationModel); 357 358 getLogger().log(BasicLevel.DEBUG, "Jetty server starting web app " + fileName); 359 } 360 contextWebApp.start(); 361 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 362 getLogger().log(BasicLevel.DEBUG, "Jetty server is running web app " + fileName); 363 } 364 365 } catch (IOException ioeExc) { 366 String err = "Cannot install this web application " + ioeExc; 367 getLogger().log(BasicLevel.ERROR, err); 368 throw new JWebContainerServiceException(err, ioeExc); 369 } catch (Exception eExc) { 370 String err = "Cannot start this web application " + eExc; 371 getLogger().log(BasicLevel.ERROR, err); 372 throw new JWebContainerServiceException(err, eExc); 373 } 374 } else { 375 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 376 getLogger().log(BasicLevel.DEBUG, "No Jetty server to install web app " + fileName); 377 } 378 } 379 380 try { 383 ctx.rebind("WebModule", getDummyJSR77ObjectName(hostName, contextRoot, earAppName)); 384 } catch (Exception e) { 385 String err = "Cannot rebind WebModule ObjectName in Context"; 388 getLogger().log(BasicLevel.ERROR, err, e); 389 throw new JWebContainerServiceException(err, e); 390 } 391 392 } 393 394 402 private ObjectName getDummyJSR77ObjectName(String hostName, String contextRoot, String earAppName) throws MalformedObjectNameException { 403 return ObjectName.getInstance(getDomainName() + ":j2eeType=WebModule,name=" + "/" + contextRoot + ",J2EEApplication=" + earAppName + ",J2EEServer=" + getJonasServerName()); 405 } 406 407 413 public String getJOnASClassPath(ClassLoader webClassLoader) { 414 415 StringBuffer classpath = new StringBuffer (); 416 int n = 0; 417 while (webClassLoader != null) { 418 if (!(webClassLoader instanceof URLClassLoader )) { 419 break; 420 } 421 URL [] repositories = ((URLClassLoader ) webClassLoader).getURLs(); 422 for (int i = 0; i < repositories.length; i++) { 423 String repository = repositories[i].toString(); 424 if (repository.startsWith("file://")) { 425 repository = repository.substring("file://".length()); 426 } else if (repository.startsWith("file:")) { 427 repository = repository.substring("file:".length()); 428 } else { 429 continue; 430 } 431 if (repository == null) { 432 continue; 433 } 434 if (n > 0) { 435 classpath.append(File.pathSeparator); 436 } 437 classpath.append(repository); 438 n++; 439 } 440 webClassLoader = webClassLoader.getParent(); 441 } 442 443 return classpath.toString(); 444 } 445 446 452 protected void doUnRegisterWar(Context ctx) throws JWebContainerServiceException { 453 String contextRoot = null; 457 try { 458 contextRoot = (String ) ctx.lookup("contextRoot"); 459 } catch (NamingException e) { 460 String err = "Error while getting parameter from context param "; 461 getLogger().log(BasicLevel.ERROR, err + e.getMessage()); 462 throw new JWebContainerServiceException(err, e); 463 } 464 465 String hostName = null; 466 try { 467 hostName = (String ) ctx.lookup("hostName"); 468 } catch (NamingException e) { 469 hostName = ""; 470 } 471 475 if (contextRoot.equals("/")) { 476 contextRoot = ""; 477 } else if (contextRoot.equalsIgnoreCase("ROOT")) { 478 contextRoot = ""; 480 } 481 482 if (jettyServer != null) { 483 HttpContext contextWebApp; 484 485 if ((hostName == null) || (hostName.length() == 0)) { 486 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 488 getLogger().log(BasicLevel.DEBUG, "Jetty server looking upweb app " + " from context " + contextRoot); 489 } 490 contextWebApp = jettyServer.getContext(contextRoot); 491 } else { 492 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 494 getLogger().log(BasicLevel.DEBUG, 495 "Jetty server looking up web app " + " on host " + hostName + " and context " + contextRoot); 496 } 497 contextWebApp = jettyServer.getContext(hostName, contextRoot); 498 } 499 500 if (contextWebApp != null) { 501 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 502 getLogger().log(BasicLevel.DEBUG, 503 "Jetty server found and is stopping web app at context " + contextRoot); 504 } 505 try { 507 contextWebApp.stop(true); 508 } catch (InterruptedException ieExc) { 509 getLogger().log(BasicLevel.LEVEL_DEBUG, 510 "Jetty server encoutered exception while stopping web application ", ieExc); 511 } 512 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 513 getLogger().log(BasicLevel.DEBUG, 514 "Jetty server stopped and is removing web app at context " + contextRoot); 515 } 516 517 jettyServer.removeContext(contextWebApp); 518 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 519 getLogger().log(BasicLevel.DEBUG, 520 "Jetty server removed and is destroying web app at context " + contextRoot); 521 } 522 contextWebApp.destroy(); 523 524 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 525 getLogger().log(BasicLevel.DEBUG, "Jetty server unloaded web app at context " + contextRoot); 526 } 527 528 } else { 529 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 530 getLogger().log(BasicLevel.DEBUG, "Jetty server didn't find web app at context " + contextRoot); 531 } 532 } 533 534 } else { 535 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 536 getLogger().log(BasicLevel.DEBUG, "No Jetty server to install web app at context " + contextRoot); 537 } 538 } 539 } 540 541 544 protected void updateServerInfos() { 545 String infos = org.mortbay.http.Version.getImplVersion(); 546 547 StringTokenizer st = new StringTokenizer (infos, "/"); 548 if (st.countTokens() != 2) { 549 setServerName(infos); 550 setServerVersion(""); 551 } else { 552 setServerName(st.nextToken()); 553 setServerVersion(st.nextToken()); 554 } 555 } 556 557 563 public String getDefaultHost() throws JWebContainerServiceException { 564 Map hosts = jettyServer.getHostMap(); 565 if (hosts.isEmpty()) { 567 String err = "Cannot determine default host : Jetty server has no host!"; 568 throw new JWebContainerServiceException(err); 569 } 570 571 String vHost = (String ) hosts.keySet().iterator().next(); 572 if (vHost == null) { 573 vHost = "localhost"; 574 } 575 576 if (hosts.size() > 1) { 577 if (getLogger().isLoggable(BasicLevel.WARN)) { 578 getLogger().log(BasicLevel.WARN, "More than 1 host found, using the first one : " + vHost); 579 } 580 } 581 return vHost; 582 } 583 584 591 public String getDefaultHttpPort() throws JWebContainerServiceException { 592 return String.valueOf(getFirstListenerFromScheme("http").getPort()); 593 } 594 595 602 public String getDefaultHttpsPort() throws JWebContainerServiceException { 603 return String.valueOf(getFirstListenerFromScheme("https").getPort()); 604 } 605 606 610 private HttpListener getFirstListenerFromScheme(String myScheme) { 611 HttpListener[] listeners = jettyServer.getListeners(); 612 Vector http = new Vector (); 613 for (int i = 0; i < listeners.length; i++) { 614 String scheme = listeners[i].getDefaultScheme(); 615 if (scheme.equalsIgnoreCase(myScheme)) { 616 http.add(listeners[i]); 617 } 618 } 619 if (http.isEmpty()) { 620 String err = "Cannot determine default '" + myScheme + "' port :" + " Jetty server has 0 '" + myScheme + "' Listener"; 621 throw new JWebContainerServiceException(err); 622 } 623 624 HttpListener hl = (HttpListener) http.get(0); 625 if (http.size() > 1) { 627 if (getLogger().isLoggable(BasicLevel.WARN)) { 628 getLogger().log(BasicLevel.WARN, "Found multiple Listener for scheme '" + myScheme + "'" 629 + ", using first by default! (port:" + hl.getPort() + ")"); 630 } 631 } 632 633 return hl; 634 } 635 636 } 637 638 | Popular Tags |