Fields Summary |
---|
private static final String | LIST_COMMAND |
private static final String | LIST_OPERATION |
private static final String | LIST_MONITORING_OPERATION |
private static final String | GET_COMMAND |
private static final String | GET_OPERATION |
private static final String | GET_MONITORING_OPERATION |
public static final String | STRING_ARRAY |
private static final String | MONITOR_OPTION |
private static final String | OBJECT_NAME |
private static final String | PROPERTY_STRING |
public static final String | SECURE |
private ArrayList | operands |
private String | name |
private HashMap | options |
private static Logger | logger |
private ArrayList | output |
private static String | OUTER_ARRAY_DELIM |
private static String | INNER_ARRAY_DELIM |
Methods Summary |
---|
private void | addToResults(java.lang.String msg)add the results to output
if (output != null) {
output.add(msg);
}
|
public boolean | checkPropertyToConvert(java.lang.String param)This method checks if the element in the dotted name contains "property"
or "system-property". If the element is "property" or "system-property"
then return true else return false.
final int index = param.lastIndexOf('.");
if (index < 0) {
return false;
}
final String elementName = param.substring(index + 1);
return elementName.matches(PROPERTY_STRING);
|
private javax.management.Attribute[] | collectAllAttributes(java.lang.Object[] results)
// use a HashMap to eliminate duplicates; use name as the key
final HashMap attrs = new HashMap();
for (final Object result : results) {
if (result instanceof Attribute) {
attrs.put(((Attribute) result).getName(), result);
} else if (result instanceof Attribute[]) {
final Attribute[] list = (Attribute[]) result;
for (int attrIndex = 0; attrIndex < list.length; ++attrIndex) {
final Attribute attr = list[attrIndex];
attrs.put(attr.getName(), attr);
}
} else {
assert(result instanceof Exception);
}
}
final Attribute[] attrsArray = new Attribute[ attrs.size() ];
attrs.values().toArray(attrsArray);
Arrays.sort(attrsArray, new AttributeComparator());
return (attrsArray);
|
public java.lang.String | convertUnderscoreToHyphen(java.lang.String param)This method will convert the attribute in the dotted name notation
from underscore to hyphen.
int endIndex = param.indexOf('=");
int begIndex = (endIndex > 0) ? param.lastIndexOf('.", endIndex) :
param.lastIndexOf('.");
if (begIndex < 1 || checkPropertyToConvert(
param.substring(0, begIndex))) {
return param;
}
if (endIndex > 0) {
return param.substring(0, begIndex) +
param.substring(begIndex, endIndex).replace('_", '-") +
param.substring(endIndex);
} else {
return param.substring(0, begIndex) +
param.substring(begIndex).replace('_", '-");
}
|
private void | displayResultFromGetOrSet(java.lang.String[] inputs, java.lang.Object[] returnValues)figure out the returnValue type and call appropriate print methods // top-level array
if (returnValues.length == 1) {
// there was a single string provided as input
final Object result = returnValues[0];
if (result instanceof Exception) {
throw (Exception) result;
} else if (result.getClass() == Attribute[].class) {
// this must have been the result of a wildcard input
final Attribute[] attrs = (Attribute[]) result;
if (attrs.length == 0) {
throw new AttributeNotFoundException("NoWildcardMatches");
}
printMessage(stringify(attrs, OUTER_ARRAY_DELIM));
} else {
printMessage(stringify(result, OUTER_ARRAY_DELIM));
}
} else {
// more than one input String; collect all the resulting Attributes
// into one big non-duplicate sorted list and print them.
final Attribute[] attrs = collectAllAttributes(returnValues);
//addToResults( stringify( attrs, OUTER_ARRAY_DELIM ) );
for (Attribute attr : attrs) {
addToResults(attr.getName() + " = " + attr.getValue());
}
// tell about any failures
for (int i = 0; i < returnValues.length; ++i) {
if (returnValues[i] instanceof Exception) {
final Exception e = (Exception) returnValues[i];
final String msg = "ErrorInGetSet " +
inputs[i] + e.getLocalizedMessage();
printMessage(msg);
}
}
}
|
private void | displayResultFromList(java.lang.String[] result)
if (result.length == 0) {
//need to convert the operands to String for display
final String displayOperands = stringify(getDottedNamesParam(true),
INNER_ARRAY_DELIM);
printMessage("EmptyList - " + displayOperands);
} else {
for (String value : result) {
addToResults(value);
}
}
|
protected boolean | getBooleanOption(java.lang.String optionName)Finds the option with the give name
return Boolean.valueOf(getOption(optionName));
|
private java.lang.Object[] | getDottedNamesParam(boolean convertUnderscore)get the dotted notation from the operand and convert it to a Object[]
final ArrayList<String> dottedNames = getOperands();
String [] dottedNamesArray = new String[dottedNames.size()];
for (int ii = 0; ii < dottedNames.size(); ii++) {
if (convertUnderscore) {
dottedNamesArray[ii] = convertUnderscoreToHyphen
(dottedNames.get(ii));
} else {
dottedNamesArray[ii] = dottedNames.get(ii);
}
}
return new Object[]{dottedNamesArray};
|
private java.lang.String | getExceptionMessage(java.lang.Exception e)
String msg = null;
if (e instanceof RuntimeMBeanException) {
RuntimeMBeanException rmbe = (RuntimeMBeanException) e;
msg = rmbe.getTargetException().getLocalizedMessage();
} else if (e instanceof RuntimeOperationsException) {
RuntimeOperationsException roe = (RuntimeOperationsException) e;
msg = roe.getTargetException().getLocalizedMessage();
} else {
msg = e.getLocalizedMessage();
}
if (msg == null || msg.length() == 0) {
msg = e.getMessage();
}
if (msg == null || msg.length() == 0) {
msg = e.getClass().getName();
}
return (msg);
|
public java.lang.String | getName()get the command name (list / get)
return name;
|
public java.util.ArrayList | getOperands()Operands passed to List/ Get command
return operands;
|
private java.lang.String | getOperation()get the operation to invoke depending on the command name and option
if command name is "set" then the operation is dottedNameSet
if command name is "get" then the operation is dottedNameGet
if the command name is "get" with --monitor option to true, then the
operation is dottedNameMonitoringGet
all others return null.
if (getName().equals(GET_COMMAND)) {
if (getBooleanOption(MONITOR_OPTION)) {
return GET_MONITORING_OPERATION;
} else {
return GET_OPERATION;
}
} else if (getName().equals(LIST_COMMAND)) {
if (getBooleanOption(MONITOR_OPTION)) {
return LIST_MONITORING_OPERATION;
} else {
return LIST_OPERATION;
}
}
return null;
|
public java.lang.String | getOption(java.lang.String optionName)Finds the option with the give name
if (!optionNameExist(optionName)) {
return null;
}
return (String) options.get(optionName);
|
private boolean | optionNameExist(java.lang.String optionName)returns true if the option name exist in the options list
return options.containsKey(optionName);
|
private void | printMessage(java.lang.String msg)Log the messages
logger.log(Level.INFO, msg, "INFO");
|
public void | runCommand(java.util.ArrayList result)execute the command (list or get)
output = result;
//use http connector
final Object[] params = getDottedNamesParam
(getName().equals(LIST_COMMAND) ?
false : true);
final String[] types = new String[]{STRING_ARRAY};
final String operationName = getOperation();
try {
MBeanServer mbs = AppServerMBeanServerFactory.
getMBeanServerInstance();
// we always invoke with an array, and so the
// result is always an Object []
final Object[] returnValues = (Object[]) mbs.
invoke(new ObjectName(OBJECT_NAME),
operationName, params, types);
if (operationName.indexOf("List") >= 0) {
// a list operation, just print the String []
displayResultFromList((String []) returnValues);
}
else {
final String[] userArgs = (String []) params[0];
displayResultFromGetOrSet(userArgs, returnValues);
}
}
catch (InitException ie) {
logger.log(Level.WARNING, "Initialization" +
" exception occurred while getting" +
" MBean Server Instance", ie);
throw new DiagnosticException(ie.getMessage());
}
catch (Exception e) {
final String msg = getExceptionMessage(e);
if (msg != null) {
logger.log(Level.WARNING, e.getMessage(), e);
}
throw new DiagnosticException(e.getMessage());
}
|
public void | setName(java.lang.String name)set the command name ( list / get )
this.name = name;
|
public void | setOperands(java.util.ArrayList operands)Operands passed to List/ Get command
this.operands = operands;
|
public void | setOption(java.lang.String optionName, java.lang.String optionValue)Sets the option value for the give name
options.put(optionName, optionValue);
|
public void | setOptions(java.util.HashMap options)Sets the list of options for this Command
this.options = options;
|
private java.lang.String | stringify(java.lang.Object o, java.lang.String delim)
String result = null;
if (o == null) {
result = "";
} else if (o instanceof Attribute) {
final Attribute attr = (Attribute) o;
result = attr.getName() + " = " + stringify(attr.getValue(),
INNER_ARRAY_DELIM);
} else if (ClassUtil.objectIsPrimitiveArray(o)) {
final Object [] objectList = ArrayConversion.toAppropriateType(o);
result = stringifyArray(objectList, delim);
} else if (ClassUtil.objectIsArray(o)) {
result = stringifyArray((Object []) o, delim);
} else if (o instanceof Exception) {
final Exception e = (Exception) o;
result = getExceptionMessage(e);
} else {
result = o.toString();
}
return (result);
|
private java.lang.String | stringifyArray(java.lang.Object[] a, java.lang.String delim)
// when an array is inside another
final StringBuffer buf = new StringBuffer();
for (int i = 0; i < a.length; ++i) {
buf.append(stringify(a[i], INNER_ARRAY_DELIM));
if (i != a.length - 1) {
buf.append(delim);
}
}
return (buf.toString());
|