FileDocCategorySizeDatePackage
J2EEUtil.javaAPI DocGlassfish v2 API12769Fri May 25 13:24:12 BST 2007com.sun.appserv.management.util.j2ee

J2EEUtil

public class J2EEUtil extends Object
J2EE JSR 77 utilities

Fields Summary
private static final String
GET
private static final Set
IGNORE
private static final Class[]
STATISTIC_CLASSES
private static final Class[]
STATS_CLASSES
Constructors Summary
private J2EEUtil()

	
			
Methods Summary
public static java.lang.StringgetStateManageableString(int state)

return
String equivalent of a {@link StateManageable} state

        String s = null;
        
        if ( state == StateManageable.STATE_STARTING )
        {
            s   = "STATE_STARTING";
        }
        else if ( state == StateManageable.STATE_RUNNING )
        {
            s   = "STATE_RUNNING";
        }
        else if ( state == StateManageable.STATE_STOPPING )
        {
            s   = "STATE_STOPPING";
        }
        else if ( state == StateManageable.STATE_STOPPED )
        {
            s   = "STATE_STOPPED";
        }
        else if ( state == StateManageable.STATE_FAILED )
        {
            s   = "STATE_FAILED";
        }
        else
        {
            throw new IllegalArgumentException( "" + state );
        }
        return s;
    
public static java.lang.reflect.Method[]getStatisticGetterMethodsUsingIntrospection(Stats stats)

		final Method[]		candidates		= stats.getClass().getMethods();
		
		final List<Method>	results	= new ArrayList<Method>();
		for( int methodIdx = 0; methodIdx < candidates.length; ++methodIdx )
		{
			final Method	method	= candidates[ methodIdx ];
			final String	methodName	= method.getName();
			
			final Class	returnType	= method.getReturnType();
			
			if ( JMXUtil.isGetter( method ) &&
				Statistic.class.isAssignableFrom( returnType ) &&
				method.getParameterTypes().length == 0 )
			{
				results.add( method );
			}
		}
	
		return (Method[])results.toArray( new Method[ results.size() ] );
	
public static java.lang.reflect.Method[]getStatisticGetterMethodsUsingNames(Stats stats)

		final String[]	statisticNames	= stats.getStatisticNames();
		
		final Class			statsClass	= stats.getClass();
		final Method[]		methods		= new Method[ statisticNames.length ];
		
		final Set<String>   missing = new HashSet<String>();
		
		for( int i = 0; i < statisticNames.length; ++i )
		{
	        //System.out.print( statisticNames[i ] + ", " );
	        final String methodName = JMXUtil.GET + statisticNames[ i ];
	        try
	        {
			methods[ i ]	= statsClass.getMethod( methodName, (Class[])null );
			}
			catch( final NoSuchMethodException e )
			{
			    missing.add( methodName );
			}
		}
		
		if ( missing.size() != 0 )
		{
		    throw new NoSuchMethodException(
		        "Missing methods: in object of class " + stats.getClass().getName() +
		        ": " + CollectionUtil.toString( missing, ", ") );
	    }
	    
		return( methods );
	
public static java.lang.StringgetStatisticType(Statistic statistic)
Get the type name for a Statistic.

		String	type	= getType( STATISTIC_CLASSES, statistic );
		
		if ( type == null )
		{
			type	= statistic.getClass().getName();
		}

		return( type );
	
public static java.lang.StringgetStatsType(Stats stats)
Get the type for use in a {@link CompositeType} type.

	
			         	 
		  
	   
	
		String	type	= getType( STATS_CLASSES, stats );
		
		if ( type == null )
		{
			type	= stats.getClass().getName();
		}
		
		return( type );
	
private static java.lang.StringgetType(java.lang.Class[] classes, java.lang.Object o)
Get the type matching the object.

		String	type	= null;
		
		for( int i = 0; i < classes.length; ++i )
		{
			final Class	theClass	= classes[ i ];
			
			if ( theClass.isInstance( o ) )
			{
				type	= theClass.getName();
				break;
			}
		}
		return( type );
	
private static javax.management.openmbean.CompositeTypestatisticMapToCompositeType(java.lang.String statisticType, java.util.Map map)

	
	
	
		  
	       
		 
	
		final String	description	= "J2EE management statistic " + statisticType;
		
		return( OpenMBeanUtil.mapToCompositeType( statisticType, description, map, null) );
	
public static javax.management.openmbean.CompositeDataSupportstatisticToCompositeData(Statistic statistic)
Convert a Statistic into a JMX CompositeDataSupport.

param
statistic any Statistic or subclass

		final String			statisticType	= getStatisticType( statistic );
		
		final Map<String,Object>    map = statisticToMap( statistic );
	
		final CompositeType		type	= statisticMapToCompositeType( statisticType, map );
		
		return( new CompositeDataSupport( type, map ) );
	
public static javax.management.openmbean.CompositeTypestatisticToCompositeType(Statistic statistic)
Get the JMX OpenMBean CompositeType corresponding to the Statistic.

		final String	statisticType	= getStatisticType( statistic );
		final Map<String,Object>    map = statisticToMap( statistic );
		
		return( statisticMapToCompositeType( statisticType, map ) );
	
public static java.util.MapstatisticToMap(Statistic statistic)
Convert the Statistic into a Map containing all the name/value pairs obtained by finding method names that match a getter pattern getXXX(). Any Statistic can thus be converted into a Map which preserves all its members.

param
statistic any Statistic or subclass

	
			          		         		            		  				     	 
		  
	   
	
		final Map<String,Object>	result	= new HashMap<String,Object>();
		
		if ( statistic instanceof MapStatistic )
		{
			result.putAll( ((MapStatistic)statistic).asMap() );
		}
		else
		{
			// get all public methods
			final Method[]	methods	= statistic.getClass().getMethods();

			for( int i = 0; i < methods.length; ++i )
			{
				final Method	method		= methods[ i ];
				final String	methodName	= method.getName();
				
				if ( methodName.startsWith( GET ) &&
						! IGNORE.contains( methodName ) &&
						method.getParameterTypes().length == 0 &&
						method.getExceptionTypes().length == 0 &&
						method.getReturnType() != Void.class )
				{
					try
					{
						final Object	value = method.invoke( statistic, (Object[])null );
						
						final String name	= methodName.substring( GET.length(), methodName.length() );
						result.put( name, value );
					}
					catch( Exception e )
					{
						// ignore
					}
				}
			}
		}

		return( result );
	
public static java.util.MapstatisticsToMap(Statistic[] statistics)
Convert a Statistic[] into a Map keyed by the name of the Statistic, with value being the Statistic.

return
Map keyed by name of the Statistic

		final Map<String,Statistic> m	= new HashMap<String,Statistic>();
		
		for( int i = 0; i < statistics.length; ++i )
		{
			final String	name	= statistics[ i ].getName();
			m.put( name, statistics[ i ] );
		}
		
		return( m );
	
public static javax.management.openmbean.CompositeDataSupportstatsToCompositeData(Stats stats)
Convert a Stats object into a CompositeDataSupport, which is a standard type in JMX which can be serialized.

The CompositeData entries will keyed by the name of the Statistic. The value of each Statistic will also be a CompositeData, keyed by the value names found in that statistic.

For example, the JVMStats object would be converted into a CompositeData with the entries:

  • HeapSize
    • Description
    • LastSampleTime
    • Name
    • StartTime
    • Unit
    • LowerBound
    • UpperBound
    • HighWaterMark
    • LowWaterMark
    • Current
  • UpTime
    • Description
    • LastSampleTime
    • Name
    • StartTime
    • Unit
    • Count

param
stats any JSR 77 Stats object

		final String			statsType	= getStatsType( stats );
		
		final Statistic[]		statistics	= stats.getStatistics();
		final String[]			names			= new String[ statistics.length ];
		final CompositeData[]	datas			= new CompositeData[ names.length ];
		final String[]			itemDescriptions	= new String[ names.length ];
		final CompositeType[]	itemTypes			= new CompositeType[ names.length ];
		
		for( int i = 0; i < names.length; ++i )
		{
			names[ i ]				= statistics[ i ].getName();
			datas[ i ]				= statisticToCompositeData( statistics[ i ] );
			itemTypes[ i ]			= datas[ i ].getCompositeType();
			itemDescriptions[ i ]	= statistics[ i ].getName();
		}
		
		final CompositeType	type	= new CompositeType(
				statsType,
				"CompositeData for " + statsType,
				names,
				itemDescriptions, 
				itemTypes );
		
		return( new CompositeDataSupport( type, names, datas ) );
	
public static java.util.MapstatsToMap(Stats stats)
Turn a Stats object into a Map containing all its Statistics, keyed by Statistic.getName() and value of the Statistic.

		final Statistic[]		statistics	= stats.getStatistics();
		
		return( statisticsToMap( statistics ) );