1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import javax.management.MBeanRegistration ; 25 import javax.management.MBeanServer ; 26 import javax.management.ObjectName ; 27 import org.apache.catalina.Container; 28 import org.apache.catalina.Engine; 29 import org.apache.catalina.Lifecycle; 30 import org.apache.catalina.LifecycleException; 31 import org.apache.catalina.LifecycleListener; 32 import org.apache.catalina.Server; 33 import org.apache.catalina.Service; 34 import org.apache.catalina.ServerFactory; 35 import org.apache.catalina.connector.Connector; 36 import org.apache.catalina.util.LifecycleSupport; 37 import org.apache.catalina.util.StringManager; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.apache.tomcat.util.modeler.Registry; 41 42 43 50 51 public class StandardService 52 implements Lifecycle, Service, MBeanRegistration 53 { 54 private static Log log = LogFactory.getLog(StandardService.class); 55 56 57 59 60 63 private static final String info = 64 "org.apache.catalina.core.StandardService/1.0"; 65 66 67 70 private String name = null; 71 72 73 76 private LifecycleSupport lifecycle = new LifecycleSupport(this); 77 78 79 82 private static final StringManager sm = 83 StringManager.getManager(Constants.Package); 84 85 88 private Server server = null; 89 90 93 private boolean started = false; 94 95 96 99 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 100 101 102 105 protected Connector connectors[] = new Connector[0]; 106 107 108 113 protected Container container = null; 114 115 116 119 protected boolean initialized = false; 120 121 122 124 125 129 public Container getContainer() { 130 131 return (this.container); 132 133 } 134 135 136 142 public void setContainer(Container container) { 143 144 Container oldContainer = this.container; 145 if ((oldContainer != null) && (oldContainer instanceof Engine)) 146 ((Engine) oldContainer).setService(null); 147 this.container = container; 148 if ((this.container != null) && (this.container instanceof Engine)) 149 ((Engine) this.container).setService(this); 150 if (started && (this.container != null) && 151 (this.container instanceof Lifecycle)) { 152 try { 153 ((Lifecycle) this.container).start(); 154 } catch (LifecycleException e) { 155 ; 156 } 157 } 158 synchronized (connectors) { 159 for (int i = 0; i < connectors.length; i++) 160 connectors[i].setContainer(this.container); 161 } 162 if (started && (oldContainer != null) && 163 (oldContainer instanceof Lifecycle)) { 164 try { 165 ((Lifecycle) oldContainer).stop(); 166 } catch (LifecycleException e) { 167 ; 168 } 169 } 170 171 support.firePropertyChange("container", oldContainer, this.container); 173 174 } 175 176 public ObjectName getContainerName() { 177 if( container instanceof ContainerBase ) { 178 return ((ContainerBase)container).getJmxName(); 179 } 180 return null; 181 } 182 183 184 189 public String getInfo() { 190 191 return (info); 192 193 } 194 195 196 199 public String getName() { 200 201 return (this.name); 202 203 } 204 205 206 211 public void setName(String name) { 212 213 this.name = name; 214 215 } 216 217 218 221 public Server getServer() { 222 223 return (this.server); 224 225 } 226 227 228 233 public void setServer(Server server) { 234 235 this.server = server; 236 237 } 238 239 240 242 243 249 public void addConnector(Connector connector) { 250 251 synchronized (connectors) { 252 connector.setContainer(this.container); 253 connector.setService(this); 254 Connector results[] = new Connector[connectors.length + 1]; 255 System.arraycopy(connectors, 0, results, 0, connectors.length); 256 results[connectors.length] = connector; 257 connectors = results; 258 259 if (initialized) { 260 try { 261 connector.initialize(); 262 } catch (LifecycleException e) { 263 log.error("Connector.initialize", e); 264 } 265 } 266 267 if (started && (connector instanceof Lifecycle)) { 268 try { 269 ((Lifecycle) connector).start(); 270 } catch (LifecycleException e) { 271 log.error("Connector.start", e); 272 } 273 } 274 275 support.firePropertyChange("connector", null, connector); 277 } 278 279 } 280 281 public ObjectName [] getConnectorNames() { 282 ObjectName results[] = new ObjectName [connectors.length]; 283 for( int i=0; i<results.length; i++ ) { 284 } 289 return results; 290 } 291 292 293 298 public void addPropertyChangeListener(PropertyChangeListener listener) { 299 300 support.addPropertyChangeListener(listener); 301 302 } 303 304 305 308 public Connector[] findConnectors() { 309 310 return (connectors); 311 312 } 313 314 315 322 public void removeConnector(Connector connector) { 323 324 synchronized (connectors) { 325 int j = -1; 326 for (int i = 0; i < connectors.length; i++) { 327 if (connector == connectors[i]) { 328 j = i; 329 break; 330 } 331 } 332 if (j < 0) 333 return; 334 if (started && (connectors[j] instanceof Lifecycle)) { 335 try { 336 ((Lifecycle) connectors[j]).stop(); 337 } catch (LifecycleException e) { 338 log.error("Connector.stop", e); 339 } 340 } 341 connectors[j].setContainer(null); 342 connector.setService(null); 343 int k = 0; 344 Connector results[] = new Connector[connectors.length - 1]; 345 for (int i = 0; i < connectors.length; i++) { 346 if (i != j) 347 results[k++] = connectors[i]; 348 } 349 connectors = results; 350 351 support.firePropertyChange("connector", connector, null); 353 } 354 355 } 356 357 358 363 public void removePropertyChangeListener(PropertyChangeListener listener) { 364 365 support.removePropertyChangeListener(listener); 366 367 } 368 369 370 373 public String toString() { 374 375 StringBuffer sb = new StringBuffer ("StandardService["); 376 sb.append(getName()); 377 sb.append("]"); 378 return (sb.toString()); 379 380 } 381 382 383 385 386 391 public void addLifecycleListener(LifecycleListener listener) { 392 393 lifecycle.addLifecycleListener(listener); 394 395 } 396 397 398 402 public LifecycleListener[] findLifecycleListeners() { 403 404 return lifecycle.findLifecycleListeners(); 405 406 } 407 408 409 414 public void removeLifecycleListener(LifecycleListener listener) { 415 416 lifecycle.removeLifecycleListener(listener); 417 418 } 419 420 421 430 public void start() throws LifecycleException { 431 432 if (log.isInfoEnabled() && started) { 434 log.info(sm.getString("standardService.start.started")); 435 } 436 437 if( ! initialized ) 438 init(); 439 440 lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); 442 if(log.isInfoEnabled()) 443 log.info(sm.getString("standardService.start.name", this.name)); 444 lifecycle.fireLifecycleEvent(START_EVENT, null); 445 started = true; 446 447 if (container != null) { 449 synchronized (container) { 450 if (container instanceof Lifecycle) { 451 ((Lifecycle) container).start(); 452 } 453 } 454 } 455 456 synchronized (connectors) { 458 for (int i = 0; i < connectors.length; i++) { 459 if (connectors[i] instanceof Lifecycle) 460 ((Lifecycle) connectors[i]).start(); 461 } 462 } 463 464 lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null); 466 467 } 468 469 470 479 public void stop() throws LifecycleException { 480 481 if (!started) { 483 return; 484 } 485 486 lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null); 488 489 synchronized (connectors) { 491 for (int i = 0; i < connectors.length; i++) { 492 connectors[i].pause(); 493 } 494 } 495 496 try { 498 Thread.sleep(1000); 499 } catch (InterruptedException e) { 500 } 502 503 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 504 if(log.isInfoEnabled()) 505 log.info 506 (sm.getString("standardService.stop.name", this.name)); 507 started = false; 508 509 if (container != null) { 511 synchronized (container) { 512 if (container instanceof Lifecycle) { 513 ((Lifecycle) container).stop(); 514 } 515 } 516 } 517 synchronized (connectors) { 520 for (int i = 0; i < connectors.length; i++) { 521 if (connectors[i] instanceof Lifecycle) 522 ((Lifecycle) connectors[i]).stop(); 523 } 524 } 525 526 if( oname==controller ) { 527 Registry.getRegistry(null, null).unregisterComponent(oname); 531 } 532 533 534 lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null); 536 537 } 538 539 540 544 public void initialize() 545 throws LifecycleException 546 { 547 if (initialized) { 549 if(log.isInfoEnabled()) 550 log.info(sm.getString("standardService.initialize.initialized")); 551 return; 552 } 553 initialized = true; 554 555 if( oname==null ) { 556 try { 557 Container engine=this.getContainer(); 559 domain=engine.getName(); 560 oname=new ObjectName (domain + ":type=Service,serviceName="+name); 561 this.controller=oname; 562 Registry.getRegistry(null, null) 563 .registerComponent(this, oname, null); 564 } catch (Exception e) { 565 log.error(sm.getString("standardService.register.failed",domain),e); 566 } 567 568 569 } 570 if( server==null ) { 571 574 ServerFactory.getServer().addService(this); 575 } 576 577 578 synchronized (connectors) { 580 for (int i = 0; i < connectors.length; i++) { 581 connectors[i].initialize(); 582 } 583 } 584 } 585 586 public void destroy() throws LifecycleException { 587 if( started ) stop(); 588 } 590 591 public void init() { 592 try { 593 initialize(); 594 } catch( Throwable t ) { 595 log.error(sm.getString("standardService.initialize.failed",domain),t); 596 } 597 } 598 599 protected String type; 600 protected String domain; 601 protected String suffix; 602 protected ObjectName oname; 603 protected ObjectName controller; 604 protected MBeanServer mserver; 605 606 public ObjectName getObjectName() { 607 return oname; 608 } 609 610 public String getDomain() { 611 return domain; 612 } 613 614 public ObjectName preRegister(MBeanServer server, 615 ObjectName name) throws Exception { 616 oname=name; 617 mserver=server; 618 domain=name.getDomain(); 619 return name; 620 } 621 622 public void postRegister(Boolean registrationDone) { 623 } 624 625 public void preDeregister() throws Exception { 626 } 627 628 public void postDeregister() { 629 } 630 631 } 632 | Popular Tags |