1 2 3 27 package org.apache.coyote.tomcat5; 28 29 import java.util.Iterator ; 30 import java.util.Set ; 31 32 import javax.management.MBeanServer ; 33 import javax.management.MBeanServerNotification ; 34 import javax.management.MBeanAttributeInfo ; 36 import javax.management.MBeanInfo ; 37 import javax.management.Notification ; 39 import javax.management.NotificationListener ; 40 import javax.management.NotificationFilter ; 42 import javax.management.ObjectInstance ; 44 import javax.management.ObjectName ; 45 46 import com.sun.org.apache.commons.logging.Log; 47 import com.sun.org.apache.commons.logging.LogFactory; 48 49 import com.sun.org.apache.commons.modeler.Registry; 50 51 import org.apache.tomcat.util.http.mapper.Mapper; 52 53 import org.apache.tomcat.util.res.StringManager; 54 55 56 62 public class MapperListener 63 66 implements NotificationListener , NotificationFilter 68 { 70 private static Log log = LogFactory.getLog(MapperListener.class); 71 72 73 77 protected Mapper mapper = null; 78 79 82 protected MBeanServer mBeanServer = null; 83 84 85 88 private StringManager sm = 89 StringManager.getManager(Constants.Package); 90 91 private String domain="*"; 93 private String engine="*"; 94 95 96 private int port; 98 private String defaultHost; 99 101 102 private String myInstance; 104 106 107 109 110 113 public MapperListener(Mapper mapper) { 114 this.mapper = mapper; 115 } 116 117 118 120 public String getDomain() { 121 return domain; 122 } 123 124 public void setDomain(String domain) { 125 this.domain = domain; 126 } 127 128 public String getEngine() { 129 return engine; 130 } 131 132 public void setEngine(String engine) { 133 this.engine = engine; 134 } 135 136 public int getPort() { 138 return port; 139 } 140 141 public void setPort(int port) { 142 this.port = port; 143 } 144 145 public String getDefaultHost() { 146 return defaultHost; 147 } 148 149 public void setDefaultHost(String defaultHost) { 150 this.defaultHost = defaultHost; 151 } 152 154 157 public void init() { 158 159 myInstance = System.getProperty("com.sun.aas.instanceName"); 161 163 try { 164 165 mBeanServer = Registry.getServer(); 166 167 registerEngine(); 168 169 String onStr = domain + ":type=Host,*"; 171 ObjectName objectName = new ObjectName (onStr); 172 Set set = mBeanServer.queryMBeans(objectName, null); 173 Iterator iterator = set.iterator(); 174 while (iterator.hasNext()) { 175 ObjectInstance oi = (ObjectInstance ) iterator.next(); 176 registerHost(oi.getObjectName()); 177 } 178 179 180 onStr = "*:j2eeType=WebModule,*"; 182 objectName = new ObjectName (onStr); 183 set = mBeanServer.queryMBeans(objectName, null); 184 iterator = set.iterator(); 185 while (iterator.hasNext()) { 186 ObjectInstance oi = (ObjectInstance ) iterator.next(); 187 registerContext(oi.getObjectName()); 188 } 189 190 onStr = "*:j2eeType=Servlet,*"; 192 objectName = new ObjectName (onStr); 193 set = mBeanServer.queryMBeans(objectName, null); 194 iterator = set.iterator(); 195 while (iterator.hasNext()) { 196 ObjectInstance oi = (ObjectInstance ) iterator.next(); 197 registerWrapper(oi.getObjectName()); 198 } 199 200 onStr = "JMImplementation:type=MBeanServerDelegate"; 201 objectName = new ObjectName (onStr); 202 205 mBeanServer.addNotificationListener(objectName, this, this, null); 207 } catch (Exception e) { 209 log.warn("Error registering contexts",e); 210 } 211 212 } 213 214 215 227 public boolean isNotificationEnabled(Notification notification) { 228 229 if (notification instanceof MBeanServerNotification ) { 230 ObjectName objectName = 231 ((MBeanServerNotification ) notification).getMBeanName(); 232 String otherInstance = objectName.getKeyProperty("J2EEServer"); 233 if (myInstance != null && otherInstance != null 234 && !otherInstance.equals(myInstance)) { 235 return false; 236 } 237 } 238 239 return true; 240 241 } 242 244 245 247 248 public void handleNotification(Notification notification, 249 java.lang.Object handback) { 250 251 if (notification instanceof MBeanServerNotification ) { 252 ObjectName objectName = 253 ((MBeanServerNotification ) notification).getMBeanName(); 254 String j2eeType = objectName.getKeyProperty("j2eeType"); 255 String engineName = null; 256 if (j2eeType != null) { 257 if ((j2eeType.equals("WebModule")) || 258 (j2eeType.equals("Servlet"))) { 259 if (mBeanServer.isRegistered(objectName)) { 260 268 MBeanInfo info = null; 270 try { 271 info = mBeanServer.getMBeanInfo(objectName); 272 } catch (Exception e) { 273 } 275 if (info != null) { 276 boolean hasEngineNameAttribute = false; 277 MBeanAttributeInfo [] attrInfo = info.getAttributes(); 278 if (attrInfo != null) { 279 for (int i=0; i<attrInfo.length; i++) { 280 if ("engineName".equals( 281 attrInfo[i].getName())) { 282 hasEngineNameAttribute = true; 283 break; 284 } 285 } 286 } 287 if (hasEngineNameAttribute) { 288 try { 289 engineName = (String ) 290 mBeanServer.getAttribute(objectName, 291 "engineName"); 292 } catch (Exception e) { 293 } 295 } 296 } 297 } 299 } 300 } 301 302 if ( (!"*".equals(domain)) && 304 ( !domain.equals(objectName.getDomain()) ) && 305 ( (!domain.equals(engineName) ) && 306 (engineName != null) ) ) { 307 return; 308 } 309 310 log.debug( "Handle " + objectName ); 311 if (notification.getType().equals 312 (MBeanServerNotification.REGISTRATION_NOTIFICATION)) { 313 String type=objectName.getKeyProperty("type"); 314 if( "Host".equals( type )) { 315 try { 316 registerHost(objectName); 317 } catch (Exception e) { 318 log.warn("Error registering Host " + objectName, e); 319 } 320 } 321 322 if (j2eeType != null) { 323 if (j2eeType.equals("WebModule")) { 324 try { 325 registerContext(objectName); 326 } catch (Throwable t) { 327 log.warn("Error registering Context " + objectName,t); 328 } 329 } else if (j2eeType.equals("Servlet")) { 330 try { 331 registerWrapper(objectName); 332 } catch (Throwable t) { 333 log.warn("Error registering Wrapper " + objectName,t); 334 } 335 } 336 } 337 } else if (notification.getType().equals 338 (MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) { 339 String type=objectName.getKeyProperty("type"); 340 if( "Host".equals( type )) { 341 try { 342 unregisterHost(objectName); 343 } catch (Exception e) { 344 log.warn("Error unregistering Host " + objectName,e); 345 } 346 } 347 348 if (j2eeType != null) { 349 if (j2eeType.equals("WebModule")) { 350 try { 351 unregisterContext(objectName); 352 } catch (Throwable t) { 353 log.warn("Error unregistering webapp " + objectName,t); 354 } 355 } 356 } 357 } 358 } 359 360 } 361 362 363 365 private void registerEngine() 366 throws Exception 367 { 368 ObjectName engineName = new ObjectName 369 (domain + ":type=Engine"); 370 if ( ! mBeanServer.isRegistered(engineName)) return; 371 376 if (defaultHost == null) { 377 defaultHost = 378 (String ) mBeanServer.getAttribute(engineName, "defaultHost"); 379 } 380 382 ObjectName hostName = new ObjectName 383 (domain + ":type=Host," + "host=" + defaultHost); 384 385 if (!mBeanServer.isRegistered(hostName)) { 386 387 String onStr = domain + ":type=Host,*"; 389 ObjectName objectName = new ObjectName (onStr); 390 Set set = mBeanServer.queryMBeans(objectName, null); 391 Iterator iterator = set.iterator(); 392 String [] aliases; 393 boolean isRegisteredWithAlias = false; 394 395 while (iterator.hasNext()) { 396 397 if (isRegisteredWithAlias) break; 398 399 ObjectInstance oi = (ObjectInstance ) iterator.next(); 400 hostName = oi.getObjectName(); 401 aliases = (String []) 402 mBeanServer.invoke(hostName, "findAliases", null, null); 403 404 for (int i=0; i < aliases.length; i++){ 405 if (aliases[i].equalsIgnoreCase(defaultHost)){ 406 isRegisteredWithAlias = true; 407 break; 408 } 409 } 410 } 411 412 if (!isRegisteredWithAlias) 413 log.warn("Unknown default host: " + defaultHost); 414 } 415 416 if( defaultHost != null ) { 418 mapper.setDefaultHostName(defaultHost); 419 } 420 } 421 422 425 private void registerHost(ObjectName objectName) 426 throws Exception { 427 String name=objectName.getKeyProperty("host"); 428 if( name != null ) { 429 434 int[] ports = (int[]) mBeanServer.invoke 435 (objectName, "findPorts", null, null); 436 boolean portMatch = false; 437 if (ports != null) { 438 for (int i=0; i<ports.length; i++) { 439 if (ports[i] == this.port) { 440 portMatch = true; 441 break; 442 } 443 } 444 } 445 if (!portMatch) { 446 return; 447 } 448 String [] aliases = (String []) 450 mBeanServer.invoke(objectName, "findAliases", null, null); 451 mapper.addHost(name, aliases, objectName); 452 } 453 } 454 455 456 459 private void unregisterHost(ObjectName objectName) 460 throws Exception { 461 String name=objectName.getKeyProperty("host"); 462 if (name != null) { 464 int[] ports = (int[]) mBeanServer.invoke 465 (objectName, "findPorts", null, null); 466 boolean portMatch = false; 467 if (ports != null) { 468 for (int i=0; i<ports.length; i++) { 469 if (ports[i] == this.port) { 470 portMatch = true; 471 break; 472 } 473 } 474 } 475 if (!portMatch) { 476 return; 477 } 478 } 479 mapper.removeHost(name); 481 } 482 483 484 487 private void registerContext(ObjectName objectName) 488 throws Exception { 489 490 String name = objectName.getKeyProperty("name"); 491 492 String targetDomain=objectName.getDomain(); 495 if( ! domain.equals( targetDomain )) { 496 try { 497 targetDomain = (String ) mBeanServer.getAttribute 498 (objectName, "engineName"); 499 } catch (Exception e) { 500 } 502 if( ! domain.equals( targetDomain )) { 503 return; 505 } 506 } 507 508 String hostName = null; 509 String contextName = null; 510 if (name.startsWith("//")) { 511 name = name.substring(2); 512 } 513 int slash = name.indexOf("/"); 514 if (slash != -1) { 515 hostName = name.substring(0, slash); 516 contextName = name.substring(slash); 517 } else { 518 return; 519 } 520 if (contextName.equals("/")) { 522 contextName = ""; 523 } 524 525 log.debug(sm.getString 526 ("mapperListener.registerContext", contextName)); 527 528 Object context = 529 mBeanServer.invoke(objectName, "findMappingObject", null, null); 530 javax.naming.Context resources = (javax.naming.Context ) 532 mBeanServer.invoke(objectName, "findStaticResources", null, null); 533 String [] welcomeFiles = (String []) 535 mBeanServer.getAttribute(objectName, "welcomeFiles"); 536 537 mapper.addContext(hostName, contextName, context, 538 welcomeFiles, resources); 539 540 } 541 542 543 546 private void unregisterContext(ObjectName objectName) 547 throws Exception { 548 549 String name = objectName.getKeyProperty("name"); 550 551 String targetDomain=objectName.getDomain(); 554 if( ! domain.equals( targetDomain )) { 555 try { 556 targetDomain = (String ) mBeanServer.getAttribute 557 (objectName, "engineName"); 558 } catch (Exception e) { 559 } 561 if( ! domain.equals( targetDomain )) { 562 return; 564 } 565 } 566 567 String hostName = null; 568 String contextName = null; 569 if (name.startsWith("//")) { 570 name = name.substring(2); 571 } 572 int slash = name.indexOf("/"); 573 if (slash != -1) { 574 hostName = name.substring(0, slash); 575 contextName = name.substring(slash); 576 } else { 577 return; 578 } 579 if (contextName.equals("/")) { 581 contextName = ""; 582 } 583 584 log.debug(sm.getString 585 ("mapperListener.unregisterContext", contextName)); 586 587 mapper.removeContext(hostName, contextName); 588 589 } 590 591 592 595 private void registerWrapper(ObjectName objectName) 596 throws Exception { 597 598 String targetDomain=objectName.getDomain(); 601 if( ! domain.equals( targetDomain )) { 602 try { 603 targetDomain=(String ) mBeanServer.getAttribute(objectName, "engineName"); 604 } catch (Exception e) { 605 } 607 if( ! domain.equals( targetDomain )) { 608 return; 610 } 611 612 } 613 614 String wrapperName = objectName.getKeyProperty("name"); 615 String name = objectName.getKeyProperty("WebModule"); 616 617 String hostName = null; 618 String contextName = null; 619 if (name.startsWith("//")) { 620 name = name.substring(2); 621 } 622 int slash = name.indexOf("/"); 623 if (slash != -1) { 624 hostName = name.substring(0, slash); 625 contextName = name.substring(slash); 626 } else { 627 return; 628 } 629 if (contextName.equals("/")) { 631 contextName = ""; 632 } 633 634 log.debug(sm.getString 635 ("mapperListener.registerWrapper", 636 wrapperName, contextName)); 637 638 String [] mappings = (String []) 639 mBeanServer.invoke(objectName, "findMappings", null, null); 640 Object wrapper = 641 mBeanServer.invoke(objectName, "findMappingObject", null, null); 642 643 for (int i = 0; i < mappings.length; i++) { 644 boolean jspWildCard = (wrapperName.equals("jsp") 645 && mappings[i].endsWith("/*")); 646 mapper.addWrapper(hostName, contextName, mappings[i], wrapper, 647 jspWildCard); 648 } 649 650 } 651 652 653 } 654 | Popular Tags |