1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.util.ArrayList ; 23 24 import javax.management.ObjectName ; 25 26 import org.apache.catalina.Contained; 27 import org.apache.catalina.Container; 28 import org.apache.catalina.Lifecycle; 29 import org.apache.catalina.LifecycleException; 30 import org.apache.catalina.LifecycleListener; 31 import org.apache.catalina.Pipeline; 32 import org.apache.catalina.Valve; 33 import org.apache.catalina.util.LifecycleSupport; 34 import org.apache.catalina.util.StringManager; 35 import org.apache.catalina.valves.ValveBase; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.apache.tomcat.util.modeler.Registry; 39 40 41 53 54 public class StandardPipeline 55 implements Pipeline, Contained, Lifecycle 56 { 57 58 private static Log log = LogFactory.getLog(StandardPipeline.class); 59 60 62 63 66 public StandardPipeline() { 67 68 this(null); 69 70 } 71 72 73 79 public StandardPipeline(Container container) { 80 81 super(); 82 setContainer(container); 83 84 } 85 86 87 89 90 93 protected Valve basic = null; 94 95 96 99 protected Container container = null; 100 101 102 105 protected String info = "org.apache.catalina.core.StandardPipeline/1.0"; 106 107 108 111 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 112 113 114 117 protected static StringManager sm = 118 StringManager.getManager(Constants.Package); 119 120 121 124 protected boolean started = false; 125 126 127 130 protected Valve first = null; 131 132 133 135 136 139 public String getInfo() { 140 141 return (this.info); 142 143 } 144 145 146 148 149 152 public Container getContainer() { 153 154 return (this.container); 155 156 } 157 158 159 164 public void setContainer(Container container) { 165 166 this.container = container; 167 168 } 169 170 171 173 174 179 public void addLifecycleListener(LifecycleListener listener) { 180 181 lifecycle.addLifecycleListener(listener); 182 183 } 184 185 186 190 public LifecycleListener[] findLifecycleListeners() { 191 192 return lifecycle.findLifecycleListeners(); 193 194 } 195 196 197 202 public void removeLifecycleListener(LifecycleListener listener) { 203 204 lifecycle.removeLifecycleListener(listener); 205 206 } 207 208 214 public synchronized void start() throws LifecycleException { 215 216 if (started) 218 throw new LifecycleException 219 (sm.getString("standardPipeline.alreadyStarted")); 220 221 lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); 223 224 started = true; 225 226 Valve current = first; 228 if (current == null) { 229 current = basic; 230 } 231 while (current != null) { 232 if (current instanceof Lifecycle) 233 ((Lifecycle) current).start(); 234 registerValve(current); 235 current = current.getNext(); 236 } 237 238 lifecycle.fireLifecycleEvent(START_EVENT, null); 240 241 lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null); 243 244 } 245 246 247 253 public synchronized void stop() throws LifecycleException { 254 255 if (!started) 257 throw new LifecycleException 258 (sm.getString("standardPipeline.notStarted")); 259 260 lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null); 262 263 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 265 started = false; 266 267 Valve current = first; 269 if (current == null) { 270 current = basic; 271 } 272 while (current != null) { 273 if (current instanceof Lifecycle) 274 ((Lifecycle) current).stop(); 275 unregisterValve(current); 276 current = current.getNext(); 277 } 278 279 lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null); 281 } 282 283 private void registerValve(Valve valve) { 284 285 if( valve instanceof ValveBase && 286 ((ValveBase)valve).getObjectName()==null ) { 287 try { 288 289 String domain=((ContainerBase)container).getDomain(); 290 if( container instanceof StandardContext ) { 291 domain=((StandardContext)container).getEngineName(); 292 } 293 if( container instanceof StandardWrapper) { 294 Container ctx=((StandardWrapper)container).getParent(); 295 domain=((StandardContext)ctx).getEngineName(); 296 } 297 ObjectName vname=((ValveBase)valve).createObjectName( 298 domain, 299 ((ContainerBase)container).getJmxName()); 300 if( vname != null ) { 301 ((ValveBase)valve).setObjectName(vname); 302 Registry.getRegistry(null, null).registerComponent 303 (valve, vname, valve.getClass().getName()); 304 ((ValveBase)valve).setController 305 (((ContainerBase)container).getJmxName()); 306 } 307 } catch( Throwable t ) { 308 log.info( "Can't register valve " + valve , t ); 309 } 310 } 311 } 312 313 private void unregisterValve(Valve valve) { 314 if( valve instanceof ValveBase ) { 315 try { 316 ValveBase vb=(ValveBase)valve; 317 if( vb.getController()!=null && 318 vb.getController() == 319 ((ContainerBase)container).getJmxName() ) { 320 321 ObjectName vname=vb.getObjectName(); 322 Registry.getRegistry(null, null).getMBeanServer() 323 .unregisterMBean(vname); 324 ((ValveBase)valve).setObjectName(null); 325 } 326 } catch( Throwable t ) { 327 log.info( "Can't unregister valve " + valve , t ); 328 } 329 } 330 } 331 332 334 335 339 public Valve getBasic() { 340 341 return (this.basic); 342 343 } 344 345 346 358 public void setBasic(Valve valve) { 359 360 Valve oldBasic = this.basic; 362 if (oldBasic == valve) 363 return; 364 365 if (oldBasic != null) { 367 if (started && (oldBasic instanceof Lifecycle)) { 368 try { 369 ((Lifecycle) oldBasic).stop(); 370 } catch (LifecycleException e) { 371 log.error("StandardPipeline.setBasic: stop", e); 372 } 373 } 374 if (oldBasic instanceof Contained) { 375 try { 376 ((Contained) oldBasic).setContainer(null); 377 } catch (Throwable t) { 378 ; 379 } 380 } 381 } 382 383 if (valve == null) 385 return; 386 if (valve instanceof Contained) { 387 ((Contained) valve).setContainer(this.container); 388 } 389 if (valve instanceof Lifecycle) { 390 try { 391 ((Lifecycle) valve).start(); 392 } catch (LifecycleException e) { 393 log.error("StandardPipeline.setBasic: start", e); 394 return; 395 } 396 } 397 398 Valve current = first; 400 while (current != null) { 401 if (current.getNext() == oldBasic) { 402 current.setNext(valve); 403 break; 404 } 405 current = current.getNext(); 406 } 407 408 this.basic = valve; 409 410 } 411 412 413 432 public void addValve(Valve valve) { 433 434 if (valve instanceof Contained) 436 ((Contained) valve).setContainer(this.container); 437 438 if (started) { 440 if (valve instanceof Lifecycle) { 441 try { 442 ((Lifecycle) valve).start(); 443 } catch (LifecycleException e) { 444 log.error("StandardPipeline.addValve: start: ", e); 445 } 446 } 447 registerValve(valve); 449 } 450 451 if (first == null) { 453 first = valve; 454 valve.setNext(basic); 455 } else { 456 Valve current = first; 457 while (current != null) { 458 if (current.getNext() == basic) { 459 current.setNext(valve); 460 valve.setNext(basic); 461 break; 462 } 463 current = current.getNext(); 464 } 465 } 466 467 } 468 469 470 475 public Valve[] getValves() { 476 477 ArrayList valveList = new ArrayList (); 478 Valve current = first; 479 if (current == null) { 480 current = basic; 481 } 482 while (current != null) { 483 valveList.add(current); 484 current = current.getNext(); 485 } 486 487 return ((Valve[]) valveList.toArray(new Valve[0])); 488 489 } 490 491 public ObjectName [] getValveObjectNames() { 492 493 ArrayList valveList = new ArrayList (); 494 Valve current = first; 495 if (current == null) { 496 current = basic; 497 } 498 while (current != null) { 499 if (current instanceof ValveBase) { 500 valveList.add(((ValveBase) current).getObjectName()); 501 } 502 current = current.getNext(); 503 } 504 505 return ((ObjectName []) valveList.toArray(new ObjectName [0])); 506 507 } 508 509 517 public void removeValve(Valve valve) { 518 519 Valve current; 520 if(first == valve) { 521 first = first.getNext(); 522 current = null; 523 } else { 524 current = first; 525 } 526 while (current != null) { 527 if (current.getNext() == valve) { 528 current.setNext(valve.getNext()); 529 break; 530 } 531 current = current.getNext(); 532 } 533 534 if (first == basic) first = null; 535 536 if (valve instanceof Contained) 537 ((Contained) valve).setContainer(null); 538 539 if (started) { 541 if (valve instanceof Lifecycle) { 542 try { 543 ((Lifecycle) valve).stop(); 544 } catch (LifecycleException e) { 545 log.error("StandardPipeline.removeValve: stop: ", e); 546 } 547 } 548 unregisterValve(valve); 550 } 551 552 } 553 554 555 public Valve getFirst() { 556 if (first != null) { 557 return first; 558 } else { 559 return basic; 560 } 561 } 562 563 564 } 565 | Popular Tags |