1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.io.File ; 23 import java.util.List ; 24 25 import javax.management.MBeanServer ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 29 import org.apache.catalina.Container; 30 import org.apache.catalina.Engine; 31 import org.apache.catalina.Host; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.Realm; 34 import org.apache.catalina.Service; 35 import org.apache.catalina.realm.JAASRealm; 36 import org.apache.catalina.util.ServerInfo; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.apache.tomcat.util.modeler.Registry; 40 import org.apache.tomcat.util.modeler.modules.MbeansSource; 41 42 51 52 public class StandardEngine 53 extends ContainerBase 54 implements Engine { 55 56 private static Log log = LogFactory.getLog(StandardEngine.class); 57 58 60 61 64 public StandardEngine() { 65 66 super(); 67 pipeline.setBasic(new StandardEngineValve()); 68 69 try { 70 setJvmRoute(System.getProperty("jvmRoute")); 71 } catch(Exception ex) { 72 } 73 backgroundProcessorDelay = 10; 75 76 } 77 78 79 81 82 86 private String defaultHost = null; 87 88 89 92 private static final String info = 93 "org.apache.catalina.core.StandardEngine/1.0"; 94 95 96 99 private Service service = null; 100 101 105 private String baseDir = null; 106 107 114 private String mbeansFile = null; 115 116 118 private List mbeans; 119 120 121 125 private String jvmRouteId; 126 127 128 130 134 public Realm getRealm() { 135 Realm configured=super.getRealm(); 136 if( configured==null ) { 139 configured=new JAASRealm(); 140 this.setRealm( configured ); 141 } 142 return configured; 143 } 144 145 146 149 public String getDefaultHost() { 150 151 return (defaultHost); 152 153 } 154 155 156 161 public void setDefaultHost(String host) { 162 163 String oldDefaultHost = this.defaultHost; 164 if (host == null) { 165 this.defaultHost = null; 166 } else { 167 this.defaultHost = host.toLowerCase(); 168 } 169 support.firePropertyChange("defaultHost", oldDefaultHost, 170 this.defaultHost); 171 172 } 173 174 public void setName(String name ) { 175 if( domain != null ) { 176 super.setName( domain ); 179 return; 180 } 181 domain=name; super.setName( name ); 184 } 185 186 187 193 public void setJvmRoute(String routeId) { 194 jvmRouteId = routeId; 195 } 196 197 198 202 public String getJvmRoute() { 203 return jvmRouteId; 204 } 205 206 207 210 public Service getService() { 211 212 return (this.service); 213 214 } 215 216 217 222 public void setService(Service service) { 223 this.service = service; 224 } 225 226 public String getMbeansFile() { 227 return mbeansFile; 228 } 229 230 public void setMbeansFile(String mbeansFile) { 231 this.mbeansFile = mbeansFile; 232 } 233 234 public String getBaseDir() { 235 if( baseDir==null ) { 236 baseDir=System.getProperty("catalina.base"); 237 } 238 if( baseDir==null ) { 239 baseDir=System.getProperty("catalina.home"); 240 } 241 return baseDir; 242 } 243 244 public void setBaseDir(String baseDir) { 245 this.baseDir = baseDir; 246 } 247 248 250 251 257 public void addChild(Container child) { 258 259 if (!(child instanceof Host)) 260 throw new IllegalArgumentException 261 (sm.getString("standardEngine.notHost")); 262 super.addChild(child); 263 264 } 265 266 267 272 public String getInfo() { 273 274 return (info); 275 276 } 277 278 284 public void setParent(Container container) { 285 286 throw new IllegalArgumentException 287 (sm.getString("standardEngine.notParent")); 288 289 } 290 291 292 private boolean initialized=false; 293 294 public void init() { 295 if( initialized ) return; 296 initialized=true; 297 298 if( oname==null ) { 299 try { 301 if (domain==null) { 302 domain=getName(); 303 } 304 if(log.isDebugEnabled()) 305 log.debug( "Register " + domain ); 306 oname=new ObjectName (domain + ":type=Engine"); 307 controller=oname; 308 Registry.getRegistry(null, null) 309 .registerComponent(this, oname, null); 310 } catch( Throwable t ) { 311 log.info("Error registering ", t ); 312 } 313 } 314 315 if( mbeansFile == null ) { 316 String defaultMBeansFile=getBaseDir() + "/conf/tomcat5-mbeans.xml"; 317 File f=new File ( defaultMBeansFile ); 318 if( f.exists() ) mbeansFile=f.getAbsolutePath(); 319 } 320 if( mbeansFile != null ) { 321 readEngineMbeans(); 322 } 323 if( mbeans != null ) { 324 try { 325 Registry.getRegistry(null, null).invoke(mbeans, "init", false); 326 } catch (Exception e) { 327 log.error("Error in init() for " + mbeansFile, e); 328 } 329 } 330 331 351 if( service==null ) { 352 try { 354 service=new StandardService(); 355 service.setContainer( this ); 356 service.initialize(); 357 } catch( Throwable t ) { 358 log.error(t); 359 } 360 } 361 362 } 363 364 public void destroy() throws LifecycleException { 365 if( ! initialized ) return; 366 initialized=false; 367 368 ((StandardService)service).destroy(); 371 372 if( mbeans != null ) { 373 try { 374 Registry.getRegistry(null, null) 375 .invoke(mbeans, "destroy", false); 376 } catch (Exception e) { 377 log.error(sm.getString("standardEngine.unregister.mbeans.failed" ,mbeansFile), e); 378 } 379 } 380 if( mbeans != null ) { 382 try { 383 for( int i=0; i<mbeans.size() ; i++ ) { 384 Registry.getRegistry(null, null) 385 .unregisterComponent((ObjectName )mbeans.get(i)); 386 } 387 } catch (Exception e) { 388 log.error(sm.getString("standardEngine.unregister.mbeans.failed", mbeansFile), e); 389 } 390 } 391 392 Registry.getRegistry(null, null).resetMetadata(); 396 397 } 398 399 404 public void start() throws LifecycleException { 405 if( started ) { 406 return; 407 } 408 if( !initialized ) { 409 init(); 410 } 411 412 if( realm == null ) { 415 ObjectName realmName=null; 416 try { 417 realmName=new ObjectName ( domain + ":type=Realm"); 418 if( mserver.isRegistered(realmName ) ) { 419 mserver.invoke(realmName, "init", 420 new Object [] {}, 421 new String [] {} 422 ); 423 } 424 } catch( Throwable t ) { 425 log.debug("No realm for this engine " + realmName); 426 } 427 } 428 429 if(log.isInfoEnabled()) 432 log.info( "Starting Servlet Engine: " + ServerInfo.getServerInfo()); 433 if( mbeans != null ) { 434 try { 435 Registry.getRegistry(null, null) 436 .invoke(mbeans, "start", false); 437 } catch (Exception e) { 438 log.error("Error in start() for " + mbeansFile, e); 439 } 440 } 441 442 super.start(); 444 445 } 446 447 public void stop() throws LifecycleException { 448 super.stop(); 449 if( mbeans != null ) { 450 try { 451 Registry.getRegistry(null, null).invoke(mbeans, "stop", false); 452 } catch (Exception e) { 453 log.error("Error in stop() for " + mbeansFile, e); 454 } 455 } 456 } 457 458 459 462 public String toString() { 463 464 StringBuffer sb = new StringBuffer ("StandardEngine["); 465 sb.append(getName()); 466 sb.append("]"); 467 return (sb.toString()); 468 469 } 470 471 472 474 475 477 public ObjectName preRegister(MBeanServer server, 478 ObjectName name) throws Exception 479 { 480 super.preRegister(server,name); 481 482 this.setName( name.getDomain()); 483 484 return name; 485 } 486 487 public ObjectName getParentName() throws MalformedObjectNameException { 489 if (getService()==null) { 490 return null; 491 } 492 String name = getService().getName(); 493 ObjectName serviceName=new ObjectName (domain + 494 ":type=Service,serviceName="+name); 495 return serviceName; 496 } 497 498 public ObjectName createObjectName(String domain, ObjectName parent) 499 throws Exception 500 { 501 if( log.isDebugEnabled()) 502 log.debug("Create ObjectName " + domain + " " + parent ); 503 return new ObjectName ( domain + ":type=Engine"); 504 } 505 506 507 private void readEngineMbeans() { 508 try { 509 MbeansSource mbeansMB=new MbeansSource(); 510 File mbeansF=new File ( mbeansFile ); 511 mbeansMB.setSource(mbeansF); 512 513 Registry.getRegistry(null, null).registerComponent 514 (mbeansMB, domain + ":type=MbeansFile", null); 515 mbeansMB.load(); 516 mbeansMB.init(); 517 mbeansMB.setRegistry(Registry.getRegistry(null, null)); 518 mbeans=mbeansMB.getMBeans(); 519 520 } catch( Throwable t ) { 521 log.error( "Error loading " + mbeansFile, t ); 522 } 523 524 } 525 526 public String getDomain() { 527 if (domain!=null) { 528 return domain; 529 } else { 530 return getName(); 531 } 532 } 533 534 public void setDomain(String domain) { 535 this.domain = domain; 536 } 537 538 } 539 | Popular Tags |