1 16 22 package org.jboss.web.tomcat.tc5; 23 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyChangeSupport ; 26 import javax.management.MBeanRegistration ; 27 import javax.management.MBeanServer ; 28 import javax.management.ObjectName ; 29 30 import org.apache.catalina.Container; 31 import org.apache.catalina.Engine; 32 import org.apache.catalina.Lifecycle; 33 import org.apache.catalina.LifecycleException; 34 import org.apache.catalina.LifecycleListener; 35 import org.apache.catalina.Server; 36 import org.apache.catalina.Service; 37 import org.apache.catalina.ServerFactory; 38 import org.apache.catalina.connector.Connector; 39 import org.apache.catalina.core.Constants; 40 import org.apache.catalina.core.ContainerBase; 41 import org.apache.catalina.util.LifecycleSupport; 42 import org.apache.catalina.util.StringManager; 43 import org.apache.commons.modeler.Registry; 44 import org.jboss.logging.Logger; 45 46 47 60 public class StandardService 61 implements Lifecycle, Service, MBeanRegistration 62 { 63 private static Logger log = Logger.getLogger(StandardService.class); 64 65 67 68 71 private Connector connectors[] = new Connector[0]; 72 73 74 77 private Container container = null; 78 79 80 83 private int debug = 0; 84 85 86 89 private static final String info = 90 "org.jboss.web.tomcat.tc5.StandardService/1.0"; 91 92 93 96 private boolean initialized = false; 97 98 99 102 private String name = null; 103 104 105 108 private LifecycleSupport lifecycle = new LifecycleSupport(this); 109 110 111 114 private static final StringManager sm = 115 StringManager.getManager(Constants.Package); 116 117 120 private Server server = null; 121 122 125 private boolean started = false; 126 127 128 131 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 132 133 134 136 137 141 public Container getContainer() 142 { 143 144 return (this.container); 145 146 } 147 148 149 155 public void setContainer(Container container) 156 { 157 158 Container oldContainer = this.container; 159 if ((oldContainer != null) && (oldContainer instanceof Engine)) 160 ((Engine) oldContainer).setService(null); 161 this.container = container; 162 if ((this.container != null) && (this.container instanceof Engine)) 163 ((Engine) this.container).setService(this); 164 if (started && (this.container != null) && 165 (this.container instanceof Lifecycle)) 166 { 167 try 168 { 169 ((Lifecycle) this.container).start(); 170 } 171 catch (LifecycleException e) 172 { 173 ; 174 } 175 } 176 synchronized (connectors) 177 { 178 for (int i = 0; i < connectors.length; i++) 179 connectors[i].setContainer(this.container); 180 } 181 if (started && (oldContainer != null) && 182 (oldContainer instanceof Lifecycle)) 183 { 184 try 185 { 186 ((Lifecycle) oldContainer).stop(); 187 } 188 catch (LifecycleException e) 189 { 190 ; 191 } 192 } 193 194 support.firePropertyChange("container", oldContainer, this.container); 196 197 } 198 199 public ObjectName getContainerName() 200 { 201 if (container instanceof ContainerBase) 202 { 203 return ((ContainerBase) container).getJmxName(); 204 } 205 return null; 206 } 207 208 209 212 public int getDebug() 213 { 214 215 return (this.debug); 216 217 } 218 219 220 225 public void setDebug(int debug) 226 { 227 228 this.debug = debug; 229 230 } 231 232 233 238 public String getInfo() 239 { 240 241 return (info); 242 243 } 244 245 246 249 public String getName() 250 { 251 252 return (this.name); 253 254 } 255 256 257 262 public void setName(String name) 263 { 264 265 this.name = name; 266 267 } 268 269 270 273 public Server getServer() 274 { 275 276 return (this.server); 277 278 } 279 280 281 286 public void setServer(Server server) 287 { 288 289 this.server = server; 290 291 } 292 293 294 296 297 303 public void addConnector(Connector connector) 304 { 305 306 synchronized (connectors) 307 { 308 connector.setContainer(this.container); 309 connector.setService(this); 310 Connector results[] = new Connector[connectors.length + 1]; 311 System.arraycopy(connectors, 0, results, 0, connectors.length); 312 results[connectors.length] = connector; 313 connectors = results; 314 315 if (initialized) 316 { 317 try 318 { 319 connector.initialize(); 320 } 321 catch (LifecycleException e) 322 { 323 e.printStackTrace(System.err); 324 } 325 } 326 327 if (started && (connector instanceof Lifecycle)) 328 { 329 try 330 { 331 ((Lifecycle) connector).start(); 332 } 333 catch (LifecycleException e) 334 { 335 ; 336 } 337 } 338 339 support.firePropertyChange("connector", null, connector); 341 } 342 343 } 344 345 public ObjectName [] getConnectorNames() 346 { 347 ObjectName results[] = new ObjectName [connectors.length]; 348 for (int i = 0; i < results.length; i++) 349 { 350 } 355 return results; 356 } 357 358 359 364 public void addPropertyChangeListener(PropertyChangeListener listener) 365 { 366 367 support.addPropertyChangeListener(listener); 368 369 } 370 371 372 375 public Connector[] findConnectors() 376 { 377 378 return (connectors); 379 380 } 381 382 383 390 public void removeConnector(Connector connector) 391 { 392 393 synchronized (connectors) 394 { 395 int j = -1; 396 for (int i = 0; i < connectors.length; i++) 397 { 398 if (connector == connectors[i]) 399 { 400 j = i; 401 break; 402 } 403 } 404 if (j < 0) 405 return; 406 if (started && (connectors[j] instanceof Lifecycle)) 407 { 408 try 409 { 410 ((Lifecycle) connectors[j]).stop(); 411 } 412 catch (LifecycleException e) 413 { 414 ; 415 } 416 } 417 connectors[j].setContainer(null); 418 connector.setService(null); 419 int k = 0; 420 Connector results[] = new Connector[connectors.length - 1]; 421 for (int i = 0; i < connectors.length; i++) 422 { 423 if (i != j) 424 results[k++] = connectors[i]; 425 } 426 connectors = results; 427 428 support.firePropertyChange("connector", connector, null); 430 } 431 432 } 433 434 435 440 public void removePropertyChangeListener(PropertyChangeListener listener) 441 { 442 443 support.removePropertyChangeListener(listener); 444 445 } 446 447 448 451 public String toString() 452 { 453 454 StringBuffer sb = new StringBuffer ("StandardService["); 455 sb.append(getName()); 456 sb.append("]"); 457 return (sb.toString()); 458 459 } 460 461 462 464 465 470 public void addLifecycleListener(LifecycleListener listener) 471 { 472 473 lifecycle.addLifecycleListener(listener); 474 475 } 476 477 478 482 public LifecycleListener[] findLifecycleListeners() 483 { 484 485 return lifecycle.findLifecycleListeners(); 486 487 } 488 489 490 495 public void removeLifecycleListener(LifecycleListener listener) 496 { 497 498 lifecycle.removeLifecycleListener(listener); 499 500 } 501 502 503 512 public void start() throws LifecycleException 513 { 514 515 if (started) 517 { 518 log.info(sm.getString("standardService.start.started")); 519 } 520 521 if (!initialized) 522 init(); 523 524 lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); 526 527 log.info(sm.getString("standardService.start.name", this.name)); 528 lifecycle.fireLifecycleEvent(START_EVENT, null); 529 started = true; 530 531 if (container != null) 533 { 534 synchronized (container) 535 { 536 if (container instanceof Lifecycle) 537 { 538 ((Lifecycle) container).start(); 539 } 540 } 541 } 542 543 551 lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null); 553 554 } 555 556 557 566 public void stop() throws LifecycleException 567 { 568 569 if (!started) 571 { 572 return; 573 } 574 575 lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null); 577 578 synchronized (connectors) 580 { 581 for (int i = 0; i < connectors.length; i++) 582 { 583 connectors[i].pause(); 584 } 585 } 586 587 try 589 { 590 Thread.sleep(1000); 591 } 592 catch (InterruptedException e) 593 { 594 } 596 597 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 598 599 log.info 600 (sm.getString("standardService.stop.name", this.name)); 601 started = false; 602 603 if (container != null) 605 { 606 synchronized (container) 607 { 608 if (container instanceof Lifecycle) 609 { 610 ((Lifecycle) container).stop(); 611 } 612 } 613 } 614 615 synchronized (connectors) 617 { 618 for (int i = 0; i < connectors.length; i++) 619 { 620 if (connectors[i] instanceof Lifecycle) 621 ((Lifecycle) connectors[i]).stop(); 622 } 623 } 624 625 if (oname == controller) 626 { 627 Registry.getRegistry(null, null).unregisterComponent(oname); 631 } 632 633 634 lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null); 636 637 } 638 639 640 644 public void initialize() 645 throws LifecycleException 646 { 647 if (initialized) 649 { 650 log.info(sm.getString("standardService.initialize.initialized")); 651 return; 652 } 653 initialized = true; 654 655 if (oname == null) 656 { 657 try 658 { 659 Container engine = this.getContainer(); 661 domain = engine.getName(); 662 oname = new ObjectName (domain + ":type=Service,serviceName=" + name); 663 this.controller = oname; 664 Registry.getRegistry(null, null) 665 .registerComponent(this, oname, null); 666 } 667 catch (Exception e) 668 { 669 log.error("Error registering ", e); 670 } 671 672 673 } 674 if (server == null) 675 { 676 679 ServerFactory.getServer().addService(this); 680 } 681 682 683 synchronized (connectors) 685 { 686 for (int i = 0; i < connectors.length; i++) 687 { 688 connectors[i].initialize(); 689 } 690 } 691 } 692 693 public void destroy() throws LifecycleException 694 { 695 if (started) stop(); 696 } 698 699 public void init() 700 { 701 try 702 { 703 initialize(); 704 } 705 catch (Throwable t) 706 { 707 t.printStackTrace(); 708 } 709 } 710 711 protected String type; 712 protected String domain; 713 protected String suffix; 714 protected ObjectName oname; 715 protected ObjectName controller; 716 protected MBeanServer mserver; 717 718 public ObjectName getObjectName() 719 { 720 return oname; 721 } 722 723 public String getDomain() 724 { 725 return domain; 726 } 727 728 public ObjectName preRegister(MBeanServer server, 729 ObjectName name) throws Exception 730 { 731 oname = name; 732 mserver = server; 733 domain = name.getDomain(); 734 return name; 735 } 736 737 public void postRegister(Boolean registrationDone) 738 { 739 } 740 741 public void preDeregister() throws Exception 742 { 743 } 744 745 public void postDeregister() 746 { 747 } 748 749 } 750 | Popular Tags |