1 23 24 29 30 31 package com.sun.cli.jmx.cmd; 32 33 import java.util.HashMap ; 34 import java.util.Arrays ; 35 import java.util.Iterator ; 36 37 import java.util.regex.Pattern ; 38 import java.util.regex.Matcher ; 39 40 import com.sun.cli.util.stringifier.SmartStringifier; 41 import com.sun.cli.util.stringifier.Stringifier; 42 import com.sun.cli.util.stringifier.StringifierRegistry; 43 import com.sun.cli.util.stringifier.ArrayStringifier; 44 import com.sun.cli.util.DebugState; 45 46 import com.sun.cli.jmx.cmd.ArgHelper; 47 48 public abstract class CmdBase implements Cmd, CmdOutput, DebugState 49 { 50 public final static String ENV_TOKENS = "TOKENS"; 51 public final static String ENV_CMD_RUNNER = "CMD_RUNNER"; 52 public final static String ENV_CMD_FACTORY = "CMD_FACTORY"; 53 public final static String ENV_COMMANDS = "COMMANDS"; 54 55 public final static String ENV_DEBUG = "debug"; 57 58 private final CmdEnv mEnv; 59 final String [] mTokens; 60 private ArgHelper mArgHelper; 61 62 private CmdOutput mOutput; 63 64 Stringifier mStringifier = 65 new SmartStringifier( StringifierRegistry.DEFAULT, "\n", false ); 66 67 void 69 dm( Object o ) 70 { 71 mOutput.println( mStringifier.stringify( o ) ); 72 } 73 74 CmdBase( final CmdEnv env ) 75 { 76 mEnv = env; 77 78 mOutput = new CmdOutputImpl( System.out, System.err ); 79 80 mTokens = (String [])env.get( "TOKENS" ); 81 82 mArgHelper = null; 84 } 85 86 90 private static class OptionsInfoWrapper implements ArgHelper.OptionsInfo 91 { 92 final ArgHelper.OptionsInfo mInfo; 93 94 OptionsInfoWrapper( ArgHelper.OptionsInfo info ) 95 { 96 mInfo = info; 97 } 98 99 static boolean 100 isHelp( String token ) 101 { 102 return( token.equalsIgnoreCase( "-h" ) || 103 token.equalsIgnoreCase( "--help" ) || 104 token.equalsIgnoreCase( "-help" ) ); 105 } 106 107 public String tokenToOptionName( String token ) 108 { return( mInfo.tokenToOptionName( token ) ); } 109 110 public String tokenToOptionData( String token ) 111 { return( mInfo.tokenToOptionData( token ) ); } 112 113 public boolean isLegalOption( String token ) 114 { 115 return( mInfo.isLegalOption( token ) || isHelp( token ) ); 116 } 117 118 public boolean isBoolean( String token ) 119 { return( isHelp( token ) ? true : mInfo.isBoolean( token ) ); } 120 121 public int getNumValues( String token ) 122 { return( isHelp( token ) ? 0 : mInfo.getNumValues( token ) ); } 123 } 124 125 void 126 initArgHelper() 127 throws ArgHelper.IllegalOptionException 128 { 129 if ( mArgHelper == null ) 130 { 131 final java.util.ListIterator iter = 132 Arrays.asList( mTokens ).listIterator( 1 ); 133 134 final ArgHelper.OptionsInfo optionsInfo = getOptionInfo(); 135 136 mArgHelper = new ArgHelperImpl( iter, new OptionsInfoWrapper( optionsInfo ) ); 137 } 138 } 139 140 CmdFactory 141 getCmdFactory() 142 { 143 return( (CmdFactory)envGet( ENV_CMD_FACTORY ) ); 144 } 145 146 147 void 148 envRemove( String key) 149 { 150 mEnv.remove( key ); 151 } 152 153 Object 154 envGet( String key) 155 { 156 return( mEnv.get( key ) ); 157 } 158 159 Object 160 envGet( String key, Object defaultValue ) 161 { 162 Object value = envGet( key ); 163 164 if ( value == null ) 165 { 166 value = defaultValue; 167 } 168 169 return( value ); 170 } 171 172 java.util.Set 173 getEnvKeys( ) 174 { 175 return( mEnv.getKeys() ); 176 } 177 178 java.util.Set 179 getEnvKeys( String regExp ) 180 { 181 final Iterator iter = getEnvKeys().iterator(); 182 final java.util.Set filteredSet = new java.util.HashSet (); 183 184 final Pattern pattern = Pattern.compile( regExp ); 185 186 while ( iter.hasNext() ) 187 { 188 final String key = (String )iter.next(); 189 190 final Matcher m = pattern.matcher( key ); 191 192 if ( m.matches() ) 193 { 194 filteredSet.add( key ); 195 } 196 197 } 198 199 return( filteredSet ); 200 } 201 202 void 203 envPut( String key, Object value, boolean allowPersistence ) 204 { 205 mEnv.put( key, value, allowPersistence); 206 } 207 208 ArgHelper 209 getArgHelper() 210 { 211 assert( mArgHelper != null ); 212 213 return( mArgHelper ); 214 } 215 216 int 217 countOptions( ) 218 throws ArgHelper.IllegalOptionException 219 { 220 return( getArgHelper().countOptions( ) ); 221 } 222 223 String 224 getString( String name, String defaultValue) 225 throws ArgHelper.IllegalOptionException 226 { 227 return( getArgHelper().getString( name, defaultValue ) ); 228 } 229 230 231 Integer 232 getInteger( String name) 233 throws ArgHelper.IllegalOptionException 234 { 235 return( getArgHelper().getInteger( name ) ); 236 } 237 238 Boolean 239 getBoolean( String name, Boolean defaultValue) 240 throws ArgHelper.IllegalOptionException 241 { 242 return( getArgHelper().getBoolean( name, defaultValue ) ); 243 } 244 245 String [] 246 getOperands( ) 247 { 248 return( getArgHelper().getOperands( ) ); 249 } 250 251 String 252 getCmdNameAsInvoked( ) 253 { 254 return( mTokens[ 0 ] ); 255 } 256 257 void 258 requireNumOperands( final int numRequiredOperands) 259 throws WrongNumberOfOperandsException 260 { 261 final String [] operands = getOperands(); 262 263 if ( operands.length < numRequiredOperands ) 264 { 265 requireNumOperandsFailed( operands.length, numRequiredOperands, null); 266 } 267 } 268 269 void 270 requireNumOperands( final int numRequiredOperands, String msg) 271 throws WrongNumberOfOperandsException 272 { 273 final String [] operands = getOperands(); 274 275 if ( operands.length < numRequiredOperands ) 276 { 277 requireNumOperandsFailed( operands.length, numRequiredOperands, msg); 278 } 279 } 280 281 int 282 getNumRequiredOperands() 283 { 284 return( 1 ); 286 } 287 288 class WrongNumberOfOperandsException extends Exception 289 { 290 WrongNumberOfOperandsException( String msg ) 291 { 292 super( msg ); 293 } 294 } 295 296 void 297 requireNumOperandsFailed( int numSupplied, int numRequired, String msg) 298 throws WrongNumberOfOperandsException 299 { 300 printError( "ERROR: " + numRequired + " arguments required, " + numSupplied + " supplied" ); 301 if ( msg != null ) 302 { 303 println( msg ); 304 } 305 else 306 { 307 printUsage(); 308 } 309 throw new WrongNumberOfOperandsException( "illlegal number of operands" ); 310 } 311 312 313 ArgHelper.OptionsInfo 314 getOptionInfo() 315 throws ArgHelper.IllegalOptionException 316 { 317 return( new ArgHelperOptionsInfo( ) ); 319 } 320 321 322 public void 323 print( Object o ) 324 { 325 mOutput.print( o ); 326 } 327 328 public void 329 println( Object o ) 330 { 331 mOutput.println( o ); 332 } 333 334 public void 335 printError( Object o ) 336 { 337 mOutput.printError( o ); 338 } 339 340 public void 341 printDebug( Object o ) 342 { 343 mOutput.printDebug( o ); 344 } 345 346 public boolean 347 getDebug() 348 { 349 boolean isDebug = false; 350 final String value = (String )envGet( ENV_DEBUG ); 351 if ( value != null ) 352 { 353 isDebug = value.equalsIgnoreCase( "true" ) || 354 value.equalsIgnoreCase( "t" ) || 355 value.equalsIgnoreCase( "yes" )|| 356 value.equalsIgnoreCase( "y" ); 357 } 358 return( isDebug ); 359 } 360 361 void 362 preExecute() 363 throws Exception 364 { 365 initArgHelper(); 366 requireNumOperands( getNumRequiredOperands() ); 367 } 368 369 void 370 handleException( final Exception e ) 371 { 372 if ( e instanceof WrongNumberOfOperandsException ) 373 { 374 } 376 else if ( e instanceof ArgHelper.IllegalOptionException ) 377 { 378 printError( "ERROR: " + e.getMessage() ); 379 printUsage(); 380 } 381 else if ( e instanceof java.io.IOException ) 382 { 383 printError( "ERROR: " + e.getMessage() ); 384 } 385 else 386 { 387 printError( "ERROR: exception of type: " + 388 e.getClass().getName() + ", msg = " + e.getMessage() ); 389 e.printStackTrace(); 390 } 391 } 392 393 public final void 394 execute( ) 395 throws Exception 396 { 397 try 398 { 399 preExecute(); 400 401 if ( getBoolean( "help", Boolean.FALSE ).booleanValue() || 402 getBoolean( "h", Boolean.FALSE ).booleanValue() ) 403 { 404 printUsage(); 405 } 406 else 407 { 408 executeInternal( ); 409 } 410 } 411 catch( Exception e ) 412 { 413 handleException( e ); 414 e.printStackTrace(); 415 } 416 } 417 418 abstract void executeInternal( ) throws Exception ; 419 abstract String getUsage( ); 420 421 private static final Class [] EMPTY_SIG = new Class [ 0 ]; 422 private static final Object [] EMPTY_ARGS = new Object [ 0 ]; 423 424 public static String [] 425 getCmdNames( final Class theClass ) 426 throws java.lang.NoSuchMethodException , 427 java.lang.IllegalAccessException , java.lang.reflect.InvocationTargetException 428 { 429 String [] names = null; 430 431 final java.lang.reflect.Method m = theClass.getDeclaredMethod( "getNames", EMPTY_SIG ); 432 433 names = (String [])m.invoke( theClass, EMPTY_ARGS); 434 435 return( names ); 436 } 437 438 String 439 getAlsoKnownAs( Class theClass ) 440 { 441 String aka = ""; 442 443 try 444 { 445 aka = ArrayStringifier.stringify( getCmdNames( theClass ), " " ); 446 } 447 catch( Exception e ) 448 { 449 } 451 452 return( "Also known as: " + aka ); 453 } 454 455 void 456 printUsage() 457 { 458 println( getUsage() ); 459 460 461 println( getAlsoKnownAs( this.getClass() ) ); 462 } 463 } 464 465 | Popular Tags |