1 23 24 package com.sun.enterprise.cli.commands; 25 26 import com.sun.enterprise.cli.framework.*; 27 import com.sun.enterprise.admin.util.ClassUtil; 28 import com.sun.enterprise.admin.util.ArrayConversion; 29 import javax.management.Attribute ; 30 import javax.management.MBeanServerConnection ; 31 import javax.management.ObjectName ; 32 import javax.management.AttributeNotFoundException ; 33 import javax.management.RuntimeMBeanException ; 34 import javax.management.RuntimeOperationsException ; 35 36 37 import java.util.Vector ; 38 import java.util.HashMap ; 39 import java.util.Arrays ; 40 41 42 46 public class DottedNamesCommand extends S1ASCommand 47 { 48 49 private static final String GET_COMMAND = "get"; 50 private static final String SET_COMMAND = "set"; 51 private static final String LIST_COMMAND = "list"; 52 private static final String GET_OPERATION = "dottedNameGet"; 53 private static final String LIST_OPERATION = "dottedNameList"; 54 private static final String GET_MONITORING_OPERATION = "dottedNameMonitoringGet"; 55 private static final String LIST_MONITORING_OPERATION = "dottedNameMonitoringList"; 56 private static final String SET_OPERATION = "dottedNameSet"; 57 private static final String MONITOR_OPTION = "monitor"; 58 private static final String OBJECT_NAME = "com.sun.appserv:name=dotted-name-get-set,type=dotted-name-support"; 59 private static final String INTERVAL_OPTION = "interval"; 60 private static final String ITERATIONS_OPTION = "iterations"; 61 private static final String PROPERTY_STRING = "property|system-property"; 62 63 64 69 public boolean validateOptions() throws CommandValidationException 70 { 71 if(getOperation().equals(GET_MONITORING_OPERATION)) 76 { 77 if(!(isIntervalValid() && isIterationValid())) 78 return false; 79 } 80 return super.validateOptions(); 81 } 82 83 private boolean isIntervalValid(){ 84 85 String interval = getOption(INTERVAL_OPTION); 86 87 if(interval == null) 88 return true; 89 else if((interval != null) && (Integer.parseInt(interval) > 0)) 90 return true; 91 else 92 { 93 printMessage(getLocalizedString("InvalidInterval", new Object [] {interval})); 94 return false; 95 } 96 } 97 98 private boolean isIterationValid() { 99 100 String iterations = getOption(ITERATIONS_OPTION); 101 102 if(iterations == null) 103 return true; 104 else if((iterations != null) && (Integer.parseInt(iterations) > 0)) 105 return true; 106 else 107 { 108 printMessage(getLocalizedString("InvalidIterations", new Object [] {iterations})); 109 return false; 110 } 111 } 112 113 116 private final class AttributeComparator implements java.util.Comparator 117 { 118 public int 119 compare( Object o1, Object o2 ) 120 { 121 final Attribute attr1 = (Attribute )o1; 122 final Attribute attr2 = (Attribute )o2; 123 124 return( attr1.getName().compareTo( attr2.getName() ) ); 125 } 126 127 public boolean 128 equals( Object other ) 129 { 130 return( other instanceof AttributeComparator ); 131 } 132 } 133 134 137 private Attribute [] 138 collectAllAttributes( Object [] results ) 139 { 140 final HashMap attrs = new HashMap (); 142 143 for( int i = 0; i < results.length; ++i ) 144 { 145 final Object result = results[ i ]; 146 147 if ( result instanceof Attribute ) 148 { 149 attrs.put( ((Attribute )result).getName(), result ); 150 } 151 else if ( result instanceof Attribute [] ) 152 { 153 final Attribute [] list = (Attribute [])result; 154 155 for( int attrIndex = 0; attrIndex < list.length; ++attrIndex ) 156 { 157 final Attribute attr = list[ attrIndex ]; 158 159 attrs.put( attr.getName(), attr ); 160 } 161 } 162 else 163 { 164 assert( result instanceof Exception ); 165 } 166 } 167 168 final Attribute [] attrsArray = new Attribute [ attrs.size() ]; 169 attrs.values().toArray( attrsArray ); 170 Arrays.sort( attrsArray, new AttributeComparator() ); 171 172 return( attrsArray ); 173 } 174 175 179 public void runCommand() throws CommandException, CommandValidationException 180 { 181 if(!validateOptions()) 182 return; 183 184 final MBeanServerConnection mbsc = getMBeanServerConnection(getHost(), 186 getPort(), 187 getUser(), 188 getPassword()); 189 final Object [] params = getDottedNamesParam(getName().equals(LIST_COMMAND)? 190 false:true); 191 final String [] types = new String [] {STRING_ARRAY}; 192 final String operationName = getOperation(); 193 final String interval = getOption(INTERVAL_OPTION); 194 final String iterations = getOption(ITERATIONS_OPTION); 195 196 try 197 { 198 final Object [] returnValues = (Object [])mbsc.invoke(new ObjectName (OBJECT_NAME), 200 operationName, params, types); 201 if ( operationName.indexOf( "List" ) >= 0) 202 { 203 displayResultFromList( (String [])returnValues ); 205 } 206 else if(operationName.equals(this.GET_MONITORING_OPERATION) && (interval != null) && (iterations != null)) 207 { 208 String [] userArgs = (String [])params[ 0 ]; 209 displayResultFromGetOrSet( userArgs, returnValues ); 210 long interval_millis = Integer.parseInt(interval) * 1000; 211 int counter = Integer.parseInt(iterations); 212 int i = 1; 213 do { 214 printMessage("\n"); 215 if (i < counter) { 216 try { 217 Thread.currentThread().sleep(interval_millis); 218 Object [] retVals = (Object []) mbsc.invoke(new ObjectName (OBJECT_NAME), 219 operationName, 220 params, 221 types); 222 userArgs = (String []) params[0]; 223 displayResultFromGetOrSet(userArgs, retVals); 224 ++i; 225 } catch (InterruptedException ie) { 226 } 227 } 228 }while(i < counter); 229 } 230 else 231 { 232 final String [] userArgs = (String [])params[ 0 ]; 233 234 displayResultFromGetOrSet( userArgs, returnValues ); 235 } 236 } 237 catch(Exception e) 238 { 239 final String msg = getExceptionMessage( e ); 240 if ( msg != null) 241 { 242 CLILogger.getInstance().printDetailMessage( msg ); 243 } 244 245 throw new CommandException(getLocalizedString("CommandUnSuccessful", 246 new Object [] {name} ), e); 247 } 248 } 249 250 251 255 private Object [] getDottedNamesParam(boolean convertUnderscore) 256 { 257 final Vector dottedNames = getOperands(); 258 String [] dottedNamesArray = new String [dottedNames.size()]; 259 260 for (int ii=0; ii<dottedNames.size(); ii++) 261 { 262 if (convertUnderscore) 263 dottedNamesArray[ii] = convertUnderscoreToHyphen((String )dottedNames.get(ii)); 264 else 265 dottedNamesArray[ii] = (String )dottedNames.get(ii); 266 } 267 return new Object []{dottedNamesArray}; 268 269 } 271 272 273 282 private String getOperation() 283 { 284 if (getName().equals(SET_COMMAND)) 285 return SET_OPERATION; 286 else if (getName().equals(GET_COMMAND)) 287 { 288 if (getBooleanOption(MONITOR_OPTION)) 289 return GET_MONITORING_OPERATION; 290 else 291 return GET_OPERATION; 292 } 293 else if (getName().equals(LIST_COMMAND)) 294 { 295 if (getBooleanOption(MONITOR_OPTION)) 296 return LIST_MONITORING_OPERATION; 297 else 298 return LIST_OPERATION; 299 } 300 return null; 301 } 302 303 private void printMessage( String msg ) 304 { 305 CLILogger.getInstance().printMessage( msg ); 306 } 307 308 private String 309 getExceptionMessage( Exception e ) 310 { 311 String msg = null; 312 313 if (e instanceof RuntimeMBeanException ) { 314 RuntimeMBeanException rmbe = (RuntimeMBeanException ) e; 315 msg = rmbe.getTargetException().getLocalizedMessage(); 316 } else if (e instanceof RuntimeOperationsException ) { 317 RuntimeOperationsException roe = (RuntimeOperationsException ) e; 318 msg = roe.getTargetException().getLocalizedMessage(); 319 } else { 320 msg = e.getLocalizedMessage(); 321 } 322 if ( msg == null || msg.length() == 0 ) 323 { 324 msg = e.getMessage(); 325 } 326 327 if ( msg == null || msg.length() == 0 ) 328 { 329 msg = e.getClass().getName(); 330 } 331 332 return( msg ); 333 } 334 335 336 337 338 private static String INNER_ARRAY_DELIM = ","; private static String OUTER_ARRAY_DELIM = System.getProperty("line.separator"); 341 346 private void 347 displayResultFromGetOrSet( final String [] inputs, final Object [] returnValues ) 348 throws Exception 349 { 350 if ( returnValues.length == 1 ) 351 { 352 final Object result = returnValues[ 0 ]; 354 355 if ( result instanceof Exception ) 356 { 357 throw (Exception ) result; 358 } 359 else if ( result.getClass() == Attribute [].class ) 360 { 361 final Attribute [] attrs = (Attribute [])result; 363 364 if ( attrs.length == 0 ) 365 { 366 throw new AttributeNotFoundException ( getLocalizedString("NoWildcardMatches") ); 367 } 368 369 printMessage( stringify( attrs, OUTER_ARRAY_DELIM ) ); 370 } 371 else 372 { 373 printMessage( stringify( result, OUTER_ARRAY_DELIM ) ); 374 } 375 } 376 else 377 { 378 final Attribute [] attrs = collectAllAttributes( returnValues ); 381 382 printMessage( stringify( attrs, OUTER_ARRAY_DELIM ) ); 383 384 for( int i = 0; i < returnValues.length; ++i ) 386 { 387 if ( returnValues[ i ] instanceof Exception ) 388 { 389 final Exception e = (Exception )returnValues[ i ]; 390 391 final String msg = getLocalizedString( "ErrorInGetSet", 392 new Object [] { inputs[ i ], getExceptionMessage( e ) } ); 393 394 printMessage( msg ); 395 } 396 } 397 } 398 399 return; 400 } 401 402 403 private String 404 stringifyArray( final Object [] a, String delim ) 405 { 406 final StringBuffer buf = new StringBuffer (); 407 408 for( int i = 0; i < a.length; ++i ) 409 { 410 buf.append( stringify( a[ i ], INNER_ARRAY_DELIM ) ); 411 if ( i != a.length - 1 ) 412 { 413 buf.append( delim ); 414 } 415 } 416 return( buf.toString() ); 417 } 418 419 private String 420 stringify( Object o ) 421 { 422 return( stringify( o, "\n" ) ); 423 } 424 425 428 private String 429 stringify( Object o, String delim ) 430 { 431 String result = null; 432 433 if ( o == null ) 434 { 435 result = ""; 436 } 437 else if ( o instanceof Attribute ) 438 { 439 final Attribute attr = (Attribute )o; 440 441 result = attr.getName() + " = " + stringify( attr.getValue(), INNER_ARRAY_DELIM ); 442 } 443 else if ( ClassUtil.objectIsPrimitiveArray( o ) ) 444 { 445 final Object [] objectList = ArrayConversion.toAppropriateType( o ); 446 447 result = stringifyArray( objectList, delim ); 448 } 449 else if ( ClassUtil.objectIsArray( o ) ) 450 { 451 result = stringifyArray( (Object [])o, delim ); 452 } 453 else if ( o instanceof Exception ) 454 { 455 final Exception e = (Exception )o; 456 457 result = getExceptionMessage( e ); 458 } 459 else 460 { 461 result = o.toString(); 462 } 463 464 assert( result != null ); 465 return( result ); 466 } 467 468 469 470 471 private void displayResultFromList( final String [] result ) 472 { 473 if ( result.length == 0 ) 474 { 475 477 final String displayOperands = stringify(getDottedNamesParam(true), 478 INNER_ARRAY_DELIM); 479 printMessage(getLocalizedString("EmptyList", new Object [] { 480 displayOperands })); 481 } 482 else 483 { 484 printMessage( stringify( result, OUTER_ARRAY_DELIM ) ); 485 } 486 } 487 488 489 495 public String convertUnderscoreToHyphen(String param) 496 { 497 int endIndex = param.indexOf('='); 498 int begIndex = (endIndex>0)? param.lastIndexOf('.', endIndex):param.lastIndexOf('.'); 499 if(begIndex<1 || checkPropertyToConvert(param.substring(0,begIndex))) 500 return param; 501 if(endIndex>0) 502 return param.substring(0,begIndex) + param.substring(begIndex,endIndex).replace('_', '-') + param.substring(endIndex); 503 else 504 return param.substring(0,begIndex) + param.substring(begIndex).replace('_', '-'); 505 } 506 507 515 public boolean checkPropertyToConvert(String param) 516 { 517 final int index = param.lastIndexOf('.'); 518 if (index < 0) return false; 519 final String elementName = param.substring(index+1); 520 if (elementName.matches(PROPERTY_STRING)) 521 return true; 522 else 523 return false; 524 } 525 526 527 } 528 | Popular Tags |