📄 xmbeanoperation.java
字号:
package book.jmx.examples;
import java.lang.reflect.*;
import javax.management.*;
import javax.management.modelmbean.*;
import javax.management.loading.*;
public class XMBeanOperation implements XMBeanConstants {
protected Object managedResource = null;
protected Class[] signature = null;
protected Method operationMethod = null;
protected String operationName = null;
public XMBeanOperation(XMBean mbean,
ModelMBeanOperationInfo operationInfo) throws MBeanException {
try {
this.operationName = operationInfo.getName();
this.managedResource = mbean.resource;
this.signature = createSignature(operationInfo);
this.operationMethod = managedResource.getClass().
getMethod(operationName, signature);
}
catch (NoSuchMethodException e) {
throw new MBeanException(e,
"Resource method " + operationName + " not found."
);
}
}
private Class[] createSignature(ModelMBeanOperationInfo info)
throws MBeanException {
Class[] paramTypes = null;
MBeanParameterInfo[] sign = info.getSignature();
if (sign != null) {
paramTypes = new Class[sign.length];
for (int i = 0; i < paramTypes.length; ++i) {
try {
String type = sign[i].getType();
paramTypes[i] = DefaultLoaderRepository.loadClass(type);
}
catch (ClassNotFoundException e) {
throw new MBeanException(e,
"Error loading parameter class " + sign[i].getType()
);
}
}
}
return paramTypes;
}
public Object invoke(Object[] args) throws ReflectionException {
if (operationMethod == null) {
throw new ReflectionException(new Exception(
"Method " + operationName + " not found.")
);
}
try {
return operationMethod.invoke(managedResource, args);
}
catch (Exception e) {
throw new ReflectionException(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -