1 23 24 package com.sun.enterprise.diagnostics.collect; 25 26 27 import com.sun.enterprise.admin.util.ClassUtil; 28 import com.sun.enterprise.admin.util.ArrayConversion; 29 import com.sun.enterprise.admin.server.core.jmx.AppServerMBeanServerFactory; 30 import com.sun.enterprise.admin.server.core.jmx.InitException; 31 32 import com.sun.enterprise.diagnostics.DiagnosticException; 33 import com.sun.logging.LogDomains; 34 35 import javax.management.*; 36 37 38 import java.util.*; 39 import java.util.logging.Logger ; 40 import java.util.logging.Level ; 41 42 43 47 public class MonitoringInfoHelper { 48 49 private static final String LIST_COMMAND = "list"; 50 private static final String LIST_OPERATION = "dottedNameList"; 51 private static final String LIST_MONITORING_OPERATION = 52 "dottedNameMonitoringList"; 53 private static final String GET_COMMAND = "get"; 54 private static final String GET_OPERATION = "dottedNameGet"; 55 private static final String GET_MONITORING_OPERATION = 56 "dottedNameMonitoringGet"; 57 58 59 public final static String STRING_ARRAY = (new String []{}). 60 getClass().getName(); 61 private static final String MONITOR_OPTION = "monitor"; 62 private static final String OBJECT_NAME = 63 "com.sun.appserv:name=dotted-name-get-set,type=dotted-name-support"; 64 private static final String PROPERTY_STRING = "property|system-property"; 65 66 public final static String SECURE = "secure"; 67 private ArrayList<String > operands; 68 private String name; 69 private HashMap options; 70 71 private static Logger logger = 72 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 73 74 private ArrayList<String > output; 75 76 public MonitoringInfoHelper() { 77 options = new HashMap(); 78 } 79 80 85 public void setOperands(ArrayList<String > operands) { 86 this.operands = operands; 87 } 88 89 94 public ArrayList<String > getOperands() { 95 return operands; 96 } 97 98 103 public void setName(String name) { 104 this.name = name; 105 } 106 107 112 public String getName() { 113 return name; 114 } 115 116 117 122 public void setOptions(HashMap options) { 123 this.options = options; 124 } 125 126 131 public String getOption(String optionName) { 132 if (!optionNameExist(optionName)) { 133 return null; 134 } 135 return (String ) options.get(optionName); 136 } 137 138 143 private boolean optionNameExist(String optionName) { 144 return options.containsKey(optionName); 145 } 146 147 148 154 public void setOption(String optionName, String optionValue) { 155 options.put(optionName, optionValue); 156 } 157 158 159 164 protected boolean getBooleanOption(String optionName) { 165 return Boolean.valueOf(getOption(optionName)); 166 } 167 168 169 175 public void runCommand(ArrayList<String > result) throws DiagnosticException { 176 output = result; 177 179 final Object [] params = getDottedNamesParam 180 (getName().equals(LIST_COMMAND) ? 181 false : true); 182 final String [] types = new String []{STRING_ARRAY}; 183 final String operationName = getOperation(); 184 185 186 try { 187 188 MBeanServer mbs = AppServerMBeanServerFactory. 189 getMBeanServerInstance(); 190 final Object [] returnValues = (Object []) mbs. 193 invoke(new ObjectName(OBJECT_NAME), 194 operationName, params, types); 195 if (operationName.indexOf("List") >= 0) { 196 displayResultFromList((String []) returnValues); 198 } 199 200 else { 201 final String [] userArgs = (String []) params[0]; 202 203 displayResultFromGetOrSet(userArgs, returnValues); 204 } 205 } 206 207 catch (InitException ie) { 208 logger.log(Level.WARNING, "Initialization" + 209 " exception occurred while getting" + 210 " MBean Server Instance", ie); 211 throw new DiagnosticException(ie.getMessage()); 212 } 213 214 catch (Exception e) { 215 final String msg = getExceptionMessage(e); 216 if (msg != null) { 217 logger.log(Level.WARNING, e.getMessage(), e); 218 } 219 throw new DiagnosticException(e.getMessage()); 220 } 221 } 222 223 private static String OUTER_ARRAY_DELIM = System. 224 getProperty("line.separator"); 226 232 private void 233 displayResultFromGetOrSet(final String [] inputs, 234 final Object [] returnValues) 235 throws Exception { 236 if (returnValues.length == 1) { 237 final Object result = returnValues[0]; 239 240 if (result instanceof Exception ) { 241 throw (Exception ) result; 242 } else if (result.getClass() == Attribute[].class) { 243 final Attribute[] attrs = (Attribute[]) result; 245 246 if (attrs.length == 0) { 247 throw new AttributeNotFoundException("NoWildcardMatches"); 248 } 249 250 printMessage(stringify(attrs, OUTER_ARRAY_DELIM)); 251 } else { 252 printMessage(stringify(result, OUTER_ARRAY_DELIM)); 253 } 254 } else { 255 final Attribute[] attrs = collectAllAttributes(returnValues); 258 259 261 for (Attribute attr : attrs) { 262 addToResults(attr.getName() + " = " + attr.getValue()); 263 } 264 265 for (int i = 0; i < returnValues.length; ++i) { 267 if (returnValues[i] instanceof Exception ) { 268 final Exception e = (Exception ) returnValues[i]; 269 270 final String msg = "ErrorInGetSet " + 271 inputs[i] + e.getLocalizedMessage(); 272 273 printMessage(msg); 274 } 275 } 276 } 277 } 278 279 280 284 private Attribute [] 285 collectAllAttributes(Object [] results) { 286 final HashMap attrs = new HashMap(); 288 289 for (final Object result : results) { 290 if (result instanceof Attribute) { 291 attrs.put(((Attribute) result).getName(), result); 292 } else if (result instanceof Attribute[]) { 293 final Attribute[] list = (Attribute[]) result; 294 295 for (int attrIndex = 0; attrIndex < list.length; ++attrIndex) { 296 final Attribute attr = list[attrIndex]; 297 298 attrs.put(attr.getName(), attr); 299 } 300 } else { 301 assert(result instanceof Exception ); 302 } 303 } 304 305 final Attribute[] attrsArray = new Attribute[ attrs.size() ]; 306 attrs.values().toArray(attrsArray); 307 Arrays.sort(attrsArray, new AttributeComparator()); 308 309 return (attrsArray); 310 } 311 312 316 317 private final class AttributeComparator implements Comparator { 318 public int 319 compare(Object o1, Object o2) { 320 final Attribute attr1 = (Attribute) o1; 321 final Attribute attr2 = (Attribute) o2; 322 323 return (attr1.getName().compareTo(attr2.getName())); 324 } 325 326 public boolean 327 equals(Object other) { 328 return (other instanceof AttributeComparator); 329 } 330 } 331 332 337 private Object [] getDottedNamesParam(boolean convertUnderscore) { 338 final ArrayList<String > dottedNames = getOperands(); 339 String [] dottedNamesArray = new String [dottedNames.size()]; 340 341 for (int ii = 0; ii < dottedNames.size(); ii++) { 342 if (convertUnderscore) { 343 dottedNamesArray[ii] = convertUnderscoreToHyphen 344 (dottedNames.get(ii)); 345 } else { 346 dottedNamesArray[ii] = dottedNames.get(ii); 347 } 348 } 349 return new Object []{dottedNamesArray}; 350 351 352 } 353 354 355 365 private String getOperation() { 366 367 if (getName().equals(GET_COMMAND)) { 368 if (getBooleanOption(MONITOR_OPTION)) { 369 return GET_MONITORING_OPERATION; 370 } else { 371 return GET_OPERATION; 372 } 373 } else if (getName().equals(LIST_COMMAND)) { 374 if (getBooleanOption(MONITOR_OPTION)) { 375 return LIST_MONITORING_OPERATION; 376 } else { 377 return LIST_OPERATION; 378 } 379 } 380 return null; 381 382 } 383 384 389 private void printMessage(String msg) { 390 logger.log(Level.INFO, msg, "INFO"); 391 } 392 393 398 private void addToResults(String msg) { 399 if (output != null) { 400 output.add(msg); 401 402 } 403 } 404 405 406 private String 407 getExceptionMessage(Exception e) { 408 String msg = null; 409 410 if (e instanceof RuntimeMBeanException) { 411 RuntimeMBeanException rmbe = (RuntimeMBeanException) e; 412 msg = rmbe.getTargetException().getLocalizedMessage(); 413 } else if (e instanceof RuntimeOperationsException) { 414 RuntimeOperationsException roe = (RuntimeOperationsException) e; 415 msg = roe.getTargetException().getLocalizedMessage(); 416 } else { 417 msg = e.getLocalizedMessage(); 418 } 419 if (msg == null || msg.length() == 0) { 420 msg = e.getMessage(); 421 } 422 423 if (msg == null || msg.length() == 0) { 424 msg = e.getClass().getName(); 425 } 426 427 return (msg); 428 } 429 430 private static String INNER_ARRAY_DELIM = ","; 431 433 private String stringifyArray(final Object [] a, String delim) { 434 final StringBuffer buf = new StringBuffer (); 435 436 for (int i = 0; i < a.length; ++i) { 437 buf.append(stringify(a[i], INNER_ARRAY_DELIM)); 438 if (i != a.length - 1) { 439 buf.append(delim); 440 } 441 } 442 return (buf.toString()); 443 } 444 445 446 449 private String stringify(Object o, String delim) { 450 String result = null; 451 452 if (o == null) { 453 result = ""; 454 } else if (o instanceof Attribute) { 455 final Attribute attr = (Attribute) o; 456 457 result = attr.getName() + " = " + stringify(attr.getValue(), 458 INNER_ARRAY_DELIM); 459 } else if (ClassUtil.objectIsPrimitiveArray(o)) { 460 final Object [] objectList = ArrayConversion.toAppropriateType(o); 461 462 result = stringifyArray(objectList, delim); 463 } else if (ClassUtil.objectIsArray(o)) { 464 result = stringifyArray((Object []) o, delim); 465 } else if (o instanceof Exception ) { 466 final Exception e = (Exception ) o; 467 468 result = getExceptionMessage(e); 469 } else { 470 result = o.toString(); 471 } 472 473 474 return (result); 475 } 476 477 private void displayResultFromList(final String [] result) { 478 if (result.length == 0) { 479 481 final String displayOperands = stringify(getDottedNamesParam(true), 482 INNER_ARRAY_DELIM); 483 printMessage("EmptyList - " + displayOperands); 484 } else { 485 for (String value : result) { 486 addToResults(value); 487 } 488 } 489 } 490 491 492 499 public String convertUnderscoreToHyphen(String param) { 500 int endIndex = param.indexOf('='); 501 int begIndex = (endIndex > 0) ? param.lastIndexOf('.', endIndex) : 502 param.lastIndexOf('.'); 503 if (begIndex < 1 || checkPropertyToConvert( 504 param.substring(0, begIndex))) { 505 return param; 506 } 507 if (endIndex > 0) { 508 return param.substring(0, begIndex) + 509 param.substring(begIndex, endIndex).replace('_', '-') + 510 param.substring(endIndex); 511 } else { 512 return param.substring(0, begIndex) + 513 param.substring(begIndex).replace('_', '-'); 514 } 515 } 516 517 526 public boolean checkPropertyToConvert(String param) { 527 final int index = param.lastIndexOf('.'); 528 if (index < 0) { 529 return false; 530 } 531 final String elementName = param.substring(index + 1); 532 return elementName.matches(PROPERTY_STRING); 533 } 534 } 535 | Popular Tags |