use SigarInvokerJMX

This commit is contained in:
Doug MacEachern 2008-10-09 05:55:17 +00:00
parent 14fc064709
commit 7ff83f9ba9
1 changed files with 19 additions and 52 deletions

View File

@ -33,12 +33,11 @@ import org.hyperic.sigar.Sigar;
public class ReflectedMBean extends AbstractMBean { public class ReflectedMBean extends AbstractMBean {
private Method method;
private Map methods; private Map methods;
private Map attrs = new HashMap(); private Map attrs = new HashMap();
private String type; private String type;
private Object[] args;
private String name; private String name;
private SigarInvokerJMX invoker;
protected String getType() { protected String getType() {
return this.type; return this.type;
@ -78,9 +77,13 @@ public class ReflectedMBean extends AbstractMBean {
} }
} }
protected Class getMBeanClass() { private String getMBeanClassName() {
return "org.hyperic.sigar." + getType();
}
private Class getMBeanClass() {
try { try {
return getMethod().getReturnType(); return Class.forName(getMBeanClassName());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
@ -88,21 +91,15 @@ public class ReflectedMBean extends AbstractMBean {
} }
protected ReflectedMBean(Sigar sigar, String type) { protected ReflectedMBean(Sigar sigar, String type) {
super(sigar, CACHELESS); super(sigar, CACHED_5SEC);
this.type = type; this.type = type;
this.args = new Object[0];
this.name = this.name =
SigarInvokerJMX.DOMAIN_NAME + ":" + SigarInvokerJMX.DOMAIN_NAME + ":" +
MBEAN_ATTR_TYPE + "=" + getType(); MBEAN_ATTR_TYPE + "=" + getType();
} }
protected ReflectedMBean(Sigar sigar, String type, Object[] args) {
this(sigar, type);
this.args = args;
}
protected ReflectedMBean(Sigar sigar, String type, String arg) { protected ReflectedMBean(Sigar sigar, String type, String arg) {
this(sigar, type, new Object[] { arg }); this(sigar, type);
this.name += ",Name=" + encode(arg); this.name += ",Name=" + encode(arg);
} }
@ -114,40 +111,13 @@ public class ReflectedMBean extends AbstractMBean {
return this.name; return this.name;
} }
protected Method getMethod() throws Exception { protected SigarInvokerJMX getInvoker() {
if (this.method == null) { if (this.invoker == null) {
String getName = "get" + getType(); this.invoker =
Class[] params = getMethodParamTypes(); SigarInvokerJMX.getInstance(this.sigar, getObjectName());
this.method = this.invoker.setType(getType());
this.sigarImpl.getClass().getDeclaredMethod(getName,
params);
} }
return this.method; return this.invoker;
}
protected Class[] getMethodParamTypes() {
int len = this.args.length;
Class[] types = new Class[len];
for (int i=0; i<len; i++) {
types[i] = this.args[i].getClass();
}
return types;
}
protected Object[] getMethodParams() {
return this.args;
}
private Object getReflectedAttribute(String name)
throws Exception {
Method method = getMethod();
Object obj =
method.invoke(this.sigarImpl,
getMethodParams());
Method attr =
obj.getClass().getMethod("get" + name, new Class[0]);
return attr.invoke(obj, new Object[0]);
} }
public Object getAttribute(String name) public Object getAttribute(String name)
@ -157,11 +127,8 @@ public class ReflectedMBean extends AbstractMBean {
if (val != null) { if (val != null) {
return val; return val;
} }
if (this.methods.get(name) == null) {
throw new AttributeNotFoundException(name);
}
try { try {
return getReflectedAttribute(name); return getInvoker().invoke(name);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new ReflectionException(e); throw new ReflectionException(e);
@ -223,8 +190,8 @@ public class ReflectedMBean extends AbstractMBean {
public MBeanInfo getMBeanInfo() { public MBeanInfo getMBeanInfo() {
MBeanInfo info = MBeanInfo info =
new MBeanInfo(getMBeanClass().getName(), new MBeanInfo(getClass().getName(),
"", getMBeanClassName(),
getAttributeInfo(), getAttributeInfo(),
null, //constructors null, //constructors
null, //operations null, //operations