How do I pass correctly primitive values when invoking operations?
This is another typical mistake when programming with MBeans (thanks to Adrian
for the explanation).
The MBeanServer presents a detyped interface for invoking operations on MBeans:
... Object invoke(ObjectName name, String operationName, Object[] params, String[] signature) throws InstanceNotFoundException, MBeanException, ReflectionException; ...
The params array contains the arguments to the operations and the
signature array contains their respective types. So for example to
call method:
class SomeMBean { ... String listThreadInfo(Integer id); ... }
We would use:
... String result = (String)server.invoke(.., // the ObjectName of the target MBean "listThreadInfo", // the method name new Object[] { new Integer(666) }, // an evil parameter new String[] { Integer.class.getName() // yields "java.lang.Integer" ); ...
But how do I call the operation if the parameter is of a primitive java type?
class SomeMBean { ... String listThreadInfo(int id); ... }
The wrong way to do it:
server.invoke(,,, new String[] { Integer.class.getName() }); or server.invoke(,,, new String[] { "int" });
The correct way to do it:
server.invoke(,,, new String[] { Integer.TYPE.getName() }); or server.invoke(,,, new String[] { "I" });
In other words, for primitive types you need to use the name of the class
that represents the primitive type (in this case Integer.TYPE), not the
classname of its wrapper class.
For more info, see Class.getName()
Referenced by:
Comments