FileDocCategorySizeDatePackage
OpenMBeanUtil.javaAPI DocGlassfish v2 API12040Fri May 04 22:31:04 BST 2007com.sun.appserv.management.util.jmx

OpenMBeanUtil

public final class OpenMBeanUtil extends Object
Utilities dealing with OpenMBeans

Fields Summary
private static Map
SIMPLETYPES_MAP
private static final String[]
EMPTY_STRING_ARRAY
private static final OpenType[]
EMPTY_OPENTYPES
Constructors Summary
private OpenMBeanUtil()

Methods Summary
public static java.util.MapcompositeDataToMap(javax.management.openmbean.CompositeData data)
Convert a CompositeData to a Map.

		final Map<String,Serializable>		map	= new HashMap<String,Serializable>();
		final CompositeType		type	= data.getCompositeType();
		final Set<String>   keySet  = TypeCast.asSet( type.keySet() );
		
		for( String name : keySet )
		{
			map.put( name, (Serializable)data.get( name ) );
		}
		
		return( map );
	
public static java.util.MapconvertTypes(java.util.Map orig)
Convert certain types and return a new Map:
  • Collection converts to an array
Nulls are not eliminated; use {@link com.sun.appserv.management.util.misc.MapUtil#newMapNoNullValues} to do that before or after.

		final Map<String,Serializable>
		    result	= new HashMap<String,Serializable>();
		
		for( final String key : orig.keySet() )
		{
			final Serializable	value	= (Serializable)orig.get( key );
			
			if ( value instanceof Collection )
			{
				Object[]	newValue	=
					IteratorUtil.toArray( ((Collection<Serializable>)value).iterator() );
				newValue	= ArrayConversion.specializeArray( newValue );
				
				result.put( key, newValue );
			}
			else
			{
				result.put( key, value );
			}
		}
		
		
		return( result );
	
private static java.lang.ObjectgetAnyArrayElement(java.lang.Object o)
Get any non-null array element from the array.

	

	
			       	 
		  
	   
	
		Object	result	= null;
	
		final int	length	= Array.getLength( o );
		if ( length != 0 )
		{
			for( int i = 0; i < length; ++i )
			{
				final Object	element	= Array.get( o, 0 );
				
				if ( element != null )
				{
					if ( element.getClass().isArray() )
					{
						result	= getAnyArrayElement( element );
						if ( result != null )
							break;
					}
					else
					{
						result	= element;
						break;
					}
				}
			}
		}
		
		return( result );
	
private static intgetArrayDimensions(java.lang.Class theClass)

		final String	classname	= theClass.getName();
		
		int	dim	= 0;
		while ( classname.charAt( dim ) == '[" )
		{
			++dim;
		}
		
		return( dim );
	
public static javax.management.openmbean.OpenTypegetOpenType(java.lang.Object o)
Get the OpenType of an Object, which must conform to OpenType requirements.

		if ( o == null )
		{
			// no OpenType for a null
			throw new IllegalArgumentException();
		}
		
		OpenType	type	= getSimpleType( o.getClass() );
		
		if ( type == null )
		{
			final Class	theClass	= o.getClass();
			
			if ( theClass.isArray() )
			{
				final int		length		= Array.getLength( o );
				final int		dimensions	= getArrayDimensions( theClass );
				final Class		elementClass	= theClass.getComponentType();
				
				final SimpleType	simpleType	= getSimpleType( elementClass );
				if ( simpleType != null )
				{
					type	= new ArrayType( dimensions, simpleType );
				}
				else
				{
					final Object	element	= getAnyArrayElement( o );
					
					if ( CompositeData.class.isAssignableFrom( elementClass ) )
					{
						if ( element == null )
						{
							type	= SimpleType.VOID;
						}
						else
						{
							type	= new ArrayType( dimensions, ((CompositeData)element).getCompositeType() );
						}
					}
					else if ( TabularData.class.isAssignableFrom( elementClass ) )
					{
						if ( element == null )
						{
							type	= SimpleType.VOID;
						}
						else
						{
							type	= new ArrayType( dimensions, ((TabularData)element).getTabularType() );
						}
					}
				}
				
			}
			else if ( o instanceof CompositeData )
			{
				type	= ((CompositeData)o).getCompositeType();
			}
			else if ( o instanceof TabularData )
			{
				type	= ((TabularData)o).getTabularType();
			}
		}
		
		if ( type == null )
		{
			throw new IllegalArgumentException( o.getClass().getName() );
		}
		
		return( type );
	
public static javax.management.openmbean.SimpleTypegetSimpleType(java.lang.Class c)
Get the SimpleType for a class which can be so-represented.

		final SimpleType	type	= (SimpleType)(getSimpleTypesMap().get( c ));
		
		return( type );

	
private static java.util.MapgetSimpleTypesMap()

		  
	
	
		if ( SIMPLETYPES_MAP == null )
		{
			final Map<Class,SimpleType> m	= new HashMap<Class,SimpleType>();
			
			m.put( Byte.class, SimpleType.BYTE );
			m.put( Short.class, SimpleType.SHORT );
			m.put( Integer.class, SimpleType.INTEGER );
			m.put( Long.class, SimpleType.LONG );
			m.put( BigInteger.class, SimpleType.BIGINTEGER );
			m.put( BigDecimal.class, SimpleType.BIGDECIMAL );
			m.put( Float.class, SimpleType.FLOAT );
			m.put( Double.class, SimpleType.DOUBLE );
			
			m.put( Character.class, SimpleType.CHARACTER );
			m.put( Boolean.class, SimpleType.BOOLEAN );
			m.put( String.class, SimpleType.STRING );
			m.put( Date.class, SimpleType.DATE );
			m.put( Void.class, SimpleType.VOID );
			
			m.put( ObjectName.class, SimpleType.OBJECTNAME );
			
			SIMPLETYPES_MAP	= m;
		}
		
		return( SIMPLETYPES_MAP );
	
public static javax.management.openmbean.OpenTypegetStackTraceElementOpenType()
Get a CompositeType describing a CompositeData which has no elements.

		final String[]	itemNames	= new String[]
		{
			"ClassName",
			"FileName",
			"LineNumber",
			"IsNativeMethod",
		};
		
		final String[]	descriptions	= new String[  ]
		{
			"ClassName",
			"FileName",
			"LineNumber",
			"IsNativeMethod",
		};
		
		final OpenType[]	openTypes	= new OpenType[ itemNames.length ];
		openTypes[ 0 ]	= SimpleType.STRING;
		openTypes[ 1 ]	= SimpleType.STRING;
		openTypes[ 2 ]	= SimpleType.INTEGER;
		openTypes[ 3 ]	= SimpleType.BOOLEAN;
		
		return( new CompositeType(
			StackTraceElement.class.getName(), 
			"StackTraceElement composite type",
			itemNames,
			descriptions,
			openTypes
			) );
	
public static javax.management.openmbean.OpenTypegetThrowableOpenType(java.lang.Throwable t)
Get a CompositeType describing a CompositeData which has no elements.

		final String[]	itemNames	= new String[]
		{
			"Message",
			"Cause",
			"StackTrace",
		};
		
		final String[]	descriptions	= new String[  ]
		{
			"The message from the Throwable",
			"The cause (if any) from the Throwable",
			"The stack trace from the Throwable",
		};
		
		final OpenType[]	openTypes	= new OpenType[ itemNames.length ];
		
		openTypes[ 0 ]	= SimpleType.STRING;
		openTypes[ 1 ]	= t.getCause() == null ? SimpleType.VOID : getThrowableOpenType( t.getCause() );
		openTypes[ 2 ]	= new ArrayType( t.getStackTrace().length,
							getStackTraceElementOpenType() );
		
		
		return( new CompositeType(
			t.getClass().getName(), 
			"Throwable composite type",
			itemNames,
			descriptions,
			openTypes
			) );
	
public static javax.management.openmbean.CompositeDatamapToCompositeData(java.lang.String typeName, java.lang.String description, java.util.Map map)
Create a CompositeData from a Map. Each key in the map must be a String, and each value must be a type consistent with OpenTypes.

param
typeName the arbitrary name of the OpenType to be used
param
description the arbitrary description of the OpenType to be used
param
map a Map keyed by String, whose values may not be null

		final CompositeType	type	= mapToCompositeType( typeName, description, map, null);
		
		return( new CompositeDataSupport( type, map ) );
	
public static javax.management.openmbean.CompositeTypemapToCompositeType(java.lang.String typeName, java.lang.String description, java.util.Map map, CompositeTypeFromNameCallback callback)
Create a CompositeType from a Map. Each key in the map must be a String, and each value must be a type consistent with OpenTypes.

param
typeName the arbitrary name of the OpenType to be used
param
description the arbitrary description of the OpenType to be used
param
map a Map keyed by String, whose values may not be null

		final String[]		itemNames	= new String[ map.keySet().size()];
		map.keySet().toArray( itemNames );
		
		final String[]		itemDescriptions	= new String[ itemNames.length ];
		final OpenType[]	itemTypes			= new OpenType[ itemNames.length ];
		
		for( int i = 0; i < itemNames.length; ++i )
		{
		    final String    name    = itemNames[ i ];
			final Object	value	= map.get( name );
			
			itemDescriptions[ i ]	= "value " + name;
			if ( value == null )
			{
				// force nulls to type String
				itemTypes[ i ]	= callback.getOpenTypeFromName( name );
			}
			else
			{
				itemTypes[ i ]			= getOpenType( value );
			}
		}
		
		final CompositeType		type	= new CompositeType(
								typeName,
								description,
								itemNames,
								itemDescriptions,
								itemTypes );
	
		return( type );