1 7 package org.jboss.web.tomcat.tc5; 8 9 import java.io.File ; 10 import java.util.Iterator ; 11 12 import javax.management.Attribute ; 13 import javax.management.MBeanServer ; 14 import javax.management.Notification ; 15 import javax.management.NotificationListener ; 16 import javax.management.ObjectInstance ; 17 import javax.management.ObjectName ; 18 import javax.security.jacc.PolicyContext ; 19 20 import org.apache.catalina.Lifecycle; 21 import org.apache.catalina.connector.Connector; 22 import org.apache.commons.modeler.Registry; 23 import org.jboss.deployment.DeploymentInfo; 24 import org.jboss.deployment.SubDeployer; 25 import org.jboss.system.server.Server; 26 import org.jboss.system.server.ServerImplMBean; 27 import org.jboss.system.ServiceControllerMBean; 28 import org.jboss.web.AbstractWebContainer; 29 import org.jboss.web.AbstractWebDeployer; 30 import org.jboss.web.tomcat.security.HttpServletRequestPolicyContextHandler; 31 import org.jboss.web.tomcat.tc5.session.SessionIDGenerator; 32 import org.jboss.security.plugins.JaasSecurityManagerServiceMBean; 33 import org.jboss.mx.util.MBeanProxyExt; 34 35 36 50 public class Tomcat5 extends AbstractWebContainer 51 implements Tomcat5MBean, NotificationListener 52 { 53 public static final String NAME = "Tomcat5"; 55 56 60 public static final String DEFAULT_CACHE_NAME = 61 "jboss.cache:service=TomcatClusteringCache"; 62 63 private String contextClassName = 66 "org.apache.catalina.core.StandardContext"; 67 68 71 private String catalinaDomain = "Catalina"; 72 73 77 private String cacheName = DEFAULT_CACHE_NAME; 78 79 83 protected String managerClass = "org.jboss.web.tomcat.tc5.session.JBossCacheManager"; 84 85 89 private int snapshotInterval = 1000; 90 91 95 private String snapshotMode = "instant"; 97 100 private boolean useLocalCache = true; 101 102 105 private boolean useJK = false; 106 107 110 private boolean useJBossWebLoader = true; 111 112 115 private String serverConfigFile = "server.xml"; 116 117 120 private String subjectAttributeName = null; 121 124 private boolean allowSelfPrivilegedWebApps = false; 125 126 private JaasSecurityManagerServiceMBean secMgrService; 127 128 private String [] filteredPackages; 129 130 private SubDeployer thisProxy; 131 132 public Tomcat5() 133 { 134 } 135 136 public String getName() 137 { 138 return NAME; 139 } 140 141 142 public String getManagerClass() 143 { 144 return managerClass; 145 } 146 147 public void setManagerClass(String managerClass) 148 { 149 this.managerClass = managerClass; 150 } 151 152 153 public String getDomain() 154 { 155 return this.catalinaDomain; 156 } 157 158 165 public void setDomain(String catalinaDomain) 166 { 167 this.catalinaDomain = catalinaDomain; 168 } 169 170 public void setContextMBeanCode(String className) 171 { 172 this.contextClassName = className; 173 } 174 175 public String getContextMBeanCode() 176 { 177 return contextClassName; 178 } 179 180 183 public void setSnapshotInterval(int interval) 184 { 185 this.snapshotInterval = interval; 186 } 187 188 191 public int getSnapshotInterval() 192 { 193 return this.snapshotInterval; 194 } 195 196 199 public void setSnapshotMode(String mode) 200 { 201 this.snapshotMode = mode; 202 } 203 204 207 public String getSnapshotMode() 208 { 209 return this.snapshotMode; 210 } 211 212 219 public String getCacheName() 220 { 221 return cacheName; 222 } 223 224 233 public void setCacheName(String cacheName) 234 { 235 this.cacheName = cacheName; 236 } 237 238 public boolean isUseLocalCache() 239 { 240 return useLocalCache; 241 } 242 243 public void setUseLocalCache(boolean useLocalCache) 244 { 245 this.useLocalCache = useLocalCache; 246 } 247 248 public boolean isUseJK() 249 { 250 return useJK; 251 } 252 253 public void setUseJK(boolean useJK) 254 { 255 this.useJK = useJK; 256 } 257 258 261 public void setSessionIdAlphabet(String sessionIdAlphabet) 262 { 263 SessionIDGenerator.getInstance().setSessionIdAlphabet(sessionIdAlphabet); 264 } 265 266 269 public String getSessionIdAlphabet() 270 { 271 return SessionIDGenerator.getInstance().getSessionIdAlphabet(); 272 } 273 274 public boolean getUseJBossWebLoader() 275 { 276 return useJBossWebLoader; 277 } 278 279 public void setUseJBossWebLoader(boolean flag) 280 { 281 this.useJBossWebLoader = flag; 282 } 283 284 public String getConfigFile() 285 { 286 return serverConfigFile; 287 } 288 289 public void setConfigFile(String configFile) 290 { 291 this.serverConfigFile = configFile; 292 } 293 294 public String getSubjectAttributeName() 295 { 296 return this.subjectAttributeName; 297 } 298 299 public void setSubjectAttributeName(String name) 300 { 301 this.subjectAttributeName = name; 302 } 303 304 public boolean isAllowSelfPrivilegedWebApps() 305 { 306 return allowSelfPrivilegedWebApps; 307 } 308 309 public void setAllowSelfPrivilegedWebApps(boolean allowSelfPrivilegedWebApps) 310 { 311 this.allowSelfPrivilegedWebApps = allowSelfPrivilegedWebApps; 312 } 313 314 public void setSecurityManagerService(JaasSecurityManagerServiceMBean mgr) 315 { 316 this.secMgrService = mgr; 317 } 318 319 public String [] getFilteredPackages() 320 { 321 return filteredPackages; 322 } 323 public void setFilteredPackages(String [] pkgs) 324 { 325 this.filteredPackages = pkgs; 326 } 327 328 public void startService() 329 throws Exception 330 { 331 332 System.setProperty("catalina.ext.dirs", 333 (System.getProperty("jboss.server.home.dir") 334 + File.separator + "lib")); 335 336 String objectNameS = catalinaDomain + ":type=server"; 337 ObjectName objectName = new ObjectName (objectNameS); 338 339 Registry.getRegistry().setMBeanServer(server); 341 342 server.createMBean("org.apache.commons.modeler.BaseModelMBean", 343 objectName, 344 new Object []{"org.apache.catalina.startup.Catalina"}, 345 new String []{"java.lang.String"}); 346 347 server.setAttribute(objectName, new Attribute 348 ("catalinaHome", 349 System.getProperty("jboss.server.home.dir"))); 350 server.setAttribute(objectName, new Attribute 351 ("configFile", serverConfigFile)); 352 server.setAttribute(objectName, new Attribute 353 ("useNaming", new Boolean (false))); 354 server.setAttribute(objectName, new Attribute 355 ("useShutdownHook", new Boolean (false))); 356 server.setAttribute(objectName, new Attribute 357 ("await", new Boolean (false))); 358 359 server.invoke(objectName, "create", new Object []{}, 360 new String []{}); 361 362 server.invoke(objectName, "start", new Object []{}, 363 new String []{}); 364 365 367 ObjectName ssoQuery = new ObjectName (catalinaDomain + ":type=Valve,*"); 368 Iterator iterator = server.queryMBeans(ssoQuery, null).iterator(); 369 while (iterator.hasNext()) 370 { 371 ObjectName ssoObjectName = 372 ((ObjectInstance ) iterator.next()).getObjectName(); 373 String name = ssoObjectName.getKeyProperty("name"); 374 375 386 387 if (cacheName != null && "ClusteredSingleSignOn".equals(name)) 390 { 391 String tcName = (String ) server.getAttribute(ssoObjectName, 392 "treeCacheName"); 393 tcName = (tcName != null ? tcName : DEFAULT_CACHE_NAME); 394 ObjectName ssoCacheName = new ObjectName (tcName); 395 if (ssoCacheName.equals(new ObjectName (DEFAULT_CACHE_NAME))) 398 { 399 log.info("Setting the cache name to " + cacheName + 400 " on " + ssoObjectName); 401 server.setAttribute(ssoObjectName, 402 new Attribute ("treeCacheName", cacheName)); 403 } 404 } 405 } 406 407 HttpServletRequestPolicyContextHandler handler = new HttpServletRequestPolicyContextHandler(); 409 PolicyContext.registerHandler(HttpServletRequestPolicyContextHandler.WEB_REQUEST_KEY, 410 handler, false); 411 412 serviceController = (ServiceControllerMBean) 414 MBeanProxyExt.create(ServiceControllerMBean.class, ServiceControllerMBean.OBJECT_NAME, server); 415 416 thisProxy = (SubDeployer) 419 MBeanProxyExt.create(SubDeployer.class, super.getServiceName(), super.getServer()); 420 421 mainDeployer.addDeployer(thisProxy); 423 424 Boolean started = (Boolean )server.getAttribute(ServerImplMBean.OBJECT_NAME, "Started"); 429 if (started.booleanValue() == true) 430 { 431 log.debug("Server '" + ServerImplMBean.OBJECT_NAME + 432 "' already started, starting connectors now"); 433 434 startConnectors(); 435 } 436 else 437 { 438 log.debug("Server '" + ServerImplMBean.OBJECT_NAME + 440 "' not started, registering for start-up notification"); 441 442 server.addNotificationListener(ServerImplMBean.OBJECT_NAME, this, null, null); 443 } 444 } 445 446 447 public void stopService() 448 throws Exception 449 { 450 451 String objectNameS = catalinaDomain + ":type=server"; 452 ObjectName objectName = new ObjectName (objectNameS); 453 454 server.invoke(objectName, "stop", new Object []{}, 455 new String []{}); 456 457 server.invoke(objectName, "destroy", new Object []{}, 458 new String []{}); 459 460 server.unregisterMBean(objectName); 461 462 MBeanServer server2 = server; 463 464 mainDeployer.removeDeployer(thisProxy); 466 467 ObjectName queryObjectName = new ObjectName 469 (catalinaDomain + ":*"); 470 Iterator iterator = 471 server2.queryMBeans(queryObjectName, null).iterator(); 472 while (iterator.hasNext()) 473 { 474 ObjectInstance oi = (ObjectInstance ) iterator.next(); 475 ObjectName toRemove = oi.getObjectName(); 476 if (!"WebServer".equals(toRemove.getKeyProperty("service"))) 478 { 479 if (server2.isRegistered(toRemove)) 480 { 481 server2.unregisterMBean(toRemove); 482 } 483 } 484 } 485 queryObjectName = new ObjectName ("Catalina:*"); 486 iterator = server2.queryMBeans(queryObjectName, null).iterator(); 487 while (iterator.hasNext()) 488 { 489 ObjectInstance oi = (ObjectInstance ) iterator.next(); 490 ObjectName name = oi.getObjectName(); 491 server2.unregisterMBean(name); 492 } 493 494 } 495 496 public void startConnectors() throws Exception 497 { 498 ObjectName service = new ObjectName (catalinaDomain + ":type=Service,serviceName=jboss.web"); 499 Object [] args = {}; 500 String [] sig = {}; 501 Connector[] connectors = (Connector[]) server.invoke(service, 502 "findConnectors", args, sig); 503 for (int n = 0; n < connectors.length; n++) 504 { 505 Lifecycle lc = (Lifecycle) connectors[n]; 506 lc.start(); 507 } 508 sendNotification(new Notification (TOMCAT_CONNECTORS_STARTED, 510 this, getNextNotificationSequenceNumber())); 511 } 512 513 public void stopConnectors() throws Exception 514 { 515 ObjectName service = new ObjectName (catalinaDomain + ":type=Service,serviceName=jboss.web"); 516 Object [] args = {}; 517 String [] sig = {}; 518 Connector[] connectors = (Connector[]) server.invoke(service, 519 "findConnectors", args, sig); 520 for (int n = 0; n < connectors.length; n++) 521 { 522 Lifecycle lc = (Lifecycle) connectors[n]; 523 lc.stop(); 524 } 525 } 526 527 public void handleNotification(Notification msg, Object handback) 528 { 529 String type = msg.getType(); 530 if (type.equals(Server.START_NOTIFICATION_TYPE)) 531 { 532 log.debug("Saw " + type + " notification, starting connectors"); 533 try 534 { 535 startConnectors(); 536 } 537 catch (Exception e) 538 { 539 log.warn("Failed to startConnectors", e); 540 } 541 } 542 } 543 544 public AbstractWebDeployer getDeployer(DeploymentInfo di) throws Exception 545 { 546 ClassLoader loader = di.ucl; 547 Class deployerClass = loader.loadClass("org.jboss.web.tomcat.tc5.TomcatDeployer"); 548 AbstractWebDeployer deployer = (AbstractWebDeployer) deployerClass.newInstance(); 549 DeployerConfig config = new DeployerConfig(); 550 config.setDefaultSecurityDomain(this.defaultSecurityDomain); 551 config.setSubjectAttributeName(this.subjectAttributeName); 552 config.setServiceClassLoader(getClass().getClassLoader()); 553 config.setManagerClass(this.managerClass); 554 config.setJava2ClassLoadingCompliance(this.java2ClassLoadingCompliance); 555 config.setUnpackWars(this.unpackWars); 556 config.setLenientEjbLink(this.lenientEjbLink); 557 config.setCatalinaDomain(catalinaDomain); 558 config.setContextClassName(contextClassName); 559 config.setServiceName(serviceName); 560 config.setSnapshotInterval(this.snapshotInterval); 561 config.setSnapshotMode(this.snapshotMode); 562 config.setUseLocalCache(this.useLocalCache); 563 config.setUseJK(this.useJK); 564 config.setSubjectAttributeName(this.subjectAttributeName); 565 config.setUseJBossWebLoader(this.useJBossWebLoader); 566 config.setAllowSelfPrivilegedWebApps(this.allowSelfPrivilegedWebApps); 567 config.setSecurityManagerService(this.secMgrService); 568 config.setFilteredPackages(filteredPackages); 569 deployer.setServer(server); 570 deployer.init(config); 571 return deployer; 572 } 573 574 } 575 | Popular Tags |