FileDocCategorySizeDatePackage
CmdBase.javaAPI DocGlassfish v2 API11176Fri May 04 22:24:48 BST 2007com.sun.cli.jmx.cmd

CmdBase

public abstract class CmdBase extends Object implements CmdOutput, com.sun.cli.util.DebugState, Cmd

Fields Summary
public static final String
ENV_TOKENS
public static final String
ENV_CMD_RUNNER
public static final String
ENV_CMD_FACTORY
public static final String
ENV_COMMANDS
public static final String
ENV_DEBUG
private final CmdEnv
mEnv
final String[]
mTokens
private com.sun.cli.jmx.cmd.ArgHelper
mArgHelper
private CmdOutput
mOutput
com.sun.cli.util.stringifier.Stringifier
mStringifier
private static final Class[]
EMPTY_SIG
private static final Object[]
EMPTY_ARGS
Constructors Summary
CmdBase(CmdEnv env)

		mEnv		= env;
		
		mOutput	= new CmdOutputImpl( System.out, System.err );
		
		mTokens		= (String [])env.get( "TOKENS" );
		
		// can't instantiate here; need the result of virtual method getOptionInfo()
		mArgHelper	= null;
	
Methods Summary
intcountOptions()

		return( getArgHelper().countOptions( ) );
	
voiddm(java.lang.Object o)

	
	// debug message
		
	   
	
		mOutput.println( mStringifier.stringify( o ) );
	
java.lang.ObjectenvGet(java.lang.String key)

		return( mEnv.get( key ) );
	
java.lang.ObjectenvGet(java.lang.String key, java.lang.Object defaultValue)

		Object value	= envGet( key );
		
		if ( value == null )
		{
			value	= defaultValue;
		}

		return( value );
	
voidenvPut(java.lang.String key, java.lang.Object value, boolean allowPersistence)

		mEnv.put( key, value, allowPersistence);
	
voidenvRemove(java.lang.String key)

		mEnv.remove( key );
	
public final voidexecute()

		try
		{
			preExecute();
		
			if ( getBoolean( "help", Boolean.FALSE ).booleanValue() ||
					getBoolean( "h", Boolean.FALSE ).booleanValue() )
			{
				printUsage();
			}
			else
			{
				executeInternal( );
			}
		}
		catch( Exception e )
		{
			handleException( e );
			e.printStackTrace();
		}
	
abstract voidexecuteInternal()

java.lang.StringgetAlsoKnownAs(java.lang.Class theClass)

		String	aka	= "";
		
		try
		{
			aka	= ArrayStringifier.stringify( getCmdNames( theClass ), " " );
		}
		catch( Exception e )
		{
			// Hmmm..
		}
		
		return( "Also known as: " + aka );
	
com.sun.cli.jmx.cmd.ArgHelpergetArgHelper()

		assert( mArgHelper != null );
		
		return( mArgHelper );
	
java.lang.BooleangetBoolean(java.lang.String name, java.lang.Boolean defaultValue)

		return( getArgHelper().getBoolean( name, defaultValue ) );
	
CmdFactorygetCmdFactory()

		return( (CmdFactory)envGet( ENV_CMD_FACTORY ) );
	
java.lang.StringgetCmdNameAsInvoked()

		return( mTokens[ 0 ] );
	
public static java.lang.String[]getCmdNames(java.lang.Class theClass)

	
		   
	    
		 
			 
	
		String []	names	= null;
		
		final java.lang.reflect.Method	m	= theClass.getDeclaredMethod( "getNames", EMPTY_SIG );
	
		names	= (String [])m.invoke( theClass, EMPTY_ARGS);
		
		return( names );
	
public booleangetDebug()

		boolean	isDebug	= false;
		final String	value	= (String)envGet( ENV_DEBUG );
		if ( value != null )
		{
			isDebug	=	value.equalsIgnoreCase( "true" ) ||
						value.equalsIgnoreCase( "t" ) ||
						value.equalsIgnoreCase( "yes" )||
						value.equalsIgnoreCase( "y" );
		}
		return( isDebug );
	
java.util.SetgetEnvKeys()

		return( mEnv.getKeys() );
	
java.util.SetgetEnvKeys(java.lang.String regExp)

		final Iterator	iter	= getEnvKeys().iterator();
		final java.util.Set		filteredSet	= new java.util.HashSet();
		
		final Pattern	 pattern	= Pattern.compile( regExp );
		
		while ( iter.hasNext() )
		{
			final String	key	= (String)iter.next();
			
			final Matcher	m	= pattern.matcher( key );
			
			if ( m.matches() )
			{
				filteredSet.add( key );
			}
			
		}

		return( filteredSet );
	
java.lang.IntegergetInteger(java.lang.String name)

		return( getArgHelper().getInteger( name ) );
	
intgetNumRequiredOperands()

		// require 1, by default
		return( 1 );
	
java.lang.String[]getOperands()

		return( getArgHelper().getOperands( ) );
	
ArgHelper.OptionsInfogetOptionInfo()

		// default to none
		return( new ArgHelperOptionsInfo( ) );
	
java.lang.StringgetString(java.lang.String name, java.lang.String defaultValue)

		return( getArgHelper().getString( name, defaultValue ) );
	
abstract java.lang.StringgetUsage()

voidhandleException(java.lang.Exception e)

		if ( e instanceof WrongNumberOfOperandsException )
		{
			// already reported
		}
		else if ( e instanceof ArgHelper.IllegalOptionException )
		{
			printError( "ERROR: " + e.getMessage() );
			printUsage();
		}
		else if ( e instanceof java.io.IOException)
		{
			printError( "ERROR: " + e.getMessage() );
		}
		else
		{
			printError( "ERROR: exception of type: " +
				e.getClass().getName() + ", msg = " + e.getMessage() );
			e.printStackTrace();
		}
	
voidinitArgHelper()

		if ( mArgHelper == null )
		{
			final java.util.ListIterator	iter	=
				Arrays.asList( mTokens ).listIterator( 1 );
			
			final ArgHelper.OptionsInfo		optionsInfo	= getOptionInfo();
			
			mArgHelper	= new ArgHelperImpl( iter, new OptionsInfoWrapper( optionsInfo ) );
		}
	
voidpreExecute()

		initArgHelper();
		requireNumOperands( getNumRequiredOperands() );
	
public voidprint(java.lang.Object o)

		mOutput.print( o );
	
public voidprintDebug(java.lang.Object o)

		mOutput.printDebug( o );
	
public voidprintError(java.lang.Object o)

		mOutput.printError( o );
	
voidprintUsage()

		println( getUsage() );
		
		
		println( getAlsoKnownAs( this.getClass() ) );
	
public voidprintln(java.lang.Object o)

		mOutput.println( o );
	
voidrequireNumOperands(int numRequiredOperands)

		final String []	operands	= getOperands();
		
		if ( operands.length < numRequiredOperands )
		{
			requireNumOperandsFailed( operands.length, numRequiredOperands, null);
		}
	
voidrequireNumOperands(int numRequiredOperands, java.lang.String msg)

		final String []	operands	= getOperands();
		
		if ( operands.length < numRequiredOperands )
		{
			requireNumOperandsFailed( operands.length, numRequiredOperands, msg);
		}
	
voidrequireNumOperandsFailed(int numSupplied, int numRequired, java.lang.String msg)

		printError( "ERROR: " + numRequired + " arguments required, " + numSupplied + " supplied" );
		if ( msg != null )
		{
			println( msg );
		}
		else
		{
			printUsage();
		}
		throw new WrongNumberOfOperandsException( "illlegal number of operands" );