support no arg SigarRegistry constructor

This commit is contained in:
Doug MacEachern 2009-04-29 17:58:39 -07:00
parent 074d20d7b6
commit 806ee088d7
2 changed files with 39 additions and 14 deletions

View File

@ -29,7 +29,6 @@ import javax.management.ObjectName;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.jmx.SigarProcess; import org.hyperic.sigar.jmx.SigarProcess;
import org.hyperic.sigar.jmx.SigarRegistry;
public class Mx extends SigarCommandBase { public class Mx extends SigarCommandBase {
@ -68,13 +67,15 @@ public class Mx extends SigarCommandBase {
if (isRegistered) { if (isRegistered) {
return; return;
} }
SigarRegistry registry = new SigarRegistry(this.proxy);
try { try {
server.registerMBean(registry, null); String name = org.hyperic.sigar.jmx.SigarRegistry.class.getName();
server.createMBean(name, null);
SigarProcess proc = new SigarProcess(this.sigar); SigarProcess proc = new SigarProcess(this.sigar);
server.registerMBean(proc, new ObjectName(proc.getObjectName())); server.registerMBean(proc, new ObjectName(proc.getObjectName()));
isRegistered = true; isRegistered = true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new SigarException(e.getMessage()); throw new SigarException(e.getMessage());
} }
} }

View File

@ -28,8 +28,10 @@ import javax.management.ObjectName;
import org.hyperic.sigar.CpuInfo; import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.FileSystem; import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.NetInterfaceConfig; import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy; import org.hyperic.sigar.SigarProxy;
import org.hyperic.sigar.SigarProxyCache;
/** /**
* <p>Registry of all Sigar MBeans. Can be used as a convenient way to invoke * <p>Registry of all Sigar MBeans. Can be used as a convenient way to invoke
@ -61,18 +63,26 @@ public class SigarRegistry implements MBeanRegistration, SigarRegistryMBean {
public static final String MBEAN_ATTR_TYPE = SigarInvokerJMX.PROP_TYPE; public static final String MBEAN_ATTR_TYPE = SigarInvokerJMX.PROP_TYPE;
private static final String MBEAN_TYPE = "SigarRegistry"; private static final String MBEAN_TYPE = "SigarRegistry";
private static final int CACHE_EXPIRE = 60 * 1000;
private Sigar sigarImpl;
private SigarProxy sigar; private SigarProxy sigar;
private final String objectName; private String objectName;
private final ArrayList managedBeans; private ArrayList managedBeans;
private MBeanServer mbeanServer; private MBeanServer mbeanServer;
public SigarRegistry() {}
public SigarRegistry(SigarProxy sigar) { public SigarRegistry(SigarProxy sigar) {
init(sigar);
}
private void init(SigarProxy sigar) {
this.sigar = sigar; this.sigar = sigar;
this.objectName = this.objectName =
MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE + "=" + MBEAN_TYPE; MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE + "=" + MBEAN_TYPE;
this.managedBeans = new ArrayList(); this.managedBeans = new ArrayList();
} }
public String getObjectName() { public String getObjectName() {
@ -99,15 +109,24 @@ public class SigarRegistry implements MBeanRegistration, SigarRegistryMBean {
// ------- // -------
public ObjectName preRegister(MBeanServer server, ObjectName name) public ObjectName preRegister(MBeanServer server, ObjectName name)
throws Exception { throws Exception {
this.mbeanServer = server;
return new ObjectName(getObjectName()); //no args constructor support
if (this.sigar == null) {
this.sigarImpl = new Sigar();
init(SigarProxyCache.newInstance(this.sigarImpl, CACHE_EXPIRE));
}
this.mbeanServer = server;
if (name == null) {
return new ObjectName(getObjectName());
}
else {
return name;
}
} }
/** /**
* Registers the default set of Sigar MBeans. Before doing so, a super call * Registers the default set of Sigar MBeans.
* is made to satisfy {@link AbstractMBean}.
*
* @see AbstractMBean#postRegister(Boolean)
*/ */
public void postRegister(Boolean success) { public void postRegister(Boolean success) {
ReflectedMBean mbean; ReflectedMBean mbean;
@ -205,7 +224,7 @@ public class SigarRegistry implements MBeanRegistration, SigarRegistryMBean {
/** /**
* Deregisters all Sigar MBeans that were created and registered using this * Deregisters all Sigar MBeans that were created and registered using this
* instance. After doing so, a super call is made to satisfy {@link AbstractMBean}. * instance.
* @throws Exception * @throws Exception
* *
* @see AbstractMBean#preDeregister() * @see AbstractMBean#preDeregister()
@ -225,6 +244,11 @@ public class SigarRegistry implements MBeanRegistration, SigarRegistryMBean {
} }
public void postDeregister() { public void postDeregister() {
this.mbeanServer = null; this.mbeanServer = null;
if (this.sigarImpl != null) {
this.sigarImpl.close();
this.sigarImpl = null;
this.sigar = null;
}
} }
} }