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

MBeanGenerator

public class MBeanGenerator extends Object implements MBeanGeneratorHook
Generate an MBean ".java" file.

Fields Summary
boolean
mEmitComments
Map
mCounts
com.sun.appserv.management.util.jmx.AttributeNameMapper
mMapper
private static final String
TAB
private static final String
NEWLINE
private static final String
PARAM_DELIM
public static final String
FINAL_PREFIX
public static final int
IMPORT_THRESHOLD
private static final String
BRACKETS
private static int
sCounter
Constructors Summary
public MBeanGenerator()

		mCounts			= null;
		mEmitComments	= true;
	
Methods Summary
private java.util.MapcountAllTypes(javax.management.MBeanInfo info)

		final Map<String,Integer>	counts	= new HashMap<String,Integer>();
		final MBeanAttributeInfo[]	attrInfos		= info.getAttributes();
		final MBeanOperationInfo[]	operationInfos	= info.getOperations();
		if ( attrInfos != null )
		{
			countTypes( counts, attrInfos );
		}
		if ( operationInfos != null )
		{
			countTypes( counts, operationInfos );
		}
		
		return( counts );
	
private static voidcountType(java.util.Map counts, java.lang.String typeIn)

		final String	type	= stripBrackets( ClassUtil.getFriendlyClassname( typeIn ) );
		
		Integer	count	= counts.get( type );
		if ( count == null )
		{
			count	= new Integer( 1 );
		}
		else
		{
			count	= new Integer( count.intValue() + 1 );
		}
		
		counts.put( type, count );
	
public static voidcountTypes(java.util.Map counts, javax.management.MBeanAttributeInfo[] infos)
Count how many times an Attribute type is used.

		for( int i = 0; i < infos.length; ++i )
		{
			countType( counts, infos[ i ].getType() );
		}
	
private static voidcountTypes(java.util.Map counts, javax.management.MBeanOperationInfo[] infos)
Count how many times the return type and parameter types are used.

		for( int i = 0; i < infos.length; ++i )
		{
			countType( counts, infos[ i ].getReturnType() );
			
			final MBeanParameterInfo[]	params	= infos[ i ].getSignature();
			for( int p = 0; p < params.length; ++p )
			{
				countType( counts, params[ p ].getType() );
			}
		}
	
protected java.lang.StringformMethod(java.lang.String returnType, java.lang.String name, java.lang.String[] params, java.lang.String[] names)

		final String	begin	= "public " + getCodeClassname( returnType ) + TAB + name + "(";
		String			paramsString	= "";
		if ( params != null && params.length != 0 )
		{
			final StringBuffer	buf	= new StringBuffer();
			
			buf.append( " " );
			for( int i = 0; i < params.length; ++i )
			{
				buf.append( FINAL_PREFIX );
				buf.append( getCodeClassname( params[ i ] ) );
				buf.append( " " + names[ i ] );
				buf.append( PARAM_DELIM );
			}
			
			buf.setLength( buf.length() - PARAM_DELIM.length() );	// strip last ","
			buf.append( " " );
			paramsString	= buf.toString();
		}
		
		
		return( begin + paramsString + ");" );
	
public java.lang.Stringgenerate(javax.management.MBeanInfo info, boolean emitComments)

		mEmitComments	= emitComments;
		
		final StringBuffer	buf	= new StringBuffer();
		
		if ( mEmitComments )
		{
			buf.append( getHeaderComment( info ) + NEWLINE + NEWLINE );
		}
		
		buf.append( "package " + getPackageName( info ) + ";" + NEWLINE );
		
		mCounts	= countAllTypes( info );
		buf.append( NEWLINE + getImportBlock( mCounts ) + NEWLINE );
		
		if ( mEmitComments )
		{
			buf.append( getInterfaceComment( info ) + NEWLINE + NEWLINE );
		}
		String	interfaceName	= getClassname( info );
		buf.append( "public interface " + interfaceName + " \n{\n" );
		
		
		final MBeanAttributeInfo[]	attrInfos		= info.getAttributes();
		final MBeanOperationInfo[]	operationInfos	= info.getOperations();
		if ( attrInfos != null )
		{
			Arrays.sort( attrInfos, MBeanAttributeInfoComparator.INSTANCE );
			buf.append( generateAttributes( attrInfos ) );
		}
		if ( operationInfos != null )
		{
			if ( operationInfos.length != 0 )
			{
				Arrays.sort( operationInfos, MBeanOperationInfoComparator.INSTANCE );
			
				buf.append( NEWLINE + "// -------------------- Operations --------------------" + NEWLINE );
				buf.append( generateOperations( operationInfos ) );
			}
		}
		
		
		buf.append( "\n}" );
		
		return( buf.toString() );
	
protected java.lang.StringgenerateAttributes(javax.management.MBeanAttributeInfo[] infos)

		final StringBuffer	buf	= new StringBuffer();
		
		final String[]	typeTemp	= new String[ 1 ];
		final String[]	nameTemp	= new String[ 1 ];
		
		mMapper	= new AttributeNameMapperImpl( JMXUtil.getAttributeNames( infos ) );
		
		for( int i = 0; i < infos.length; ++i )
		{
			final MBeanAttributeInfo	info	= infos[ i ];
			
			final String	attributeName	= info.getName();
			final String	type			= info.getType();
			String	comment	= "";
			
			final String	javaName	= mMapper.originalToDerived( attributeName );
			
			if ( info.isReadable() )
			{
				if ( mEmitComments )
				{
					comment	= getGetterComment( info, javaName );
					if ( comment.length() != 0 )
					{
						buf.append( indent( comment )  + NEWLINE );
					}
				}
				buf.append( indent( formMethod( type, "get" + javaName, null, null) ) );
			}
			
			if ( info.isWritable() )
			{
				buf.append( NEWLINE );
				if ( mEmitComments )
				{
					comment	= getSetterComment( info, javaName );
					if ( comment.length() != 0 )
					{
						buf.append( indent( comment )  + NEWLINE );
					}
				}
				
				typeTemp[ 0 ]	= type;
				nameTemp[ 0 ]	= "value";
				
				buf.append( indent( formMethod( "void", "set" + javaName, typeTemp, nameTemp ) ) );
			}
			
			buf.append( NEWLINE + NEWLINE  );
		}
		
		return( buf.toString() );
	
protected java.lang.StringgenerateOperations(javax.management.MBeanOperationInfo[] infos)

		final StringBuffer	buf	= new StringBuffer();
		
		for( int i = 0; i < infos.length; ++i )
		{
			final MBeanOperationInfo	info	= infos[ i ];
			final String	name		= info.getName();
			final String	returnType	= info.getReturnType();
			final MBeanParameterInfo[]	paramInfos	= info.getSignature();
			final int		impact		= info.getImpact();
			
			final String[]	paramTypes	= new String[ paramInfos.length ];
			for( int p = 0; p < paramInfos.length; ++p )
			{
				paramTypes[ p ]	= paramInfos[ p ].getType();
			}
			
			final String[]	paramNames	= getParamNames( info );
			
			if ( mEmitComments )
			{
				final String comment	= getOperationComment( info, paramNames );
				if ( comment.length() != 0 )
				{
					buf.append( NEWLINE + indent( comment )  + NEWLINE );
				}
			}
			
			final String method	= formMethod( returnType, name, paramTypes, paramNames );
			buf.append( indent( method ) + NEWLINE );
		}
		
		return( buf.toString() );
	
protected java.lang.StringgetAttributeNameComment(java.lang.String attributeName, java.lang.String javaName)
Return a comment regarding the Attribute name if it was mapped to a different Java name.

		String	comment	= "";
		
		if ( ! attributeName.equals( javaName ) )
		{

		}
		return( comment );
	
public java.lang.StringgetClassname(javax.management.MBeanInfo info)

		// mangle the ObjectName into a class name
		return( "Interface" + getCounter() );
	
java.lang.StringgetCodeClassname(java.lang.String classname)

		String	name	= ClassUtil.getFriendlyClassname( classname );
		
		if ( typeMayBeAbbreviated( name ) )
		{
			name	= ClassUtil.stripPackagePrefix( name );
		}
		
		return( name );
	
private static final synchronized intgetCounter()

		    
	
	
		return( sCounter++ );
	
public java.lang.StringgetExceptions(javax.management.MBeanOperationInfo info)

		return( "" );
	
public java.lang.StringgetGetterComment(javax.management.MBeanAttributeInfo info, java.lang.String actualName)

		return( getGetterSetterComment( info, actualName ) );
	
public java.lang.StringgetGetterSetterComment(javax.management.MBeanAttributeInfo info, java.lang.String actualName)

		String	description	= info.getDescription() == null ? "" : info.getDescription();
		if ( isBoilerplateDescription( description ) )
		{
			description	= "";
		}
		
		final String	nameComment	= getAttributeNameComment( info.getName(), actualName );
		String			result	= null;
		
		if ( description.length() == 0 && nameComment.length() == 0 )
		{
			result	= "";
		}
		else
		{
			result	= description;
			if ( nameComment.length() != 0 )
			{
				if ( description.length() != 0 )
				{
					result	= result + NEWLINE;
				}
				result	= result + nameComment;
			}
			
			result	= makeJavadocComment( result );
		}

		return( result );
	
public java.lang.StringgetHeaderComment(javax.management.MBeanInfo info)

		return( makeJavadocComment( "" ) );
	
private java.lang.StringgetImportBlock(java.util.Map counts)

		final StringBuffer	buf		= new StringBuffer();
		final Iterator		iter	= counts.keySet().iterator();
		
		while ( iter.hasNext() )
		{
			final String	key	= (String)iter.next();
			final Integer	count	= (Integer)counts.get( key );
			
			// if used twice or more, generate an import statement
			if ( count.intValue() >= IMPORT_THRESHOLD && ! isUnqualifiedType( key ) )
			{
				buf.append( "import " + key + ";" + NEWLINE );
			}
		}
		
		return( buf.toString() );
	
public java.lang.StringgetInterfaceComment(javax.management.MBeanInfo info)

		final String	comment	= "Implementing class was: " + info.getClassName();
		
		return( makeJavadocComment( comment ) );
	
public java.lang.StringgetOperationComment(javax.management.MBeanOperationInfo info, java.lang.String[] paramNames)

		final String	description	= info.getDescription();
		
		if ( description == null || isBoilerplateDescription( description ) )
		{
			return( "" );
		}
		
		final StringBuffer	buf	= new StringBuffer();
		
		final MBeanParameterInfo[]	signature	= info.getSignature();
		for( int i = 0; i < paramNames.length; ++i )
		{
			final String paramDescription	= signature[i].getDescription();
			
			buf.append( "@param " + paramNames[ i ] + TAB + paramDescription + NEWLINE );
		}
		
		final String	returnType	= getCodeClassname( info.getReturnType() );
		if ( ! returnType.equals( "void" ) )
		{
			buf.append( "@return " + returnType + NEWLINE );
		}

		return( makeJavadocComment( buf.toString() ) );
	
public java.lang.StringgetPackageName(javax.management.MBeanInfo info)

		return( "mbeans" );
	
public java.lang.String[]getParamNames(javax.management.MBeanOperationInfo info)

		final MBeanParameterInfo[]	params	= info.getSignature();
		
		final String[]	names	= new String[ params.length ];
		
		for( int i = 0; i < params.length; ++i )
		{
			names[ i ]	= params[ i ].getName();
		}
		
		return( names );
	
public java.lang.StringgetSetterComment(javax.management.MBeanAttributeInfo info, java.lang.String actualName)

		return( getGetterSetterComment( info, actualName ) );
	
protected java.lang.Stringindent(java.lang.String contents, java.lang.String prefix)

		final StringBuffer	buf	= new StringBuffer();
		if ( contents.length() != 0 )
		{
			final String[]		lines	= contents.split( NEWLINE );
			
			for( int i = 0; i < lines.length; ++i )
			{
				buf.append( prefix + lines[ i ] + NEWLINE);
			}
			
			buf.setLength( buf.length() - 1 );
		}
		
		return( buf.toString() );
	
protected java.lang.Stringindent(java.lang.String contents)

		return( indent( contents, TAB ) );
	
protected booleanisBoilerplateDescription(java.lang.String description)

		return( description == null || description.length() == 0 ||
			description.indexOf( "Attribute exposed for management" ) >= 0 ||
			description.indexOf( "Operation exposed for management" ) >= 0 ||
			description.indexOf( "No Description was available" ) >= 0);
	
protected booleanisUnqualifiedType(java.lang.String type)

		return( type.indexOf( "." ) < 0 );
	
protected java.lang.StringmakeJavadocComment(java.lang.String contents)

		return( "/**" + NEWLINE + indent( contents ) + NEWLINE + "*/" );
	
static java.lang.StringstripBrackets(java.lang.String name)

	
		 
	   
	
		String result	= name;
		
		while( result.endsWith( BRACKETS ) )
		{
			result	= result.substring( 0, result.length() - BRACKETS.length() );
		}
		
		return( result );
	
protected booleantypeMayBeAbbreviated(java.lang.String type)
type must be the "friendly" name.

		final Integer	count	= mCounts.get( type );
		if ( count == null )
		{
			return( false );
		}
		
		return( count.intValue() >= IMPORT_THRESHOLD );