private void invoke(final ObjectName name)
throws Exception
{
log.debug("Invoke " + name);
MBeanServerConnection server = getMBeanServer();
// get mbean info for this mbean
MBeanInfo info = server.getMBeanInfo(name);
// does it even have an operation of this name?
MBeanOperationInfo[] ops = info.getOperations();
MBeanOp inputOp = new MBeanOp(opName, opArgs.size());
MBeanOp matchOp = null;
ArrayList opList = new ArrayList();
for (int i = 0; i < ops.length; i++)
{
MBeanOperationInfo opInfo = ops[i];
MBeanOp op = new MBeanOp(opInfo.getName(), opInfo.getSignature());
if (inputOp.equals(op) == true)
{
matchOp = op;
break;
}
opList.add(op);
}
if (matchOp == null)
{
// If there was not explicit match on type, look for a match on arg count
OpCountComparator comparator = new OpCountComparator();
Collections.sort(opList, comparator);
int match = Collections.binarySearch(opList, inputOp, comparator);
if (match >= 0)
{
// Validate that the match op equates to the input op
matchOp = (MBeanOp) opList.get(match);
match = comparator.compare(matchOp, inputOp);
if (match != 0)
{
throw new CommandException("MBean has no such operation named '" +
opName + "' with signature compatible with: " + opArgs);
}
}
else
{
throw new CommandException("MBean has no such operation named '" +
opName + "' with signature compatible with: " + opArgs);
}
}
// convert parameters with PropertyEditor
int count = matchOp.getArgCount();
Object[] params = new Object[count];
for (int i = 0; i < count; i++)
{
String argType = matchOp.getArgType(i);
PropertyEditor editor = PropertyEditors.getEditor(argType);
editor.setAsText((String) opArgs.get(i));
params[i] = editor.getValue();
}
log.debug("Using params: " + Strings.join(params, ","));
// invoke the operation
Object result = server.invoke(name, opName, params, matchOp.getSignature());
log.debug("Raw result: " + result);
if (!context.isQuiet())
{
// Translate the result to text