1 22 23 package org.jboss.system.server.profileservice; 24 25 import java.io.File ; 26 import java.net.URL ; 27 import java.util.Date ; 28 import java.util.Properties ; 29 30 import javax.management.Notification ; 31 import javax.management.NotificationBroadcasterSupport ; 32 import javax.management.NotificationEmitter ; 33 34 import org.jboss.Version; 35 import org.jboss.deployers.spi.deployment.MainDeployer; 36 import org.jboss.kernel.Kernel; 37 import org.jboss.kernel.plugins.deployment.BasicKernelDeployer; 38 import org.jboss.kernel.spi.event.KernelEvent; 39 import org.jboss.logging.Logger; 40 import org.jboss.net.protocol.URLStreamHandlerFactory; 41 import org.jboss.system.server.Server; 42 import org.jboss.system.server.ServerConfig; 43 import org.jboss.system.server.BaseServerConfig; 44 import org.jboss.util.StopWatch; 45 46 47 55 public class ServerImpl extends NotificationBroadcasterSupport 56 implements Server, NotificationEmitter 57 { 58 59 private Logger log; 60 61 62 private final Version version = Version.getInstance(); 63 64 65 private final Package jbossPackage = Package.getPackage("org.jboss"); 66 67 68 private ProfileServiceBootstrap bootstrap; 69 70 private String deployerBeansPrefix; 71 72 73 private BaseServerConfig config; 74 75 76 private Date startDate; 77 78 79 private boolean started; 80 81 private boolean isInStart; 82 83 private boolean isInShutdown; 84 85 86 private ShutdownHook shutdownHook; 87 88 89 private LifeThread lifeThread; 90 91 94 public ServerImpl() 95 { 96 } 97 98 106 public void init(final Properties props) 107 throws IllegalStateException , Exception 108 { 109 if (props == null) 110 throw new IllegalArgumentException ("props is null"); 111 if (config != null) 112 throw new IllegalStateException ("already initialized"); 113 114 ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 115 116 try 117 { 118 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 119 doInit(props); 120 } 121 finally 122 { 123 Thread.currentThread().setContextClassLoader(oldCL); 124 } 125 } 126 127 132 private void doInit(final Properties props) throws Exception 133 { 134 this.config = new BaseServerConfig(props); 136 this.deployerBeansPrefix = props.getProperty("jboss.server.deployerBeansPrefix"); 137 138 boolean overrideTmpDir = Boolean.getBoolean("jboss.server.temp.dir.overrideJavaTmpDir"); 140 if( overrideTmpDir ) 141 { 142 File serverTmpDir = config.getServerTempDir(); 143 System.setProperty("java.io.tmpdir", serverTmpDir.getCanonicalPath()); 144 } 145 146 150 config.getServerLogDir(); 151 log = Logger.getLogger(getClass()); 152 153 initURLHandlers(); 155 config.initURLs(); 156 157 log.info("Starting JBoss (Microcontainer)..."); 158 159 if (jbossPackage != null) 160 { 161 log.info("Release ID: " + 163 jbossPackage.getImplementationTitle() + " " + 164 jbossPackage.getImplementationVersion()); 165 } 166 else 167 { 168 log.warn("could not get package info to display release, either the " + 169 "jar manifest in jboss-system.jar has been mangled or you're " + 170 "running unit tests from ant outside of JBoss itself."); 171 } 172 173 log.debug("Using config: " + config); 174 175 log.debug("Server type: " + getClass()); 177 178 log.info("Home Dir: " + config.getHomeDir()); 180 log.info("Home URL: " + config.getHomeURL()); 181 log.info("Library URL: " + config.getLibraryURL()); 182 log.info("Patch URL: " + config.getPatchURL()); 183 log.info("Server Name: " + config.getServerName()); 184 log.info("Server Home Dir: " + config.getServerHomeDir()); 185 log.info("Server Home URL: " + config.getServerHomeURL()); 186 log.info("Server Data Dir: " + config.getServerDataDir()); 187 log.info("Server Temp Dir: " + config.getServerTempDir()); 188 log.info("Server Config URL: " + config.getServerConfigURL()); 189 log.info("Server Library URL: " + config.getServerLibraryURL()); 190 log.info("Root Deployment Filename: " + config.getRootDeploymentFilename()); 191 } 192 193 199 private void initURLHandlers() 200 { 201 if (config.getRequireJBossURLStreamHandlerFactory()) 202 { 203 internalInitURLHandlers(); 204 } else 206 { 207 try 208 { 209 internalInitURLHandlers(); 210 } 211 catch (SecurityException e) 212 { 213 log.warn("You do not have permissions to set URLStreamHandlerFactory", e); 214 } catch (Error e) 216 { 217 log.warn("URLStreamHandlerFactory already set", e); 218 } } } 221 222 227 private void internalInitURLHandlers() 228 { 229 try 230 { 231 URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory()); 233 234 URLStreamHandlerFactory.preload(); 236 } 237 catch (Error error) 238 { log.warn("Caught Throwable Error, this probably means " + 241 "we've already set the URLStreamHAndlerFactory before"); 242 } 243 244 String handlerPkgs = System.getProperty("java.protocol.handler.pkgs"); 246 if (handlerPkgs != null) 247 { 248 handlerPkgs += "|org.jboss.net.protocol"; 249 } 250 else 251 { 252 handlerPkgs = "org.jboss.net.protocol"; 253 } 254 System.setProperty("java.protocol.handler.pkgs", handlerPkgs); 255 } 256 257 265 public ServerConfig getConfig() throws IllegalStateException 266 { 267 if (config == null) 268 throw new IllegalStateException ("not initialized"); 269 270 return config; 271 } 272 273 278 public boolean isStarted() 279 { 280 return started; 281 } 282 283 288 public boolean isInShutdown() 289 { 290 return isInShutdown; 291 } 292 293 299 public void start() throws IllegalStateException , Exception 300 { 301 synchronized( this ) 302 { 303 if( isInStart == false ) 304 { 305 isInStart = true; 306 } 307 else 308 { 309 log.debug("Already in start, ignoring duplicate start"); 310 return; 311 } 312 } 313 314 getConfig(); 316 317 if (started) 319 throw new IllegalStateException ("already started"); 320 321 ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 322 323 try 324 { 325 ClassLoader myCL = getClass().getClassLoader(); 326 339 Thread.currentThread().setContextClassLoader(myCL); 340 doStart(); 341 } 342 catch (Throwable t) 343 { 344 log.debug("Failed to start", t); 345 346 if (t instanceof Exception ) 347 throw (Exception )t; 348 if (t instanceof Error ) 349 throw (Error )t; 350 351 throw new org.jboss.util.UnexpectedThrowable(t); 352 } 353 finally 354 { 355 Thread.currentThread().setContextClassLoader(oldCL); 356 isInStart = false; 357 } 358 } 359 360 366 private void doStart() throws Throwable 367 { 368 StopWatch watch = new StopWatch(true); 370 371 startDate = new Date (); 373 374 bootstrap = new ProfileServiceBootstrap(config.getServerName(), this); 375 if( deployerBeansPrefix != null ) 376 { 377 bootstrap.setDeployerBeansPrefix(deployerBeansPrefix); 378 log.info("Starting Microcontainer, deployerBeansPrefix="+deployerBeansPrefix); 379 } 380 else 381 { 382 URL configURL = config.getServerConfigURL(); 383 URL bootstrapURL = new URL (configURL, ProfileServiceBootstrap.BOOTSTRAP_XML_NAME); 384 log.info("Starting Microcontainer, bootstrapURL="+bootstrapURL); 385 bootstrap.setBootstrapURL(bootstrapURL); 386 } 387 388 shutdownHook = new ShutdownHook(); 390 shutdownHook.setDaemon(true); 391 try 392 { 393 Runtime.getRuntime().addShutdownHook(shutdownHook); 394 log.debug("Shutdown hook added"); 395 } 396 catch (Exception e) 397 { 398 log.warn("Failed to add shutdown hook; ignoring", e); 399 } 400 401 bootstrap.run(); 402 403 lifeThread = new LifeThread(); 404 lifeThread.start(); 405 406 started = true; 407 408 KernelEvent startEvent = bootstrap.createEvent(START_NOTIFICATION_TYPE, new Long (watch.getLapTime())); 410 bootstrap.fireKernelEvent(startEvent); 411 Notification msg = new Notification (START_NOTIFICATION_TYPE, this, 1); 413 msg.setUserData(new Long (watch.getLapTime())); 414 sendNotification(msg); 415 416 watch.stop(); 417 418 if (jbossPackage != null) 419 { 420 log.info("JBoss (Microcontainer) [" + jbossPackage.getImplementationVersion() + 422 "] Started in " + watch); 423 } 424 else 425 { 426 log.warn("could not get package info to display release, either the " + 427 "jar manifest in run.jar has been mangled or you're " + 428 "running unit tests from ant outside of JBoss itself."); 429 } 430 } 431 432 440 public void shutdown() throws IllegalStateException 441 { 442 if (!started) 443 throw new IllegalStateException ("not started"); 444 445 final ServerImpl server = this; 446 447 log.debug("Shutting down"); 448 449 isInShutdown = true; 450 boolean exitOnShutdown = config.getExitOnShutdown(); 451 boolean blockingShutdown = config.getBlockingShutdown(); 452 453 log.debug("exitOnShutdown: " + exitOnShutdown); 454 log.debug("blockingShutdown: " + blockingShutdown); 455 456 lifeThread.interrupt(); 457 458 if (exitOnShutdown) 459 { 460 server.exit(0); 461 } 462 else if (blockingShutdown) 463 { 464 shutdownHook.shutdown(); 465 } else 467 { 468 new Thread () 471 { 472 public void run() 473 { 474 shutdownHook.shutdown(); 477 } 478 }.start(); 479 } 480 } 481 482 487 public void exit(final int exitcode) 488 { 489 new Thread () 492 { 493 public void run() 494 { 495 log.info("Shutting down the JVM now!"); 496 Runtime.getRuntime().exit(exitcode); 497 } 498 }.start(); 499 } 500 501 505 public void exit() 506 { 507 exit(1); 508 } 509 510 515 public void halt(final int exitcode) 516 { 517 KernelEvent stopEvent = bootstrap.createEvent(STOP_NOTIFICATION_TYPE, null); 519 bootstrap.fireKernelEvent(stopEvent); 520 521 new Thread () 524 { 525 public void run() 526 { 527 System.err.println("Halting the system now!"); 528 Runtime.getRuntime().halt(exitcode); 529 } 530 }.start(); 531 } 532 533 537 public void halt() 538 { 539 halt(1); 540 } 541 542 543 547 552 private void logMemoryUsage(final Runtime rt) 553 { 554 log.info("Total/free memory: " + rt.totalMemory() + "/" + rt.freeMemory()); 555 } 556 557 560 public void runGarbageCollector() 561 { 562 Runtime rt = Runtime.getRuntime(); 563 564 logMemoryUsage(rt); 565 rt.gc(); 566 log.info("Hinted to the JVM to run garbage collection"); 567 logMemoryUsage(rt); 568 } 569 570 573 public void runFinalization() 574 { 575 Runtime.getRuntime().runFinalization(); 576 log.info("Hinted to the JVM to run any pending object finalizations"); 577 } 578 579 584 public void traceMethodCalls(final Boolean flag) 585 { 586 Runtime.getRuntime().traceMethodCalls(flag.booleanValue()); 587 } 588 589 594 public void traceInstructions(final Boolean flag) 595 { 596 Runtime.getRuntime().traceInstructions(flag.booleanValue()); 597 } 598 599 600 604 public Date getStartDate() 605 { 606 return startDate; 607 } 608 609 public String getVersion() 610 { 611 return version.toString(); 612 } 613 614 public String getVersionName() 615 { 616 return version.getName(); 617 } 618 619 public String getBuildNumber() 620 { 621 return version.getBuildNumber(); 622 } 623 624 public String getBuildJVM() 625 { 626 return version.getBuildJVM(); 627 } 628 629 public String getBuildOS() 630 { 631 return version.getBuildOS(); 632 } 633 634 public String getBuildID() 635 { 636 return version.getBuildID(); 637 } 638 639 643 public String getBuildDate() 644 { 645 return version.getBuildDate(); 646 } 647 648 652 public Kernel getKernel() 653 { 654 return bootstrap.getKernel(); 655 } 656 657 660 private class LifeThread 661 extends Thread 662 { 663 Object lock = new Object (); 664 LifeThread() 665 { 666 super("JBossLifeThread"); 667 } 668 public void run() 669 { 670 synchronized (lock) 671 { 672 try 673 { 674 lock.wait(); 675 } 676 catch (InterruptedException ignore) 677 { 678 } 679 } 680 log.info("LifeThread.run exits!"); 681 } 682 } 683 684 688 private class ShutdownHook 689 extends Thread 690 { 691 private boolean forceHalt = true; 692 693 public ShutdownHook() 694 { 695 super("JBoss Shutdown Hook"); 696 697 String value = SecurityActions.getSystemProperty("jboss.shutdown.forceHalt", null); 698 if (value != null) 699 { 700 forceHalt = new Boolean (value).booleanValue(); 701 } 702 } 703 704 public void run() 705 { 706 shutdown(); 707 if (forceHalt) 708 { 709 System.out.println("Halting VM"); 710 Runtime.getRuntime().halt(0); 711 } 712 } 713 714 public void shutdown() 715 { 716 log.info("JBoss SHUTDOWN"); 718 Notification msg = new Notification (Server.STOP_NOTIFICATION_TYPE, this, 2); 719 sendNotification(msg); 720 log.info("Undeploying all packages"); 721 shutdownDeployments(); 722 723 log.debug("Shutting down Microcontainer"); 724 System.out.println("Shutting down Microcontainer"); 725 726 shutdownKernel(); 728 729 log.info("Shutdown complete"); 730 System.out.println("Shutdown complete"); 731 } 732 733 protected void shutdownDeployments() 734 { 735 try 736 { 737 MainDeployer deployer = bootstrap.getMainDeployer(); 738 if (deployer != null) 739 deployer.shutdown(); 740 } 741 catch (Exception e) 742 { 743 log.error("Failed to shutdown deployer", e); 744 } 745 } 746 747 751 protected void shutdownKernel() 752 { 753 try 754 { 755 BasicKernelDeployer deployer = bootstrap.getKernelDeployer(); 756 if (deployer != null) 757 deployer.shutdown(); 758 } 759 catch (Exception e) 760 { 761 log.error("Failed to shutdown services", e); 762 } 763 } 764 765 } 766 } 767 | Popular Tags |