FileDocCategorySizeDatePackage
StatsImpl.javaAPI DocGlassfish v2 API11314Fri May 04 22:30:52 BST 2007com.sun.appserv.management.j2ee.statistics

StatsImpl

public class StatsImpl extends MapGetterInvocationHandler implements Serializable, javax.management.j2ee.statistics.Stats
Generic implementation of Stats based on either a Map or a {@link CompositeData}. There are two ways to implement a specific type of Stats object:
  • The subclass extends this base class, using getValue() to fetch the requested value from the Map maintained by this class.
  • Create a proxy implementing the desired Stats subclass interface and use an instance of this class as the {@link java.lang.reflect.InvocationHandler}.
In addition to the standard JSR 77 Stats interfaces, the following specific Stats interfaces are available:
  • {@link com.sun.appserv.management.monitor.statistics.AltJDBCConnectionPoolStats}
  • {@link com.sun.appserv.management.monitor.statistics.AltServletStats}
  • {@link com.sun.appserv.management.monitor.statistics.ConnectionManagerStats}
  • {@link com.sun.appserv.management.monitor.statistics.ConnectionPoolStats}
  • {@link com.sun.appserv.management.monitor.statistics.ConnectorConnectionPoolStats}
  • {@link com.sun.appserv.management.monitor.statistics.EJBCacheStats}
  • {@link com.sun.appserv.management.monitor.statistics.EJBMethodStats}
  • {@link com.sun.appserv.management.monitor.statistics.EJBPoolStats}
  • {@link com.sun.appserv.management.monitor.statistics.HTTPListenerStats}
  • {@link com.sun.appserv.management.monitor.statistics.HTTPServiceVirtualServerStats}
  • {@link com.sun.appserv.management.monitor.statistics.KeepAliveStats}
  • {@link com.sun.appserv.management.monitor.statistics.ThreadPoolStats}
  • {@link com.sun.appserv.management.monitor.statistics.TransactionServiceStats}
  • {@link com.sun.appserv.management.monitor.statistics.WebModuleVirtualServerStats}

Fields Summary
static final long
serialVersionUID
Constructors Summary
public StatsImpl(CompositeData compositeData)
Create a Stats from a CompositeData, whose keys are the Statistic names and whose values are CompositeData for the Statistic.

	
			           		       	 
		
	    
	
		this( createStatisticsMap( compositeData ) );
	
public StatsImpl(Map statisticsIn)
Create a Stats from a Map, whose keys are the Statistic names and whose values are the Statistics.

		super( requireSerializableMap( statisticsIn ) );
	
public StatsImpl(javax.management.j2ee.statistics.Statistic[] statistics)

		this( createStatisticsMap( statistics ) );
	
Methods Summary
private static java.util.MapcreateStatisticsMap(javax.management.openmbean.CompositeData compositeData)

		final CompositeType	compositeType	= compositeData.getCompositeType();
		final Set		keySet	= compositeType.keySet();
		final Iterator	iter	= keySet.iterator();
		
		final Map<String,Statistic>	map	= new HashMap<String,Statistic>();
		
		while ( iter.hasNext() )
		{
			final String		name	= (String)iter.next();
			final CompositeData	data	= (CompositeData)compositeData.get( name );
			
			map.put( name, StatisticFactory.create( data ) );
		}
		
		return( map );
	
private static java.util.MapcreateStatisticsMap(javax.management.j2ee.statistics.Statistic[] statistics)

		final Map<String,Statistic>	m	= new HashMap<String,Statistic>();
		
		for( int i = 0; i < statistics.length; ++i )
		{
			final Statistic	statistic	= requireStatisticImpl( statistics[ i ] );
			
			if ( statistic != null )
			{
				m.put( statistic.getName(), statistic );
			}
		}
		
		return( m );
	
public booleanequals(java.lang.Object rhs)

		boolean	equals	= false;
		
		if ( rhs != null && rhs instanceof Stats )
		{
			final Stats	stats	= (Stats)rhs;
			
			final String[]	myNames	= getStatisticNames();
			final String[]	rhsNames	= stats.getStatisticNames();
			if ( myNames.length == rhsNames.length )
			{
				equals	= true;
				
				for( int i = 0; i < myNames.length; ++i )
				{
					final String	statisticName	= myNames[ i ];
					final Statistic	myStatistic		= getStatistic( statisticName );
					final Statistic	rhsStatistic	= stats.getStatistic( statisticName );
					
					if ( ! myStatistic.equals( rhsStatistic ) )
					{
						equals	= false;
						break;
					}
				}
			}
		}
		return( equals );
	
public javax.management.j2ee.statistics.StatisticgetStatistic(java.lang.String statisticName)

		try
		{
			return( (Statistic)getValue( statisticName ) );
		}
		catch( Exception e )
		{
			final String msg	= "NOT a Statistic: " + statisticName +
				" of class " + getValue( statisticName ).getClass();
			throw new RuntimeException( msg, e );
		}
	
public java.lang.String[]getStatisticNames()

		final Set<String>	names	= getMap().keySet();
		
		return( GSetUtil.toStringArray( names ) );
	
public javax.management.j2ee.statistics.Statistic[]getStatistics()

		final Collection<Statistic>	values	= getMap().values();
		final Statistic[]	statistics	= new Statistic[ values.size() ];
		
		return( (Statistic[])values.toArray( statistics ) );
	
public java.lang.Objectinvoke(java.lang.Object myProxy, java.lang.reflect.Method method, java.lang.Object[] args)

   		Object			result	= null;
   		final String	methodName		= method.getName();
   		final int		numArgs	= args == null ? 0 : args.length;
   		
   		if ( numArgs == 0 && methodName.equals( "getStatisticNames" ) )
   		{
   			result	= getStatisticNames();
   		}
   		else if ( numArgs == 0 && methodName.equals( "getStatistics" ) )
   		{
   			result	= getStatistics();
   		}
   		else if ( numArgs	== 1  && methodName.equals( "getStatistic" ) &&
   			method.getReturnType() == Statistic.class &&
   			method.getParameterTypes()[ 0 ] == String.class )
   		{
   			result	= getStatistic( (String)args[ 0 ] );
   		}
   		else
   		{
   			result	= super.invoke( myProxy, method, args );
   		}

   		return( result );
   	
private static java.util.MaprequireSerializableMap(java.util.Map m)

		// required every entry to be a Statistic
		for( final Statistic s : m.values() )
		{
			if ( ! (s instanceof Statistic ) )
			{
				throw new IllegalArgumentException();
			}
		}
		
		Map<String,Statistic>	sMap	= null;
		
		if ( m instanceof Serializable )
		{
			sMap	= m;
		}
		else
		{
			sMap	= new HashMap<String, Statistic>( m );
		}
		
		return( sMap );
	
private static javax.management.j2ee.statistics.StatisticrequireStatisticImpl(javax.management.j2ee.statistics.Statistic statisticIn)
Ensure that every Statistic is an instance of one of our acceptable implementations. If you are adding a custom statistic, add that statistic here in the if clause.

		Statistic	statisticOut	= null;
		
		if ( statisticIn instanceof CountStatisticImpl ||
			statisticIn instanceof RangeStatisticImpl ||
			statisticIn instanceof BoundedRangeStatisticImpl ||
			statisticIn instanceof BoundaryStatisticImpl ||
			statisticIn instanceof TimeStatisticImpl  ||
			statisticIn instanceof NumberStatisticImpl  ||
			statisticIn instanceof StringStatisticImpl
			)
		{
			statisticOut	= statisticIn;
		}
		else if ( statisticIn instanceof MapStatistic )
		{
			final Class<? extends Statistic> theInterface	=
			    StatisticFactory.getInterface( statisticIn );
			
			if ( theInterface != MapStatistic.class )
			{
				statisticOut = StatisticFactory.create( theInterface,
								J2EEUtil.statisticToMap( statisticIn ) );
			}
			else if ( ! ( statisticIn instanceof MapStatisticImpl ) )
			{
				// it's a MapStatistic, but not our implementation.
				statisticOut	= new MapStatisticImpl( statisticIn );
			}
		}
		else
		{
			// some weird kind, convert it to generic Statistic
			assert( false ) :
				"requireStatisticImpl: unsupported Statistic type of class " + statisticIn.getClass().getName();
			statisticOut	= new MapStatisticImpl( statisticIn );
		}
		
		return( statisticOut );
	
private static javax.management.j2ee.statistics.Statistic[]requireStatisticImpl(javax.management.j2ee.statistics.Statistic[] statisticsIn)
Ensure that every Statistic is an instance of StatisticsImpl.

		boolean	convert	= false;
		
		// avoid conversion if already in desired form
		for( int i = 0; i < statisticsIn.length; ++i )
		{
			if ( ! (statisticsIn[ i ] instanceof MapStatistic ) )
			{
				convert	= true;
				break;
			}
		}
		
		Statistic[]		statisticsOut	= null;
		if ( convert )
		{
			statisticsOut	= new Statistic[ statisticsIn.length ];
			
			for( int i = 0; i <
			 statisticsIn.length; ++i )
			{
				statisticsOut[ i ]	= requireStatisticImpl( statisticsIn[ i ] );
			}
		}
		else
		{
			statisticsOut	= statisticsIn;
		}
		
		return( statisticsOut );
	
public java.lang.StringtoString()

		final StringBuffer	buf	= new StringBuffer();
		
		final Statistic[]	statistics	= getStatistics();
		buf.append( statistics.length + " Statistics:\n" );
		for( int i = 0; i < statistics.length; ++i )
		{
			buf.append( statistics[ i ].toString() + "\n");
		}
		
		return( buf.toString() );