final EvaluatedType rtype = m.returnType();
final TypeConverter rtc = rtype == null ? null : mom.getTypeConverter(
rtype);
final List<EvaluatedType> atypes = m.parameterTypes();
final List<TypeConverter> atcs = new ArrayList<TypeConverter>();
final ManagedOperation mo = mom.getAnnotation(m.element(),
ManagedOperation.class);
Descriptor modelDescriptor = makeValidDescriptor(
DescriptorIntrospector.descriptorForElement(mom, m.element()),
DescriptorType.operation, m.name());
for (EvaluatedType ltype : atypes) {
atcs.add(mom.getTypeConverter(ltype));
}
if (mom.registrationFineDebug()) {
mm.info( true, "desc", desc);
mm.info( true, "rtype", rtype);
mm.info( true, "rtc", rtc);
mm.info( true, "atcs", atcs);
mm.info( true, "atypes", atypes);
mm.info( true, "descriptor", modelDescriptor);
}
final Operation oper = new Operation() {
public Object evaluate(FacetAccessor target, List<Object> args) {
mm.enter( mom.runtimeDebug(), "Operation:evaluate", target,
args);
Object[] margs = new Object[args.size()];
Iterator<Object> argsIterator = args.iterator();
Iterator<TypeConverter> tcIterator = atcs.iterator();
int ctr = 0;
while (argsIterator.hasNext() && tcIterator.hasNext()) {
final Object arg = argsIterator.next();
final TypeConverter tc = tcIterator.next();
margs[ctr++] = tc.fromManagedEntity(arg);
}
mm.info( mom.runtimeDebug(), "Before invoke: margs=", margs);
Object result = target.invoke(m.method(), mom.runtimeDebug(),
margs);
mm.info( mom.runtimeDebug(), "After invoke: result=", result);
if (rtc == null) {
return null;
} else {
return rtc.toManagedEntity(result);
}
}
};
final ParameterNames pna = mom.getAnnotation( m.element(),
ParameterNames.class);
mm.info( mom.registrationFineDebug(), "pna", pna);
if (pna != null && pna.value().length != atcs.size()) {
throw Exceptions.self.parameterNamesLengthBad();
}
final MBeanParameterInfo[] paramInfo =
new OpenMBeanParameterInfoSupport[atcs.size()];
int ctr = 0;
for (TypeConverter tc : atcs) {
String name = "";
try {
name = (pna == null) ? "arg" + ctr : pna.value()[ctr];
paramInfo[ctr] = new OpenMBeanParameterInfoSupport(
name, Exceptions.self.noDescriptionAvailable(),
tc.getManagedType());
ctr++;
} catch (IllegalArgumentException ex) {
Exceptions.self.excInOpenParameterInfo(ex, name, m);
}
}
final ModelMBeanOperationInfo operInfo =
new ModelMBeanOperationInfo(m.name(),
desc, paramInfo, rtc.getManagedType().getClassName(),
mo.impact().ordinal(), modelDescriptor);
mm.info(mom.registrationFineDebug(), "operInfo", operInfo);
return new Pair<Operation, ModelMBeanOperationInfo>(oper, operInfo);
} finally {