+ "], as we can't connect to the MBean - is it down?");
}
Map<String, PropertySimple> paramProps = parameters.getSimpleProperties();
SortedSet<EmsOperation> emsOperations = emsBean.getOperations();
EmsOperation operation = null;
// There could be multiple operations with the same name but different parameters. Try to find one that has
// the same # of parameters as the RHQ operation def.
for (EmsOperation emsOperation : emsOperations) {
if (emsOperation.getName().equals(name) && (emsOperation.getParameters().size() == paramProps.size())) {
operation = emsOperation;
break;
}
}
if (operation == null) {
// We couldn't find an operation with the expected name and # of parameters, so as as a last ditch effort,
// see if there's an operation that at least has the expected name.
operation = emsBean.getOperation(name);
}
if (operation == null) {
throw new Exception("Operation [" + name + "] not found on MBean [" + emsBean.getBeanName() + "].");
}
List<EmsParameter> emsParams = operation.getParameters();
Map<String, Integer> emsParamIndexesByName = new HashMap<String, Integer>();
for (int i = 0, emsParamsSize = emsParams.size(); i < emsParamsSize; i++) {
EmsParameter emsParam = emsParams.get(i);
if (emsParam.getName() != null) {
emsParamIndexesByName.put(emsParam.getName(), i);
}
}
Object[] paramValues = new Object[operation.getParameters().size()];
for (String propName : paramProps.keySet()) {
Integer paramIndex;
if (propName.matches("\\[\\d+\\]")) {
paramIndex = Integer.valueOf(propName.substring(propName.indexOf('[') + 1, propName.indexOf(']')));
if (paramIndex < 0 || paramIndex >= emsParams.size()) {
throw new IllegalStateException("Index [" + paramIndex + "] specified for parameter of operation ["
+ name + "] on MBean [" + emsBean.getBeanName() + "] is invalid. The MBean operation takes "
+ emsParams.size() + " parameters.");
}
} else {
paramIndex = emsParamIndexesByName.get(propName);
if (paramIndex == null) {
throw new IllegalStateException("Name [" + propName + "] specified for parameter of operation ["
+ name + "] on MBean [" + emsBean.getBeanName()
+ "] is invalid. The MBean operation does not take a parameter by that name.");
}
}
EmsParameter emsParam = emsParams.get(paramIndex);
PropertySimple paramProp = paramProps.get(propName);
String emsParamType = emsParam.getType();
Object paramValue = getPropertyValueAsType(paramProp, emsParamType);
paramValues[paramIndex] = paramValue;
}
Object resultObject = operation.invoke(paramValues);
boolean hasVoidReturnType = (operation.getReturnType() == null
|| Void.class.getName().equals(operation.getReturnType()) || void.class.getName().equals(
operation.getReturnType()));
OperationResult resultToReturn;
if (resultObject == null && hasVoidReturnType) {
resultToReturn = null;
} else {