From 1c87169bdb0fe84f76d55b47292cb1bfb36e856b Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 29 Apr 2009 12:58:45 -0700 Subject: [PATCH] leave SigarProxy impl to the caller --- .../java/src/org/hyperic/sigar/cmd/Mx.java | 2 +- .../org/hyperic/sigar/jmx/AbstractMBean.java | 72 ++----------------- .../org/hyperic/sigar/jmx/ReflectedMBean.java | 8 +-- .../hyperic/sigar/jmx/SigarLoadAverage.java | 21 ++---- .../org/hyperic/sigar/jmx/SigarRegistry.java | 41 +++++------ .../src/org/hyperic/sigar/test/TestMx.java | 4 +- 6 files changed, 37 insertions(+), 111 deletions(-) diff --git a/bindings/java/src/org/hyperic/sigar/cmd/Mx.java b/bindings/java/src/org/hyperic/sigar/cmd/Mx.java index 642464ae..07ce94e4 100644 --- a/bindings/java/src/org/hyperic/sigar/cmd/Mx.java +++ b/bindings/java/src/org/hyperic/sigar/cmd/Mx.java @@ -68,7 +68,7 @@ public class Mx extends SigarCommandBase { if (isRegistered) { return; } - SigarRegistry registry = new SigarRegistry(); + SigarRegistry registry = new SigarRegistry(this.proxy); try { server.registerMBean(registry, null); SigarProcess proc = new SigarProcess(); diff --git a/bindings/java/src/org/hyperic/sigar/jmx/AbstractMBean.java b/bindings/java/src/org/hyperic/sigar/jmx/AbstractMBean.java index f7b0db10..fd38b172 100644 --- a/bindings/java/src/org/hyperic/sigar/jmx/AbstractMBean.java +++ b/bindings/java/src/org/hyperic/sigar/jmx/AbstractMBean.java @@ -31,7 +31,6 @@ import javax.management.ReflectionException; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarProxy; -import org.hyperic.sigar.SigarProxyCache; /** * Base class for all Sigar JMX MBeans. Provides a skeleton which handles @@ -49,21 +48,6 @@ public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration { public static final String MBEAN_DOMAIN = SigarInvokerJMX.DOMAIN_NAME; public static final String MBEAN_ATTR_TYPE = SigarInvokerJMX.PROP_TYPE; - protected static final short CACHED_30SEC = 0; - - protected static final short CACHED_5SEC = 1; - - protected static final short CACHED_500MS = 2; - - protected static final short CACHELESS = 3; - - protected static final short DEFAULT = CACHED_30SEC; - - /** - * The Sigar implementation to be used to fetch information from the system. - */ - protected final Sigar sigarImpl; - /** * The Sigar proxy cache to be used in case the data does not have to be * fetched during each call. The cache timeout is decided during @@ -85,59 +69,13 @@ public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration { protected MBeanServer mbeanServer; /** - *

Creates a new instance of this class. The Sigar instance is stored (and - * accessible) via the {@link #sigarImpl} member. A second instance is - * stored within the {@link #sigar} member which is either {@link #sigarImpl} - * or an instance of {@link SigarProxyCache} with the expiration time set to - * whatever the cacheMode parameter specifies.

+ *

Creates a new instance of this class. The SigarProxy instance is stored (and + * accessible) via the {@link #sigar} member. * - *

The following cache modes exist:

- * - * - * - * - * - * - * - * - *
ConstantDescription
{@link #CACHELESS}No cached instance, {@link #sigar} - * == {@link #sigarImpl}.
{@link #CACHED_500MS}500 millisecond cache, for high - * frequency queries on raw data such as reading out CPU timers each - * second. Avoids reading out multiple data sets when all attributes of - * an MBean are queried in short sequence.
{@link #CACHED_5SEC}5 second cache, for high frequency - * queries on calculated data such as CPU percentages.
{@link #CACHED_30SEC}30 second cache, for normal queries - * or data readouts such as CPU model / vendor. This is the default if - * nothing (0) is specified.
{@link #DEFAULT}Same as {@link #CACHED_30SEC}.
- * - *

Note: Only make use of the cacheless or half second mode if you - * know what you are doing. They may have impact on system performance if - * used excessively.

- * - * @param sigar The Sigar impl to use. Must not be null - * @param cacheMode The cache mode to use for {@link #sigar} or {@link #CACHELESS} - * if no separate, cached instance is to be maintained. + * @param sigar The SigarProxy instance to use. Must not be null */ - protected AbstractMBean(Sigar sigar, short cacheMode) { - // store Sigar - this.sigarImpl = sigar; - - // create a cached instance as well - if (cacheMode == CACHELESS) { - // no cached version - this.sigar = this.sigarImpl; - - } else if (cacheMode == CACHED_500MS) { - // 500ms cached version (for 1/sec queries) - this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 500); - - } else if (cacheMode == CACHED_5SEC) { - // 5sec cached version (for avg'd queries) - this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 5000); - - } else /* if (cacheMode == CACHED_30SEC) */{ - // 30sec (default) cached version (for info and long term queries) - this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 30000); - } + protected AbstractMBean(SigarProxy sigar) { + this.sigar = sigar; } /** diff --git a/bindings/java/src/org/hyperic/sigar/jmx/ReflectedMBean.java b/bindings/java/src/org/hyperic/sigar/jmx/ReflectedMBean.java index bb62b1ab..162f4930 100644 --- a/bindings/java/src/org/hyperic/sigar/jmx/ReflectedMBean.java +++ b/bindings/java/src/org/hyperic/sigar/jmx/ReflectedMBean.java @@ -29,7 +29,7 @@ import javax.management.MBeanException; import javax.management.MBeanInfo; import javax.management.ReflectionException; -import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarProxy; public class ReflectedMBean extends AbstractMBean { @@ -96,15 +96,15 @@ public class ReflectedMBean extends AbstractMBean { } } - protected ReflectedMBean(Sigar sigar, String type) { - super(sigar, CACHED_5SEC); + protected ReflectedMBean(SigarProxy sigar, String type) { + super(sigar); this.type = type; this.name = MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE + "=" + getType(); } - protected ReflectedMBean(Sigar sigar, String type, String arg) { + protected ReflectedMBean(SigarProxy sigar, String type, String arg) { this(sigar, type); this.name += ",Name=" + encode(arg); } diff --git a/bindings/java/src/org/hyperic/sigar/jmx/SigarLoadAverage.java b/bindings/java/src/org/hyperic/sigar/jmx/SigarLoadAverage.java index a1808571..19962477 100644 --- a/bindings/java/src/org/hyperic/sigar/jmx/SigarLoadAverage.java +++ b/bindings/java/src/org/hyperic/sigar/jmx/SigarLoadAverage.java @@ -29,6 +29,7 @@ import javax.management.ReflectionException; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarNotImplementedException; +import org.hyperic.sigar.SigarProxy; /** * Sigar JMX MBean implementation for the LoadAverage information @@ -122,16 +123,6 @@ public class SigarLoadAverage extends AbstractMBean { */ private boolean notImplemented; - /** - * Creates a new instance, using a new Sigar instance to fetch the data. - * - * @throws IllegalArgumentException - * If an unexpected Sigar error occurs. - */ - public SigarLoadAverage() throws IllegalArgumentException { - this(new Sigar()); - } - /** * Creates a new instance, using the Sigar instance specified to fetch the * data. @@ -142,8 +133,8 @@ public class SigarLoadAverage extends AbstractMBean { * @throws IllegalArgumentException * If an unexpected Sigar error occurs */ - public SigarLoadAverage(Sigar sigar) throws IllegalArgumentException { - super(sigar, CACHED_30SEC); + public SigarLoadAverage(SigarProxy sigar) throws IllegalArgumentException { + super(sigar); // all fine this.objectName = MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE @@ -164,7 +155,7 @@ public class SigarLoadAverage extends AbstractMBean { */ public double getLastMinute() { try { - return sigarImpl.getLoadAverage()[0]; + return this.sigar.getLoadAverage()[0]; } catch (SigarNotImplementedException e) { return NOT_IMPLEMENTED_LOAD_VALUE; @@ -181,7 +172,7 @@ public class SigarLoadAverage extends AbstractMBean { */ public double getLastFiveMinutes() { try { - return sigarImpl.getLoadAverage()[1]; + return this.sigar.getLoadAverage()[1]; } catch (SigarNotImplementedException e) { return NOT_IMPLEMENTED_LOAD_VALUE; @@ -197,7 +188,7 @@ public class SigarLoadAverage extends AbstractMBean { */ public double getLast15Minutes() { try { - return sigarImpl.getLoadAverage()[2]; + return this.sigar.getLoadAverage()[2]; } catch (SigarNotImplementedException e) { return NOT_IMPLEMENTED_LOAD_VALUE; diff --git a/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java b/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java index eb26d986..f055b0aa 100644 --- a/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java +++ b/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java @@ -33,6 +33,7 @@ import org.hyperic.sigar.FileSystem; import org.hyperic.sigar.NetInterfaceConfig; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; +import org.hyperic.sigar.SigarProxy; /** *

Registry of all Sigar MBeans. Can be used as a convenient way to invoke @@ -90,12 +91,8 @@ public class SigarRegistry extends AbstractMBean { private final ArrayList managedBeans; - /** - * Creates a new instance of this class. Will create the Sigar instance this - * class uses when constructing other MBeans. - */ - public SigarRegistry() { - super(new Sigar(), CACHELESS); + public SigarRegistry(SigarProxy sigar) { + super(sigar); this.objectName = MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE + "=" + MBEAN_TYPE; this.managedBeans = new ArrayList(); @@ -166,26 +163,26 @@ public class SigarRegistry extends AbstractMBean { for (int i=0; i