1 23 24 31 package com.sun.enterprise.admin.monitor; 32 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 38 import javax.management.InstanceAlreadyExistsException ; 39 import javax.management.InstanceNotFoundException ; 40 import javax.management.MBeanRegistrationException ; 41 import javax.management.NotCompliantMBeanException ; 42 import javax.management.ObjectName ; 43 44 import com.sun.enterprise.admin.common.constant.AdminConstants; 45 import com.sun.enterprise.admin.server.core.AdminService; 46 47 import com.sun.enterprise.util.i18n.StringManager; 49 50 55 public class MonitoringHelper { 56 57 private static StringManager localStrings = 59 StringManager.getManager( MonitoringHelper.class ); 60 61 64 static Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 65 66 84 public static ObjectName registerEJBMonitoringMBean(String appName, 85 String moduleName, String ejbName, MonitoredObjectType type, 86 BaseMonitorMBean mbean) 87 throws InstanceAlreadyExistsException , MBeanRegistrationException { 88 BaseMonitorMBean app = getLevelOneMBeanForSure( 89 MonitoredObjectType.APPLICATION, appName); 90 BaseMonitorMBean module = getMBeanForSure(app, 91 MonitoredObjectType.EJBMODULE, moduleName); 92 module.addChild(ejbName, type, mbean); 93 return mbean.getObjectName(); 94 } 95 96 107 public static ObjectName getEJBMonitoringMBeanName(String appName, 108 String moduleName, String ejbName) 109 throws InstanceNotFoundException { 110 return getEJBMonitoringMBean(appName, moduleName, ejbName).getObjectName(); 111 } 112 113 126 public static void unregisterEJBMonitoringMBean(String appName, 127 String moduleName, String ejbName) 128 throws InstanceNotFoundException , MBeanRegistrationException { 129 BaseMonitorMBean module = getEJBModuleMBean(appName, moduleName); 130 BaseMonitorMBean ejb = module.getFirstChildByName(ejbName); 131 module.removeChild(ejb); 132 checkAndPurgeUnusedAppAndModule(appName, module); 133 } 134 135 152 public static ObjectName registerEJBMonitoringMBean(String standaloneModuleName, 153 String ejbName, MonitoredObjectType type, BaseMonitorMBean mbean) 154 throws InstanceAlreadyExistsException , MBeanRegistrationException { 155 BaseMonitorMBean standaloneModule = getLevelOneMBeanForSure( 156 MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName); 157 standaloneModule.addChild(ejbName, type, mbean); 158 return mbean.getObjectName(); 159 } 160 161 171 public static ObjectName getEJBMonitoringMBeanName(String standaloneModuleName, 172 String ejbName) 173 throws InstanceNotFoundException { 174 return getEJBMonitoringMBean(standaloneModuleName, ejbName).getObjectName(); 175 } 176 177 189 public static void unregisterEJBMonitoringMBean(String standaloneModuleName, 190 String ejbName) 191 throws InstanceNotFoundException , MBeanRegistrationException { 192 BaseMonitorMBean module = getEJBModuleMBean(standaloneModuleName); 193 BaseMonitorMBean ejb = module.getFirstChildByName(ejbName); 194 module.removeChild(ejb); 195 checkAndPurgeUnusedLevelOneMBean(module); 196 } 197 198 207 public static void registerEJBMethodMonitoringMBean(ObjectName ejbMBeanName, 208 String methodName, BaseMonitorMBean mbean) 209 throws InstanceNotFoundException , InstanceAlreadyExistsException , 210 MBeanRegistrationException { 211 BaseMonitorMBean ejb = getMonitorMBean(ejbMBeanName); 212 ejb.addChild(methodName, MonitoredObjectType.BEAN_METHOD, mbean); 213 } 214 215 232 public static ObjectName registerMonitoringMBean(String name, 233 BaseMonitorMBean mbean) 234 throws InstanceNotFoundException , MBeanRegistrationException { 235 return null; 236 } 237 238 251 public static void unregisterMonitoringMBean(String name) 252 throws InstanceNotFoundException , MBeanRegistrationException { 253 } 254 255 258 public static final String SYSTEM_ORB_NAME = "system"; 259 260 273 public static void registerSystemORBMonitoringMBean(BaseMonitorMBean mbean) 274 throws InstanceAlreadyExistsException , MBeanRegistrationException { 275 registerORBMonitoringMBean(SYSTEM_ORB_NAME, mbean); 276 } 277 278 296 public static void registerUserORBMonitoringMBean(String hint, 297 BaseMonitorMBean mbean) 298 throws InstanceAlreadyExistsException , MBeanRegistrationException { 299 if (SYSTEM_ORB_NAME.equalsIgnoreCase(hint)) { 300 String msg = localStrings.getString( 301 "admin.monitor.system_orb_name_used", hint); 302 throw new IllegalArgumentException (msg); 303 } 304 registerORBMonitoringMBean(getUserOrbMBeanName(hint), mbean); 305 } 306 307 322 private static void registerORBMonitoringMBean(String name, 323 BaseMonitorMBean mbean) 324 throws InstanceAlreadyExistsException , MBeanRegistrationException { 325 if (AdminService.getAdminService() == null) { 326 String msg = localStrings.getString( 327 "admin.monitor.admin_service_not_inited"); 328 throw new IllegalStateException (msg); 329 } 330 BaseMonitorMBean iiop = getLevelOneMBeanForSure( 331 MonitoredObjectType.IIOP_SERVICE, 332 MonitoredObjectType.IIOP_SERVICE.getTypeName()); 333 iiop.addChild(name, MonitoredObjectType.ORB, mbean); 334 logger.log(Level.FINEST, ORB_MBEAN_REGISTERED, name); 335 } 336 337 340 private static final HashMap userOrbNames = new HashMap (); 341 342 345 private static int orbNameUniquifier = 0; 346 347 350 public static final String DEFAULT_USER_ORB_HINT = "user"; 351 352 362 private static String getUserOrbMBeanName(String hint) { 363 if (hint == null || hint.trim().equals("") 364 || hint.equalsIgnoreCase(DEFAULT_USER_ORB_HINT) 365 || (hint.indexOf(COMMA) != -1) 366 || (hint.indexOf(SPACE) != -1) 367 || (hint.indexOf(COLON) != -1) 368 || (hint.indexOf(EQUALS) != -1)) { 369 logger.log(Level.FINEST, INVALID_USER_ORB_NAME_HINT, hint); 370 hint = DEFAULT_USER_ORB_HINT + (++orbNameUniquifier); 371 } 372 synchronized (userOrbNames) { 373 while (userOrbNames.containsKey(hint)) { 374 logger.log(Level.FINEST, USER_ORB_MBEAN_NAME_USED, hint); 375 hint = hint + (++orbNameUniquifier); 376 } 377 userOrbNames.put(hint, hint); 378 } 379 return hint; 380 } 381 382 400 public static ObjectName registerMonitorable(String name, 401 IMonitorable monitorable) 402 throws InstanceNotFoundException , MBeanRegistrationException { 403 return null; 404 } 405 406 420 public static void unregisterMonitorable(String name) 421 throws InstanceNotFoundException , MBeanRegistrationException { 422 } 423 424 430 public static BaseMonitorMBean getMonitorMBean(ObjectName name) 431 throws InstanceNotFoundException { 432 BaseMonitorMBean mbean = getMonitorMBeanOrNull(name); 433 if (mbean == null) { 434 String msg = localStrings.getString( "admin.monitor.mbean_with_name_not_found", name ); 435 throw new InstanceNotFoundException ( msg ); 436 } 437 return mbean; 438 } 439 440 446 private static BaseMonitorMBean getMonitorMBeanOrNull(ObjectName name) { 447 BaseMonitorMBean mbean = 448 (BaseMonitorMBean)BaseMonitorMBean.objectNameMap.get(name); 449 return mbean; 450 } 451 452 463 private static BaseMonitorMBean getEJBMonitoringMBean(String appName, 464 String moduleName, String ejbName) 465 throws InstanceNotFoundException { 466 BaseMonitorMBean mbean = null; 467 BaseMonitorMBean app = getLevelOneMBean(MonitoredObjectType.APPLICATION, 468 appName); 469 if (app != null) { 470 BaseMonitorMBean module = app.getChildOrNull( 471 MonitoredObjectType.EJBMODULE, moduleName); 472 if (module != null) { 473 ArrayList list = module.getChildList(ejbName); 474 if (!list.isEmpty()) { 475 mbean = (BaseMonitorMBean)list.get(0); 476 } 477 } 478 } 479 if (mbean == null) { 480 String msg = localStrings.getString( "admin.monitor.no_mbean_for_appname_modulename_ejbname", appName, moduleName, ejbName ); 481 throw new InstanceNotFoundException ( msg ); 482 } 483 return mbean; 484 } 485 486 493 private static BaseMonitorMBean getEJBModuleMBean(String appName, 494 String moduleName) { 495 BaseMonitorMBean module = null; 496 BaseMonitorMBean app = getLevelOneMBean(MonitoredObjectType.APPLICATION, 497 appName); 498 if (app != null) { 499 module = app.getChildOrNull( 500 MonitoredObjectType.EJBMODULE, moduleName); 501 } 502 return module; 503 } 504 505 515 private static BaseMonitorMBean getEJBMonitoringMBean( 516 String standaloneModuleName, String ejbName) 517 throws InstanceNotFoundException { 518 BaseMonitorMBean mbean = null; 519 BaseMonitorMBean module = getLevelOneMBean( 520 MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName); 521 if (module != null) { 522 ArrayList list = module.getChildList(ejbName); 523 if (!list.isEmpty()) { 524 mbean = (BaseMonitorMBean)list.get(0); 525 } 526 } 527 if (mbean == null) { 528 String msg = localStrings.getString( "admin.monitor.no_mbean_for_standalonemodulename_ejbname", standaloneModuleName, ejbName ); 529 throw new InstanceNotFoundException ( msg ); 530 } 531 return mbean; 532 } 533 534 540 private static BaseMonitorMBean getEJBModuleMBean( 541 String standaloneModuleName) { 542 return getLevelOneMBean( 543 MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName); 544 } 545 546 554 private static BaseMonitorMBean getLevelOneMBean( 555 MonitoredObjectType type, String name) { 556 GenericMonitorMBean root = GenericMonitorMBean.getRoot(); 557 return root.getChildOrNull(type, name); 558 } 559 560 568 private static BaseMonitorMBean getLevelOneMBeanForSure( 569 MonitoredObjectType type, String name) { 570 GenericMonitorMBean root = GenericMonitorMBean.getRoot(); 571 return getMBeanForSure(root, type, name); 572 } 573 574 583 private static BaseMonitorMBean getMBeanForSure(BaseMonitorMBean parent, 584 MonitoredObjectType type, String name) { 585 BaseMonitorMBean mbean = parent.getChildOrNull(type, name); 586 if (mbean == null) { 587 mbean = new GenericMonitorMBean(); 588 try { 589 parent.addChild(name, type, mbean); 590 } catch (InstanceAlreadyExistsException iae) { 591 mbean = parent.getChildOrNull(type, name); 594 if (mbean == null) { 595 String msg = localStrings.getString( "admin.monitor.unable_getting_mbean", type, name, parent.getObjectName() ); 599 throw new RuntimeException ( msg ); 600 } 601 } catch (MBeanRegistrationException mbr) { 602 String msg = localStrings.getString( "admin.monitor.rootcause_unable_to_register_mbean", type, name, mbr.getMessage() ); 607 throw new RuntimeException ( msg ); 608 } 609 } 610 return mbean; 611 } 612 613 626 public static ObjectName registerTxnMonitoringMBean(BaseMonitorMBean mbean) 627 throws InstanceAlreadyExistsException , MBeanRegistrationException { 628 GenericMonitorMBean.getRoot().addChild(MonitoredObjectType.TXNMGR.getTypeName(), MonitoredObjectType.TXNMGR, mbean); 629 return mbean.getObjectName(); 630 } 631 632 public static ObjectName registerJdbcPoolMonitoringMBean(String poolName, MonitoredObjectType type, BaseMonitorMBean mbean) 633 throws InstanceAlreadyExistsException , MBeanRegistrationException 634 { 635 if(AdminService.getAdminService() != null){ 636 BaseMonitorMBean resMBean = getLevelOneMBeanForSure(MonitoredObjectType.RESOURCES,"resources"); 637 resMBean.addChild(poolName, type, mbean); 638 return mbean.getObjectName(); 639 } 640 return null; 641 642 } 643 644 public void unregisterJdbcPoolMonitoringMBean(MonitoredObjectType type,String poolName) 645 throws InstanceNotFoundException , MBeanRegistrationException 646 { 647 648 BaseMonitorMBean resMBean = getLevelOneMBean(MonitoredObjectType.RESOURCES,"resources"); 649 if(resMBean != null) 650 resMBean.removeChild(type,poolName); 651 } 652 653 659 private static void checkAndPurgeUnusedAppAndModule(String appName, 660 BaseMonitorMBean moduleMBean) { 661 if (moduleMBean == null) { 662 return; 663 } 664 BaseMonitorMBean appMBean = getLevelOneMBean( 665 MonitoredObjectType.APPLICATION, appName); 666 boolean purged = checkAndPurgeMBean(appMBean, moduleMBean); 667 if (purged) { 668 checkAndPurgeUnusedLevelOneMBean(appMBean); 669 } 670 return; 671 } 672 673 678 private static void checkAndPurgeUnusedLevelOneMBean( 679 BaseMonitorMBean levelOneMBean) { 680 if (levelOneMBean == null) { 681 return; 682 } 683 GenericMonitorMBean root = GenericMonitorMBean.getRoot(); 684 boolean purged = checkAndPurgeMBean(root, levelOneMBean); 685 return; 686 } 687 688 695 private static boolean checkAndPurgeMBean(BaseMonitorMBean parent, 696 BaseMonitorMBean mbean) { 697 boolean purged = false; 698 if (mbean != null && parent != null) { 699 ArrayList childList = mbean.getChildList(); 700 if (childList == null || childList.size() == 0) { 701 try { 702 parent.removeChild(mbean); 703 purged = true; 704 } catch (Throwable t) { 705 logger.log(Level.FINE, PURGE_MBEAN_FAILED, 710 mbean.getNodeType() + "/" + mbean.getNodeName()); 711 logger.log(Level.FINEST, PURGE_MBEAN_FAILED_TRACE, t); 712 } 713 } 714 } 715 return purged; 716 } 717 718 721 private static final char COMMA = ','; 722 private static final char SPACE = ' '; 723 private static final char COLON = ':'; 724 private static final char EQUALS = '='; 725 726 729 private static final String PURGE_MBEAN_FAILED = 730 "monitor.purge_mbean_failed"; 731 private static final String PURGE_MBEAN_FAILED_TRACE = 732 "monitor.purge_mbean_failed_trace"; 733 private static final String USER_ORB_MBEAN_NAME_USED = 734 "monitor.user_orb_mbean_name_used"; 735 private static final String ORB_MBEAN_REGISTERED = 736 "monitor.orb_mbean_registered"; 737 private static final String INVALID_USER_ORB_NAME_HINT = 738 "monitor.invalid_user_orb_name_hint"; 739 } 740 | Popular Tags |