From e0b67cf3faa6869a14fab2b6c992c7d37ebaea3f Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Fri, 3 Oct 2008 15:48:56 +0000 Subject: [PATCH] add version attributes --- .../org/hyperic/sigar/jmx/SigarRegistry.java | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java b/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java index 43fc091f..6ec42856 100644 --- a/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java +++ b/bindings/java/src/org/hyperic/sigar/jmx/SigarRegistry.java @@ -19,9 +19,13 @@ package org.hyperic.sigar.jmx; import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; import javax.management.Attribute; import javax.management.AttributeNotFoundException; +import javax.management.MBeanAttributeInfo; import javax.management.MBeanConstructorInfo; import javax.management.MBeanException; import javax.management.MBeanInfo; @@ -33,6 +37,7 @@ import javax.management.ReflectionException; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; +import org.hyperic.sigar.SigarLoader; /** *

Registry of all Sigar MBeans. Can be used as a convenient way to invoke @@ -63,6 +68,8 @@ public class SigarRegistry extends AbstractMBean { private static final String MBEAN_TYPE = "SigarRegistry"; + private static final Map VERSION_ATTRS = new LinkedHashMap(); + private static final MBeanInfo MBEAN_INFO; private static final MBeanConstructorInfo MBEAN_CONSTR_DEFAULT; @@ -87,7 +94,7 @@ public class SigarRegistry extends AbstractMBean { + "and destruction of Sigar MBeans. Any Sigar MBean created via " + "this instance will automatically be cleaned up when this " + "instance is deregistered from the MBean server.", - null /*new MBeanAttributeInfo[0]*/, + getAttributeInfo(), new MBeanConstructorInfo[] { MBEAN_CONSTR_DEFAULT }, null /*new MBeanOperationInfo[0] */, null /*new MBeanNotificationInfo[0]*/); @@ -97,6 +104,33 @@ public class SigarRegistry extends AbstractMBean { private final ArrayList managedBeans; + private static MBeanAttributeInfo[] getAttributeInfo() { + VERSION_ATTRS.put("JarVersion", Sigar.VERSION_STRING); + VERSION_ATTRS.put("NativeVersion", Sigar.NATIVE_VERSION_STRING); + VERSION_ATTRS.put("JarBuildDate", Sigar.BUILD_DATE); + VERSION_ATTRS.put("NativeBuildDate", Sigar.NATIVE_BUILD_DATE); + VERSION_ATTRS.put("JarSourceRevision", Sigar.SCM_REVISION); + VERSION_ATTRS.put("NativeSourceRevision", Sigar.NATIVE_SCM_REVISION); + VERSION_ATTRS.put("NativeLibraryName", SigarLoader.getNativeLibraryName()); + + MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[VERSION_ATTRS.size()]; + int i=0; + for (Iterator it=VERSION_ATTRS.entrySet().iterator(); + it.hasNext();) + { + Map.Entry entry = (Map.Entry)it.next(); + String name = (String)entry.getKey(); + attrs[i++] = + new MBeanAttributeInfo(name, + entry.getValue().getClass().getName(), + name, + true, // isReadable + false, // isWritable + false); // isIs + } + return attrs; + } + /** * Creates a new instance of this class. Will create the Sigar instance this * class uses when constructing other MBeans. @@ -141,7 +175,11 @@ public class SigarRegistry extends AbstractMBean { * @see javax.management.DynamicMBean#getAttribute(java.lang.String) */ public Object getAttribute(String attr) throws AttributeNotFoundException { - throw new AttributeNotFoundException(attr); + Object obj = VERSION_ATTRS.get(attr); + if (obj == null) { + throw new AttributeNotFoundException(attr); + } + return obj; } /* (non-Javadoc)