FileDocCategorySizeDatePackage
StatsHolderImpl.javaAPI DocGlassfish v2 API11550Fri May 04 22:24:20 BST 2007com.sun.enterprise.admin.monitor.registry.spi

StatsHolderImpl

public class StatsHolderImpl extends Object implements StatsHolder
Creates and holds a hierarchy of Objects that represent various dependent components that require monitoring
author
Shreedhar Ganapathy

Fields Summary
private final String
name
private final Hashtable
children
private final MBeanServer
server
private Class
statsClass
private String
statsClassName
MonitoredObjectType
type
Stats
stats
ObjectName
objectName
String
dottedName
private static final Logger
logger
private static final com.sun.enterprise.util.i18n.StringManager
sm
Constructors Summary
public StatsHolderImpl(String name)
Creates a new instance of StatsHolderImpl

           
	   
		this.name = name;
		this.children = new Hashtable();
		this.server = findMBeanServer();
		logger.fine("StatsHolderImpl initialized, name = " + name);
	
public StatsHolderImpl(String name, MonitoredObjectType type)

		this(name);
		this.setType(type);
    
Methods Summary
private StatsHolderaddChild(StatsHolder sh)
Add a child node or leaf to this node.

param
statsHolder

		// The name of the child is the unique key
		assert (sh != null) : "Null StatsHolder to be added";
        if(!children.containsKey(sh.getName())){
            children.put(sh.getName(), sh);
			logger.fine("StatsHolder.addChild: New key created with statsHolder key name = " + sh.getName() + " type = " + sh.getType().toString());
        }
		else {
			sh = this.getChild(sh.getName());
			logger.fine("Not adding, StatsHolder.addChild: Child exists, name = " + sh.getName() + " type = " + sh.getType().toString());
			//Ideally an exception should be thrown
		}
		return ( sh );
    
public StatsHolderaddChild(java.lang.String name, MonitoredObjectType type)
Add a child node or leaf to this node

param
name
param
type
return
StatsHolder

		assert ( name != null && type != null) : "Asked to add a null name-type child";
        StatsHolder child = this.getChild(name);
		if (child == null) 
			child = this.addChild(new StatsHolderImpl(name, type));
		
		return ( child );
    
private javax.management.MBeanServerfindMBeanServer()
finds and returns an MBeanServer instance

        MBeanServer server = null;
        ArrayList servers = MBeanServerFactory.findMBeanServer(null);
        if(!servers.isEmpty()){
            server = (MBeanServer)servers.get(0);
        }
        return server;
    
public java.util.CollectiongetAllChildren()
return an array of StatHolder objects each representing a child of this node.

return
Collection

        return children.values();
    
public StatsHoldergetChild(java.lang.String name)

		final StatsHolder child = (StatsHolder) children.get(name);
		if (child == null) {
			logger.fine("SH.getChild - child is null with name = " + name);
		}
		return ( child );
	
public java.lang.StringgetDottedName()

		return dottedName;
	
public java.lang.StringgetName()
Returns name of this hierarchical node

        return name;
    
public javax.management.ObjectNamegetObjectName()

        return objectName;
    
public StatsgetStats()

		return ( this.stats );
	
public java.lang.ClassgetStatsClass()

		return ( this.statsClass );
	
public java.lang.StringgetStatsClassName()

		return ( this.statsClassName );
	
public MonitoredObjectTypegetType()
Returns type of this hierarchical node

        return type;
    
public voidregisterMBean()
registers a monitoring MBean with the MBeanServer

        try {
			if (server.isRegistered(this.objectName)) {
				logger.fine("ObjectName obj is already registered - this is an error, ignoring for now : " + this.objectName);
				return;
			}
            //final DynamicMBean mbean = MonitoringMBeansFactory.generateMonitoringMBean(stats);
            //server.registerMBean(mbean, this.objectName);
			final StatsHolderMBean mbean = new StatsHolderMBeanImpl(this);
			server.registerMBean(mbean, this.objectName);
			logger.finer("Registered the MBean for this StatsHolder: " + this.objectName);
        }
		catch(Exception e) {
			logger.fine("SH.registerMBean: Exception for object-name " + this.objectName);
			logger.throwing(StatsHolderImpl.class.getName(), "registerMBean()", e);
			//squelching it for now - throw RuntimeException
        }
    
public voidremoveAllChildren()
removes all children belonging to this node.

		 //removes all the children, grandchildren etc. recursively and deregisters mbeans
		 if (children.isEmpty()) {
			 this.unregisterMBean();
		 }
		 else {
			 final Enumeration e = children.elements();
			 while (e.hasMoreElements()) {
				 final StatsHolder sh = (StatsHolder)e.nextElement();
				 sh.removeAllChildren();
			 }
			 children.clear();
		 }
     
public voidremoveChild(java.lang.String name)
Remove a child node or leaf from this node.

param
name
param
type

        children.remove(name);
    
public voidsetDottedName(java.lang.String dottedName)

		this.dottedName = dottedName;
	
public voidsetObjectName(javax.management.ObjectName name)
sets the hierarchically denoted dotted name for this node.

        this.objectName = name;
    
public voidsetStats(Stats stats)
sets this hierarchical node's associated stats object. Used when node was originally created without a Stats implementation or a new monitoring level has been set requiring a new Stats registration

	    this.stats=stats;
    
public voidsetStatsClass(java.lang.Class c)

		if (! javax.management.j2ee.statistics.Stats.class.isAssignableFrom(c)) {
			final String msg = sm.getString("sh.not_a_stats_interface", c.getName());
			throw new IllegalArgumentException(msg);
		}
		//the given class has to be an interface.
		if (! c.isInterface()) {
			final String msg = sm.getString("sh.should_be_an_interface", c.getName());
			throw new IllegalArgumentException(msg);
		}
		
		this.statsClass = c;
		this.statsClassName = c.getName();
	
public voidsetStatsClassName(java.lang.String cName)

        Class c = null;
        try {
            c = Class.forName(cName);
        } catch (ClassNotFoundException cnfe) {
            final String msg = sm.getString("invalidclassname.statsholder",
            cName);
            throw new IllegalArgumentException(msg);
        }

		if (! javax.management.j2ee.statistics.Stats.class.isAssignableFrom(c)) {
			final String msg = sm.getString("sh.not_a_stats_interface", c.getName());
			throw new IllegalArgumentException(msg);
		}
		//the given class has to be an interface.
		if (! c.isInterface()) {
			final String msg = sm.getString("sh.should_be_an_interface", c.getName());
			throw new IllegalArgumentException(msg);
		}
		
		this.statsClass = c;
		this.statsClassName = cName;
	
public voidsetType(MonitoredObjectType type)

		this.type = type;
	
public voidunregisterMBean()
unregisters a monitoring MBean from the MBean Server

        String msg = null;
        try {
			if (server.isRegistered(this.objectName)) {
				server.unregisterMBean(this.objectName);
				logger.fine("SH.unregisterMonitoringMBean(): unregistered - " + objectName);
			}
			else {
				logger.fine("SH.unregisterMonitoringMBean(): never registered - so not unregistering" + objectName);
			}
        }
		catch(Exception e){
			logger.fine("SH.unregisterMBeanUnregistration failed, objectName = " + objectName);
			//squelching for now - throw RuntimeException
        }
    
voidwrite()
this is just a convenience - debugging method

		//new DumpThread().start();