1 22 package org.jboss.management.j2ee; 23 24 import org.jboss.deployment.DeploymentInfo; 25 import org.jboss.deployment.MainDeployerConstants; 26 import org.jboss.deployment.SubDeployer; 27 import org.jboss.logging.Logger; 28 import org.jboss.management.j2ee.factory.DefaultManagedObjectFactoryMap; 29 import org.jboss.management.j2ee.factory.ManagedObjectFactory; 30 import org.jboss.management.j2ee.factory.ManagedObjectFactoryMap; 31 import org.jboss.system.ServiceControllerMBean; 32 import org.jboss.system.ServiceMBean; 33 import org.jboss.system.ServiceMBeanSupport; 34 35 import javax.management.JMException ; 36 import javax.management.MBeanException ; 37 import javax.management.MBeanServer ; 38 import javax.management.Notification ; 39 import javax.management.NotificationListener ; 40 import javax.management.ObjectName ; 41 import java.net.InetAddress ; 42 import java.util.Collection ; 43 import java.util.Iterator ; 44 import java.util.Set ; 45 46 55 public class LocalJBossServerDomain extends ServiceMBeanSupport 56 implements NotificationListener , LocalJBossServerDomainMBean 57 { 58 61 private static final Logger log = Logger.getLogger(LocalJBossServerDomain.class); 62 63 private J2EEDomain serverDomain; 64 65 68 private ObjectName mainDeployer; 69 72 private ObjectName sarDeployer; 73 76 private ObjectName earDeployer; 77 80 private ObjectName ejbDeployer; 81 84 private ObjectName rarDeployer; 85 88 private ObjectName jcaCMDeployer; 89 92 private ObjectName warDeployer; 93 96 private ObjectName carDeployer; 97 100 private ObjectName jmsService; 101 104 private ObjectName jndiService; 105 108 private ObjectName jtaService; 109 112 private ObjectName userTxService; 113 116 private ObjectName mailService; 117 120 private ObjectName rmiiiopService; 121 124 private ObjectName jndiBindingService; 125 128 private ManagedObjectFactoryMap managedObjFactoryMap; 129 private Class managedObjFactoryMapClass = DefaultManagedObjectFactoryMap.class; 130 131 135 public LocalJBossServerDomain() 136 { 137 } 138 139 143 147 public ObjectName getMainDeployer() 148 { 149 return mainDeployer; 150 } 151 152 156 public void setMainDeployer(ObjectName name) 157 { 158 this.mainDeployer = name; 159 } 160 161 165 public ObjectName getSARDeployer() 166 { 167 return sarDeployer; 168 } 169 170 174 public void setSARDeployer(ObjectName name) 175 { 176 this.sarDeployer = name; 177 } 178 179 183 public ObjectName getEARDeployer() 184 { 185 return earDeployer; 186 } 187 188 192 public void setEARDeployer(ObjectName name) 193 { 194 this.earDeployer = name; 195 } 196 197 201 public ObjectName getEJBDeployer() 202 { 203 return ejbDeployer; 204 } 205 206 210 public void setEJBDeployer(ObjectName name) 211 { 212 this.ejbDeployer = name; 213 } 214 215 219 public ObjectName getRARDeployer() 220 { 221 return rarDeployer; 222 } 223 224 228 public void setRARDeployer(ObjectName name) 229 { 230 this.rarDeployer = name; 231 } 232 233 237 public ObjectName getCMDeployer() 238 { 239 return jcaCMDeployer; 240 } 241 242 246 public void setCMDeployer(ObjectName name) 247 { 248 this.jcaCMDeployer = name; 249 } 250 251 255 public ObjectName getWARDeployer() 256 { 257 return warDeployer; 258 } 259 260 264 public void setWARDeployer(ObjectName name) 265 { 266 this.warDeployer = name; 267 } 268 269 273 public ObjectName getCARDeployer() 274 { 275 return carDeployer; 276 } 277 281 public void setCARDeployer(ObjectName name) 282 { 283 this.carDeployer = name; 284 } 285 286 290 public ObjectName getJMSService() 291 { 292 return jmsService; 293 } 294 295 299 public void setJMSService(ObjectName name) 300 { 301 this.jmsService = name; 302 } 303 304 308 public ObjectName getJNDIService() 309 { 310 return jndiService; 311 } 312 313 317 public void setJNDIService(ObjectName name) 318 { 319 this.jndiService = name; 320 } 321 322 326 public ObjectName getJTAService() 327 { 328 return jtaService; 329 } 330 331 335 public void setJTAService(ObjectName name) 336 { 337 this.jtaService = name; 338 } 339 340 344 public ObjectName getMailService() 345 { 346 return mailService; 347 } 348 349 353 public void setMailService(ObjectName name) 354 { 355 this.mailService = name; 356 } 357 358 362 public ObjectName getUserTransactionService() 363 { 364 return userTxService; 365 } 366 367 371 public void setUserTransactionService(ObjectName name) 372 { 373 this.userTxService = name; 374 } 375 376 380 public ObjectName getRMI_IIOPService() 381 { 382 return rmiiiopService; 383 } 384 385 389 public void setRMI_IIOPService(ObjectName name) 390 { 391 this.rmiiiopService = name; 392 } 393 394 398 public ObjectName getJndiBindingService() 399 { 400 return jndiBindingService; 401 } 402 403 407 public void setJndiBindingService(ObjectName name) 408 { 409 this.jndiBindingService = name; 410 } 411 412 416 public Class getManagementObjFactoryMapClass() 417 { 418 return managedObjFactoryMapClass; 419 } 420 421 425 public void setManagementObjFactoryMapClass(Class cls) 426 { 427 this.managedObjFactoryMapClass = cls; 428 } 429 430 437 public void handleNotification(Notification msg, Object handback) 438 { 439 MBeanServer mbeanServer = getServer(); 440 if (managedObjFactoryMap == null || mbeanServer == null) 441 { 442 return; 443 } 444 445 log.debug("handleNotification: " + msg); 446 String type = msg.getType(); 447 Object userData = msg.getUserData(); 448 try 449 { 450 455 if (type.equals(ServiceMBean.CREATE_EVENT)) 456 { 457 ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg); 458 if (factory != null) 459 { 460 factory.create(mbeanServer, userData); 461 } 462 } 463 else if (type.equals(ServiceMBean.DESTROY_EVENT)) 464 { 465 ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg); 466 if (factory != null) 467 { 468 factory.destroy(mbeanServer, userData); 469 } 470 } 471 else if (type.equals(SubDeployer.START_NOTIFICATION)) 472 { 473 ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg); 474 if (factory != null) 475 { 476 factory.create(mbeanServer, userData); 477 } 478 } 479 else if (type.equals(SubDeployer.DESTROY_NOTIFICATION)) 480 { 481 ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg); 482 if (factory != null) 483 { 484 DeploymentInfo di = (DeploymentInfo) msg.getUserData(); 485 factory.destroy(mbeanServer, di); 486 } 487 } 488 else if (type.equals(MainDeployerConstants.ADD_DEPLOYER)) 489 { 490 ObjectName deployerName = (ObjectName ) msg.getUserData(); 491 registerWithDeployer(deployerName); 492 } 493 else if (type.equals(MainDeployerConstants.REMOVE_DEPLOYER)) 494 { 495 ObjectName deployerName = (ObjectName ) msg.getUserData(); 496 unregisterWithDeployer(deployerName); 497 } 498 } 499 catch (Throwable t) 500 { 501 log.debug("Failed to handle event", t); 502 } 503 } 504 505 public String toString() 506 { 507 return "LocalJBossServerDomain { " + super.toString() + " } []"; 508 } 509 510 public void createService() throws Exception 511 { 512 MBeanServer server = getServer(); 513 514 setupJ2EEMBeans(server); 515 516 registerWithController(server); 517 518 populateFactoryMap(); 519 registerWithCurrentDeployers(server); 520 } 521 522 529 protected void destroyService() throws Exception 530 { 531 MBeanServer server = getServer(); 532 533 unregisterWithCurrentDeployers(server); 534 535 cleanupLeftoverMBeans(server); 536 537 unregisterWithController(server); 538 } 539 540 545 protected void registerWithDeployer(ObjectName deployerName) 546 { 547 log.debug("Registering as listener of deployer: " + deployerName); 548 try 549 { 550 getServer().addNotificationListener(deployerName, this, null, null); 551 } 552 catch (Exception e) 553 { 554 log.debug("Failed to register with deployer: " + deployerName, e); 555 } 556 } 557 558 563 protected void unregisterWithDeployer(ObjectName deployerName) 564 { 565 log.debug("Unregistering as listener of deployer: " + deployerName); 566 try 567 { 568 getServer().removeNotificationListener(deployerName, this); 569 } 570 catch (Exception e) 571 { 572 log.debug("Failed to unregister with deployer: " + deployerName, e); 573 } 574 } 575 576 582 private void populateFactoryMap() throws Exception 583 { 584 managedObjFactoryMap = (ManagedObjectFactoryMap) managedObjFactoryMapClass.newInstance(); 586 managedObjFactoryMap.setSARDeployer(sarDeployer); 587 managedObjFactoryMap.setEARDeployer(earDeployer); 588 managedObjFactoryMap.setEJBDeployer(ejbDeployer); 589 managedObjFactoryMap.setRARDeployer(rarDeployer); 590 managedObjFactoryMap.setCMDeployer(jcaCMDeployer); 591 managedObjFactoryMap.setWARDeployer(warDeployer); 592 managedObjFactoryMap.setJMSResource(jmsService); 593 managedObjFactoryMap.setJNDIResource(jndiService); 594 managedObjFactoryMap.setJTAResource(jtaService); 595 managedObjFactoryMap.setJTAResource(userTxService); 596 managedObjFactoryMap.setJavaMailResource(mailService); 597 managedObjFactoryMap.setRMI_IIOPResource(rmiiiopService); 598 } 599 600 605 private void setupJ2EEMBeans(MBeanServer mbeanServer) 606 { 607 try 609 { 610 log.debug("setupJ2EEMBeans(), create J2EEServer instance"); 611 Package pkg = Package.getPackage("org.jboss"); 612 String vendor = pkg.getSpecificationVendor(); 613 String version = pkg.getImplementationVersion(); 614 serverDomain = new J2EEDomain(serviceName.getDomain()); 616 ObjectName domain = serverDomain.getObjectName(); 617 mbeanServer.registerMBean(serverDomain, domain); 618 J2EEServer j2eeServer = new J2EEServer("Local", domain, vendor, version); 620 ObjectName lServer = j2eeServer.getObjectName(); 621 mbeanServer.registerMBean(j2eeServer, lServer); 622 623 String hostName = "localhost"; 625 try 626 { 627 InetAddress lLocalHost = InetAddress.getLocalHost(); 628 hostName = lLocalHost.getHostName(); 629 } 630 catch (Exception e) 631 { 632 } 634 JVM jvm = new JVM("localhost", lServer, 635 System.getProperty("java.version"), 636 System.getProperty("java.vendor"), 637 hostName); 638 ObjectName jvmName = jvm.getObjectName(); 639 mbeanServer.registerMBean(jvm, jvmName); 640 } 641 catch (JMException jme) 642 { 643 log.debug("setupJ2EEMBeans - unexpected JMException", jme); 644 } 645 catch (Exception e) 646 { 647 log.debug("setupJ2EEMBeans - unexpected exception", e); 648 } 649 } 650 651 656 private void registerWithController(MBeanServer mbeanServer) 657 { 658 try 659 { 660 mbeanServer.addNotificationListener(ServiceControllerMBean.OBJECT_NAME, 661 this, null, null); 662 log.debug("Registered as listener of: " + ServiceControllerMBean.OBJECT_NAME); 663 } 664 catch (JMException jme) 665 { 666 log.debug("unexpected exception", jme); 667 } 668 catch (Exception e) 669 { 670 log.debug("unexpected exception", e); 671 } 672 } 673 674 679 private void unregisterWithController(MBeanServer mbeanServer) 680 { 681 try 682 { 683 mbeanServer.removeNotificationListener(ServiceControllerMBean.OBJECT_NAME, this); 684 log.debug("UNRegistered as listener of: " + ServiceControllerMBean.OBJECT_NAME); 685 } 686 catch (JMException jme) 687 { 688 log.debug("unexpected exception", jme); 689 } 690 catch (Exception e) 691 { 692 log.debug("unexpected exception", e); 693 } 694 } 695 696 703 private void registerWithCurrentDeployers(MBeanServer mbeanServer) 704 throws Exception 705 { 706 log.debug("Registering with all deployers, mainDeployer=" + mainDeployer); 707 mbeanServer.addNotificationListener(mainDeployer, this, null, null); 708 709 log.debug("Getting current deployers"); 711 Object [] args = {}; 712 String [] sig = {}; 713 Collection deployers = (Collection ) mbeanServer.invoke(mainDeployer, 714 "listDeployers", args, sig); 715 Iterator iter = deployers.iterator(); 716 while (iter.hasNext()) 717 { 718 ObjectName name = (ObjectName ) iter.next(); 719 registerWithDeployer(name); 720 } 721 } 722 723 730 private void unregisterWithCurrentDeployers(MBeanServer mbeanServer) 731 throws Exception 732 { 733 log.debug("Unregistering with all deployers, mainDeployer=" + mainDeployer); 734 mbeanServer.removeNotificationListener(mainDeployer, this); 735 736 log.debug("Getting current deployers"); 738 Object [] args = {}; 739 String [] sig = {}; 740 Collection deployers = (Collection ) mbeanServer.invoke(mainDeployer, 741 "listDeployers", args, sig); 742 Iterator iter = deployers.iterator(); 743 while (iter.hasNext()) 744 { 745 ObjectName name = (ObjectName ) iter.next(); 746 unregisterWithDeployer(name); 747 } 748 } 749 750 755 private void cleanupLeftoverMBeans(MBeanServer mbeanServer) throws Exception 756 { 757 String domain = serviceName.getDomain(); 758 ObjectName domainName = new ObjectName (domain + ":*"); 759 Set domainNames = mbeanServer.queryNames(domainName, null); 760 log.debug("Found " + domainNames.size() + " domain mbeans"); 761 Iterator domainIter = domainNames.iterator(); 762 while (domainIter.hasNext()) 763 { 764 try 765 { 766 ObjectName name = (ObjectName ) domainIter.next(); 767 if (name.equals(serviceName)) 768 continue; 769 server.unregisterMBean(name); 770 } 771 catch (MBeanException ignore) 772 { 773 } 774 } 775 } 776 } 777 | Popular Tags |