FileDocCategorySizeDatePackage
MonitoringHelper.javaAPI DocGlassfish v2 API33079Fri May 04 22:33:44 BST 2007com.sun.enterprise.admin.monitor

MonitoringHelper

public class MonitoringHelper extends Object
Helper class to manage monitoring data provider object. This class has helper methods to register and unregister monitoring MBeans and implementors of monitorable interface (IMonitorable).

Fields Summary
private static com.sun.enterprise.util.i18n.StringManager
localStrings
static Logger
logger
A reference to logger object
public static final String
SYSTEM_ORB_NAME
Name of system orb. This string can not be used for naming any user orbs.
private static final HashMap
userOrbNames
A map to keep track of user orb names already in use.
private static int
orbNameUniquifier
An int that is used to make user orb names unique.
public static final String
DEFAULT_USER_ORB_HINT
Default prefix for user orbs.
private static final char
COMMA
private static final char
SPACE
private static final char
COLON
private static final char
EQUALS
private static final String
PURGE_MBEAN_FAILED
private static final String
PURGE_MBEAN_FAILED_TRACE
private static final String
USER_ORB_MBEAN_NAME_USED
private static final String
ORB_MBEAN_REGISTERED
private static final String
INVALID_USER_ORB_NAME_HINT
Constructors Summary
Methods Summary
private static booleancheckAndPurgeMBean(BaseMonitorMBean parent, BaseMonitorMBean mbean)
Purge an mbean if the mbean does not have any child mbean. This method tries to do

param
parent the parent mbean of the mbean that needs to purged.
param
mbean the mbean that needs to be purged
returns
true if the mbean was purged, false otherwise

        boolean purged = false;
        if (mbean != null && parent != null) {
            ArrayList childList = mbean.getChildList();
            if (childList == null || childList.size() == 0) {
                try {
                    parent.removeChild(mbean);
                    purged = true;
                } catch (Throwable t) {
                    // Failure in deletion of mbean is not fatal, it will have
                    // undesirable effect of not removing the module mbean even
                    // when the application is undeployed, but won't cause any
                    // harm. So just log the error.
                    logger.log(Level.FINE, PURGE_MBEAN_FAILED,
                            mbean.getNodeType() + "/" + mbean.getNodeName());
                    logger.log(Level.FINEST, PURGE_MBEAN_FAILED_TRACE, t);
                }
            }
        }
        return purged;
    
private static voidcheckAndPurgeUnusedAppAndModule(java.lang.String appName, BaseMonitorMBean moduleMBean)
Purge module mbean if it does not have any child mbean. If module mbean is purged then also purge application mbean if there are no more modules.

param
appName name of the application
param
moduleMBean module mbean

        if (moduleMBean == null) {
            return;
        }
        BaseMonitorMBean appMBean = getLevelOneMBean(
                MonitoredObjectType.APPLICATION, appName);
        boolean purged = checkAndPurgeMBean(appMBean, moduleMBean);
        if (purged) {
            checkAndPurgeUnusedLevelOneMBean(appMBean);
        }
        return;
    
private static voidcheckAndPurgeUnusedLevelOneMBean(BaseMonitorMBean levelOneMBean)
Purge unused mbean at the top level (below root) in the monitoring mbean tree. Any mbean that does not have children is considered unused.

param
levelOneMBean the mbean that needs to checked and purged

        if (levelOneMBean == null) {
            return;
        }
        GenericMonitorMBean root = GenericMonitorMBean.getRoot();
        boolean purged = checkAndPurgeMBean(root, levelOneMBean);
        return;
    
private static BaseMonitorMBeangetEJBModuleMBean(java.lang.String appName, java.lang.String moduleName)
Get MBean for specified application and ejb module. If the MBean for application or module does not exist, it returns null.

param
appName name of the application
param
moduleName name of the ejb module
return
MBean for the ejb module if it exists, null otherwise

        BaseMonitorMBean module = null;
        BaseMonitorMBean app = getLevelOneMBean(MonitoredObjectType.APPLICATION,
                appName);
        if (app != null) {
            module = app.getChildOrNull(
                    MonitoredObjectType.EJBMODULE, moduleName);
        }
        return module;
    
private static BaseMonitorMBeangetEJBModuleMBean(java.lang.String standaloneModuleName)
Get MBean for specified stand alone ejb module. If the MBean for stand alone ejb module does not exist, it returns null.

param
standaloneModuleName name of the stand alone ejb module
return
MBean for the stand alone ejb module if it exists, null otherwise

        return getLevelOneMBean(
                MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName);
    
private static BaseMonitorMBeangetEJBMonitoringMBean(java.lang.String appName, java.lang.String moduleName, java.lang.String ejbName)
Get monitoring MBean (in MBeanServer) for a ejb that is part of specified module in the specified application.

param
appName name of the application
param
moduleName name of the module
param
ejbName name of the ejb
throws
InstanceNotFoundException There is no MBean registered for specified bean, module and application in the MBean server.

        BaseMonitorMBean mbean = null;
        BaseMonitorMBean app = getLevelOneMBean(MonitoredObjectType.APPLICATION,
                appName);
        if (app != null) {
            BaseMonitorMBean module = app.getChildOrNull(
                    MonitoredObjectType.EJBMODULE, moduleName);
            if (module != null) {
                ArrayList list = module.getChildList(ejbName);
                if (!list.isEmpty()) {
                    mbean = (BaseMonitorMBean)list.get(0);
                }
            }
        }
        if (mbean == null) {
			String msg = localStrings.getString( "admin.monitor.no_mbean_for_appname_modulename_ejbname", appName, moduleName, ejbName );
            throw new InstanceNotFoundException( msg );
        }
        return mbean;
    
private static BaseMonitorMBeangetEJBMonitoringMBean(java.lang.String standaloneModuleName, java.lang.String ejbName)
Get monitoring MBean (in MBeanServer) for a ejb that is part of specified stand alone ejb module.

param
standaloneModuleName name of the stand alone ejb module
param
ejbName name of the ejb
throws
InstanceNotFoundException There is no MBean registered for specified bean, and stand alone ejb module in the MBean server.

        BaseMonitorMBean mbean = null;
        BaseMonitorMBean module = getLevelOneMBean(
                MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName);
        if (module != null) {
            ArrayList list = module.getChildList(ejbName);
            if (!list.isEmpty()) {
                mbean = (BaseMonitorMBean)list.get(0);
            }
        }
        if (mbean == null) {
			String msg = localStrings.getString( "admin.monitor.no_mbean_for_standalonemodulename_ejbname", standaloneModuleName, ejbName );
            throw new InstanceNotFoundException( msg );
        }
        return mbean;
    
public static javax.management.ObjectNamegetEJBMonitoringMBeanName(java.lang.String appName, java.lang.String moduleName, java.lang.String ejbName)
Get name of the monitoring MBean (in MBeanServer) for a ejb that is part of specified module in the specified application.

param
appName name of the application
param
moduleName name of the module
param
ejbName name of the ejb
throws
InstanceNotFoundException There is no MBean registered for specified bean, module and application in the MBean server.

        return getEJBMonitoringMBean(appName, moduleName, ejbName).getObjectName();
    
public static javax.management.ObjectNamegetEJBMonitoringMBeanName(java.lang.String standaloneModuleName, java.lang.String ejbName)
Get name of the monitoring MBean (in MBeanServer) for a ejb that is part of specified stand alone ejb module.

param
standaloneModuleName name of the stand alone ejb module.
param
ejbName name of the ejb
throws
InstanceNotFoundException There is no MBean registered for specified bean, and stand alone ejb module in the MBean server.

        return getEJBMonitoringMBean(standaloneModuleName, ejbName).getObjectName();
    
private static BaseMonitorMBeangetLevelOneMBean(MonitoredObjectType type, java.lang.String name)
Get a monitoring mbean from top level (immediate child of root monitor mbean). If the mbean does not exist, it returns null.

param
type type of the mbean
param
name name of the mbean
return
mbean representing specified type and name, if it exists, null otherwise.

        GenericMonitorMBean root = GenericMonitorMBean.getRoot();
        return root.getChildOrNull(type, name);
    
private static BaseMonitorMBeangetLevelOneMBeanForSure(MonitoredObjectType type, java.lang.String name)
Get a monitoring mbean from top level (immediate child of root monitor mbean). If the mbean does not exist, a generic mbean is created and added.

param
type type of the mbean
param
name name of the mbean
return
mbean representing specified type and name

        GenericMonitorMBean root = GenericMonitorMBean.getRoot();
        return getMBeanForSure(root, type, name);
    
private static BaseMonitorMBeangetMBeanForSure(BaseMonitorMBean parent, MonitoredObjectType type, java.lang.String name)
Get a MBean of specified type and name contained within specified parent mbean. If a MBean representing the child does not exist, an instance of generic mbean is created and added to the parent.

param
parent the parent MBean
param
type type of the child MBean
param
name name of the child MBean
return
an MBean representing specified child.

        BaseMonitorMBean mbean = parent.getChildOrNull(type, name);
        if (mbean == null) {
            mbean = new GenericMonitorMBean();
            try {
                parent.addChild(name, type, mbean);
            } catch (InstanceAlreadyExistsException iae) {
                // Narrow window between getChildOrNull() and addChild() call
                // allows this possibility. So get the child again
                mbean = parent.getChildOrNull(type, name);
                if (mbean == null) {
                    // If this is null, don't deal with it (removed?). If this
                    // exception shows up, synchronize calls to getChildOrNull()
                    // and addChild() on parent
					String msg = localStrings.getString( "admin.monitor.unable_getting_mbean", type, name, parent.getObjectName() );
                    throw new RuntimeException( msg );
                }
            } catch (MBeanRegistrationException mbr) {
                // This is thrown by errors during execution of method in
                // MBeanRegistration interface. GenericMonitorMBean does not
                // implement this interace, so if it happens throw a
                // RuntimeException
				String msg = localStrings.getString( "admin.monitor.rootcause_unable_to_register_mbean", type, name, mbr.getMessage() );
                throw new RuntimeException( msg );
            }
        }
        return mbean;
    
public static BaseMonitorMBeangetMonitorMBean(javax.management.ObjectName name)
Get monitoring mBean registered in MBean Server with specified name.

param
name registered name of the MBean in MBeanServer
throws
InstanceNotFoundException if the specified name is not registered in the MBean server.

        BaseMonitorMBean mbean = getMonitorMBeanOrNull(name);
        if (mbean == null) {
			String msg = localStrings.getString( "admin.monitor.mbean_with_name_not_found", name );
            throw new InstanceNotFoundException( msg );
        }
        return mbean;
    
private static BaseMonitorMBeangetMonitorMBeanOrNull(javax.management.ObjectName name)
Get monitoring mBean registered in MBean Server with specified name. If the mbean with specified name is not found this method returns null.

param
name registered name of the MBean in MBeanServer
return
mbean of specified name or null if it is not found

        BaseMonitorMBean mbean =
                (BaseMonitorMBean)BaseMonitorMBean.objectNameMap.get(name);
        return mbean;
    
private static java.lang.StringgetUserOrbMBeanName(java.lang.String hint)
Get a unique name for user orb. It tries to uniquify the name by adding integers to the end of the hint.

param
hint hint for orb name, if the name is not in use, it is used as is, otherwise integers are appended to it till it is unique. If hint is null, empty or any form (lowercase or uppercase) of constant DEFAULT_USER_ORB_HINT, the method uses constant DEFAULT_USER_ORB_HINT as hint.
return
a name that is derived from the specified hint and is not in use


                                                                                                               
         
        if (hint == null || hint.trim().equals("")
                 || hint.equalsIgnoreCase(DEFAULT_USER_ORB_HINT)
                 || (hint.indexOf(COMMA) != -1)
                 || (hint.indexOf(SPACE) != -1)
                 || (hint.indexOf(COLON) != -1)
                 || (hint.indexOf(EQUALS) != -1)) {
            logger.log(Level.FINEST, INVALID_USER_ORB_NAME_HINT, hint);
            hint = DEFAULT_USER_ORB_HINT + (++orbNameUniquifier);
        }
        synchronized (userOrbNames) {
            while (userOrbNames.containsKey(hint)) {
                logger.log(Level.FINEST, USER_ORB_MBEAN_NAME_USED, hint);
                hint = hint + (++orbNameUniquifier);
            }
            userOrbNames.put(hint, hint);
        }
        return hint;
    
public static voidregisterEJBMethodMonitoringMBean(javax.management.ObjectName ejbMBeanName, java.lang.String methodName, BaseMonitorMBean mbean)

throws
InstanceNotFoundException There is no MBean registered for specified bean, and stand alone ejb module in the MBean server.
throws
InstanceAlreadyExistsException A MBean for specified parameter(s) is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.

        BaseMonitorMBean ejb = getMonitorMBean(ejbMBeanName);
        ejb.addChild(methodName, MonitoredObjectType.BEAN_METHOD, mbean);
    
public static javax.management.ObjectNameregisterEJBMonitoringMBean(java.lang.String appName, java.lang.String moduleName, java.lang.String ejbName, MonitoredObjectType type, BaseMonitorMBean mbean)
Register a monitoring MBean for a ejb that is part of specified module in the specified application.

param
appName name of the application
param
moduleName name of the module
param
ejbName name of the ejb
param
type type of ejb - entity, stateless or stateful session, mdb
param
mbean the monitorable mbean
returns
name under which the mbean is registered in MBeanServer
throws
InstanceAlreadyExistsException A MBean for specified parameter(s) is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.


                                                                                                                                  
        
                 
             
               
        BaseMonitorMBean app = getLevelOneMBeanForSure(
                MonitoredObjectType.APPLICATION, appName);
        BaseMonitorMBean module = getMBeanForSure(app,
                MonitoredObjectType.EJBMODULE, moduleName);
        module.addChild(ejbName, type, mbean);
        return mbean.getObjectName();
    
public static javax.management.ObjectNameregisterEJBMonitoringMBean(java.lang.String standaloneModuleName, java.lang.String ejbName, MonitoredObjectType type, BaseMonitorMBean mbean)
Register a monitoring MBean for a ejb that is part of specified stand alone ejb module.

param
standaloneModuleName name of the stand alone ejb module.
param
ejbName name of the ejb
param
type type of ejb - entity, stateless or stateful session, mdb
param
mbean the monitorable mbean
returns
name under which the mbean is registered in MBeanServer
throws
InstanceAlreadyExistsException A MBean for specified parameter(s) is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.

        BaseMonitorMBean standaloneModule = getLevelOneMBeanForSure(
                MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName);
        standaloneModule.addChild(ejbName, type, mbean);
        return mbean.getObjectName();
    
public static javax.management.ObjectNameregisterJdbcPoolMonitoringMBean(java.lang.String poolName, MonitoredObjectType type, BaseMonitorMBean mbean)

	    if(AdminService.getAdminService() != null){
		BaseMonitorMBean resMBean = getLevelOneMBeanForSure(MonitoredObjectType.RESOURCES,"resources");
       resMBean.addChild(poolName, type, mbean);
       return mbean.getObjectName();
        }
		return null;

    
public static javax.management.ObjectNameregisterMonitorable(java.lang.String name, IMonitorable monitorable)
Register a monitorable object under specified name. A MBean is created from the monitorable and then registered to MBean server. name is dotted representation that denotes the position of MBean within the current context (this instance). [TBD: Dotted notation]

param
name name of the MBean in dotted notation
param
monitorable monitorable object
return
name under which the mbean is registered in MBeanServer
throws
InstanceAlreadyExistsException A MBean for specified parameter(s) is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.
deprecated

        return null;
    
public static javax.management.ObjectNameregisterMonitoringMBean(java.lang.String name, BaseMonitorMBean mbean)
Register a monitoring MBean under specified name. name is dotted representation that denotes the position of MBean within the current context (this instance). [TBD: Dotted notation]

param
name name of the MBean in dotted notation
param
mbean monitoring mbean object
return
name under which the mbean is registered in MBeanServer
throws
InstanceAlreadyExistsException A MBean for specified parameter(s) is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.
deprecated

        return null;
    
private static voidregisterORBMonitoringMBean(java.lang.String name, BaseMonitorMBean mbean)
Register a ORB monitoring MBean. ORB monitoring MBeans are registered under the node iiop-service (which is directly under root node) in the tree of monitoring mbeans.

param
name name of the mbean to be registered
param
mbean the mbean to be registered
throws
IllegalStateException if the method is called when admin service has not been initialized (for example, a call by application client container, which does not ever initialize admin service)
throws
InstanceAlreadyExistsException A MBean for user orb is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.

        if (AdminService.getAdminService() == null) {
            String msg = localStrings.getString(
                    "admin.monitor.admin_service_not_inited");
            throw new IllegalStateException(msg);
        }
        BaseMonitorMBean iiop = getLevelOneMBeanForSure(
                MonitoredObjectType.IIOP_SERVICE,
                MonitoredObjectType.IIOP_SERVICE.getTypeName());
        iiop.addChild(name, MonitoredObjectType.ORB, mbean);
        logger.log(Level.FINEST, ORB_MBEAN_REGISTERED, name);
    
public static voidregisterSystemORBMonitoringMBean(BaseMonitorMBean mbean)
Register a mbean to monitor system ORB. System ORB is created at server startup.

param
mbean the mbean to be registered.
throws
IllegalStateException if the method is called when admin service has not been initialized (for example, a call by application client container, which does not ever initialize admin service)
throws
InstanceAlreadyExistsException A MBean for system orb is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.


                                                                                                                              
        
               
        registerORBMonitoringMBean(SYSTEM_ORB_NAME, mbean);
    
public static javax.management.ObjectNameregisterTxnMonitoringMBean(BaseMonitorMBean mbean)
Register a monitoring MBean for transaction service.

param
mbean the monitorable mbean
returns
name under which the mbean is registered in MBeanServer
throws
InstanceAlreadyExistsException A MBean for specified parameter(s) is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.

        GenericMonitorMBean.getRoot().addChild(MonitoredObjectType.TXNMGR.getTypeName(), MonitoredObjectType.TXNMGR, mbean);
        return mbean.getObjectName();
    
public static voidregisterUserORBMonitoringMBean(java.lang.String hint, BaseMonitorMBean mbean)
Register a mbean to monitor user created ORB.

param
hint a hint for the name of mbean. The hint will be used as name if it is valid and has not been used earlier. A hint is valid if it is not null and does not contain any of following characters - comma, space, equals sign or colon. If hint is invalid a name derived from the constant DEFAULT_USER_ORB_HINT will be used for the mbean.
throws
IllegalArgumentException if the specified hint is reserved for use by system. The reserved name is value of constant SYSTEM_ORB_NAME
throws
IllegalStateException if the method is called when admin service has not been initialized (for example, a call by application client container, which does not ever initialize admin service)
throws
InstanceAlreadyExistsException A MBean for system orb is already under the control of the MBean server.
throws
MBeanRegistrationException preRegister (MBeanRegistration interface) method of the MBean has thrown an exception. The MBean will not be registered.

        if (SYSTEM_ORB_NAME.equalsIgnoreCase(hint)) {
            String msg = localStrings.getString(
                    "admin.monitor.system_orb_name_used", hint);
            throw new IllegalArgumentException(msg);
        }
        registerORBMonitoringMBean(getUserOrbMBeanName(hint), mbean);
    
public static voidunregisterEJBMonitoringMBean(java.lang.String appName, java.lang.String moduleName, java.lang.String ejbName)
Unregister a monitoring MBean for a ejb that is part of specified module in the specified application.

param
appName name of the application
param
moduleName name of the module
param
ejbName name of the ejb
throws
InstanceNotFoundException There is no MBean registered for specified bean, module and application in the MBean server.
throws
MBeanRegistrationException - The preDeregister (MBeanRegistration interface) method of the MBean has thrown an exception.

        BaseMonitorMBean module = getEJBModuleMBean(appName, moduleName);
        BaseMonitorMBean ejb = module.getFirstChildByName(ejbName);
        module.removeChild(ejb);
        checkAndPurgeUnusedAppAndModule(appName, module);
    
public static voidunregisterEJBMonitoringMBean(java.lang.String standaloneModuleName, java.lang.String ejbName)
Unregister a monitoring MBean for a ejb that is part of specified stand alone ejb module.

param
standaloneModuleName name of the stand alone ejb module.
param
ejbName name of the ejb
throws
InstanceNotFoundException There is no MBean registered for specified bean, and stand alone ejb module in the MBean server.
throws
MBeanRegistrationException - The preDeregister (MBeanRegistration interface) method of the MBean has thrown an exception.

        BaseMonitorMBean module = getEJBModuleMBean(standaloneModuleName);
        BaseMonitorMBean ejb = module.getFirstChildByName(ejbName);
        module.removeChild(ejb);
        checkAndPurgeUnusedLevelOneMBean(module);
    
public voidunregisterJdbcPoolMonitoringMBean(MonitoredObjectType type, java.lang.String poolName)


      BaseMonitorMBean resMBean = getLevelOneMBean(MonitoredObjectType.RESOURCES,"resources");
	   if(resMBean != null)
           resMBean.removeChild(type,poolName);
    
public static voidunregisterMonitorable(java.lang.String name)
Unregister a monitorable under specified name. A MBean is created from the monitorable and then registered to MBean server. name is dotted representation that denotes the position of MBean within the current context (this instance). [TBD: Dotted notation]

param
name name of the MBean in dotted notation
throws
InstanceNotFoundException The MBean specified is not registered in the MBean server.
throws
MBeanRegistrationException - The preDeregister (MBeanRegistration interface) method of the MBean has thrown an exception.
deprecated

    
public static voidunregisterMonitoringMBean(java.lang.String name)
Unregister a monitoring MBean under specified name. name is dotted representation that denotes the position of MBean within the current context (this instance). [TBD: Dotted notation]

param
name name of the MBean in dotted notation
throws
InstanceNotFoundException The MBean specified is not registered in the MBean server.
throws
MBeanRegistrationException - The preDeregister (MBeanRegistration interface) method of the MBean has thrown an exception.
deprecated