This adds an example for printing out the info on the management beans dynamically. In the process, it also adds toString() output to the info classes so that they print out something useful, and fixes a bug in StandardMBean.
Mark, another one for 0.92?
Changelog:
2006-07-29 Andrew John Hughes <[EMAIL PROTECTED]>
* examples/gnu/classpath/examples/management/TestBeans.java:
New file.
* javax/management/MBeanAttributeInfo.java:
(toString()): Implemented.
* javax/management/MBeanConstructorInfo.java:
(toString()): Implemented.
* javax/management/MBeanFeatureInfo.java:
(toString()): Implemented.
* javax/management/MBeanInfo.java:
(toString()): Implemented.
* javax/management/MBeanNotificationInfo.java:
(toString()): Implemented.
* javax/management/MBeanOperationInfo.java:
(toString()): Implemented.
* javax/management/MBeanParameterInfo.java:
(toString()): Implemented.
* javax/management/StandardMBean.java:
(getMBeanInfo()): Fix attribute naming.
--
Andrew :-)
Escape the Java Trap with GNU Classpath!
http://www.gnu.org/philosophy/java-trap.html
public class gcj extends Freedom implements Java { ... }
Index: examples/gnu/classpath/examples/management/TestBeans.java
===================================================================
RCS file: examples/gnu/classpath/examples/management/TestBeans.java
diff -N examples/gnu/classpath/examples/management/TestBeans.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ examples/gnu/classpath/examples/management/TestBeans.java 29 Jul 2006
21:17:36 -0000
@@ -0,0 +1,55 @@
+/* TestBeans.java -- Tests the dynamic interface of the beans.
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath examples.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA. */
+
+package gnu.classpath.examples.management;
+
+import java.lang.management.ManagementFactory;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.management.DynamicMBean;
+
+public class TestBeans
+{
+ public static void main(String[] args)
+ throws Exception
+ {
+ List beans = new ArrayList();
+ /* FIXME: When there's a server, this will be easier... :) */
+ beans.add(ManagementFactory.getOperatingSystemMXBean());
+ beans.add(ManagementFactory.getRuntimeMXBean());
+ beans.add(ManagementFactory.getThreadMXBean());
+ beans.add(ManagementFactory.getCompilationMXBean());
+ beans.add(ManagementFactory.getClassLoadingMXBean());
+ beans.add(ManagementFactory.getMemoryMXBean());
+ beans.addAll(ManagementFactory.getMemoryPoolMXBeans());
+ beans.addAll(ManagementFactory.getMemoryManagerMXBeans());
+ beans.addAll(ManagementFactory.getGarbageCollectorMXBeans());
+ Iterator it = beans.iterator();
+ while (it.hasNext())
+ {
+ DynamicMBean bean = (DynamicMBean) it.next();
+ if (bean != null)
+ System.out.println(bean.getMBeanInfo());
+ }
+ }
+}
Index: javax/management/MBeanAttributeInfo.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/management/MBeanAttributeInfo.java,v
retrieving revision 1.1
diff -u -3 -p -u -r1.1 MBeanAttributeInfo.java
--- javax/management/MBeanAttributeInfo.java 24 Jul 2006 20:34:33 -0000
1.1
+++ javax/management/MBeanAttributeInfo.java 29 Jul 2006 21:17:37 -0000
@@ -265,4 +265,37 @@ public class MBeanAttributeInfo
return isWrite;
}
+ /**
+ * <p>
+ * Returns a textual representation of this instance. This
+ * is constructed using the class name
+ * (<code>javax.management.MBeanAttributeInfo</code>),
+ * the name, description and type of the attribute and the
+ * current settings of the [EMAIL PROTECTED] #isReadable()},
+ * [EMAIL PROTECTED] #isWritable()} and [EMAIL PROTECTED] #isIs()}
properties.
+ * </p>
+ * <p>
+ * As instances of this class are immutable, the return value
+ * is computed just once for each instance and reused
+ * throughout its life.
+ * </p>
+ *
+ * @return a @link{java.lang.String} instance representing
+ * the instance in textual form.
+ */
+ public String toString()
+ {
+ if (string == null)
+ {
+ super.toString();
+ string = string.substring(0, string.length() - 1)
+ + ",type=" + attributeType
+ + ",isReadable=" + (isRead ? "yes" : "no")
+ + ",isWritable=" + (isWrite ? "yes" : "no")
+ + ",isIs=" + (is ? "yes" : "no")
+ + "]";
+ }
+ return string;
+ }
+
}
Index: javax/management/MBeanConstructorInfo.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/management/MBeanConstructorInfo.java,v
retrieving revision 1.2
diff -u -3 -p -u -r1.2 MBeanConstructorInfo.java
--- javax/management/MBeanConstructorInfo.java 26 Jul 2006 21:03:47 -0000
1.2
+++ javax/management/MBeanConstructorInfo.java 29 Jul 2006 21:17:37 -0000
@@ -196,4 +196,33 @@ public class MBeanConstructorInfo
return super.hashCode() + Arrays.hashCode(signature);
}
+ /**
+ * <p>
+ * Returns a textual representation of this instance. This
+ * is constructed using the class name
+ * (<code>javax.management.MBeanConstructorInfo</code>),
+ * the name and description of the constructor and the
+ * contents of the array of parameters.
+ * </p>
+ * <p>
+ * As instances of this class are immutable, the return value
+ * is computed just once for each instance and reused
+ * throughout its life.
+ * </p>
+ *
+ * @return a @link{java.lang.String} instance representing
+ * the instance in textual form.
+ */
+ public String toString()
+ {
+ if (string == null)
+ {
+ super.toString();
+ string = string.substring(0, string.length() - 1)
+ + ",signature=" + Arrays.toString(signature)
+ + "]";
+ }
+ return string;
+ }
+
}
Index: javax/management/MBeanFeatureInfo.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/management/MBeanFeatureInfo.java,v
retrieving revision 1.3
diff -u -3 -p -u -r1.3 MBeanFeatureInfo.java
--- javax/management/MBeanFeatureInfo.java 29 Jul 2006 14:05:59 -0000
1.3
+++ javax/management/MBeanFeatureInfo.java 29 Jul 2006 21:17:37 -0000
@@ -77,6 +77,11 @@ public class MBeanFeatureInfo
protected String name;
/**
+ * The <code>toString()</code> result of this instance.
+ */
+ protected transient String string;
+
+ /**
* Constructs a new [EMAIL PROTECTED] MBeanFeatureInfo} with the specified
* name and description.
*
@@ -152,4 +157,30 @@ public class MBeanFeatureInfo
+ (description == null ? -1 : description.hashCode());
}
+ /**
+ * <p>
+ * Returns a textual representation of this instance. This
+ * is constructed using the class name
+ * (<code>javax.management.MBeanFeatureInfo</code>) and
+ * the name and description of the feature.
+ * </p>
+ * <p>
+ * As instances of this class are immutable, the return value
+ * is computed just once for each instance and reused
+ * throughout its life.
+ * </p>
+ *
+ * @return a @link{java.lang.String} instance representing
+ * the instance in textual form.
+ */
+ public String toString()
+ {
+ if (string == null)
+ string = getClass().getName()
+ + "[name=" + name
+ + ",desc=" + description
+ + "]";
+ return string;
+ }
+
}
Index: javax/management/MBeanInfo.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/management/MBeanInfo.java,v
retrieving revision 1.3
diff -u -3 -p -u -r1.3 MBeanInfo.java
--- javax/management/MBeanInfo.java 26 Jul 2006 21:03:47 -0000 1.3
+++ javax/management/MBeanInfo.java 29 Jul 2006 21:17:37 -0000
@@ -128,6 +128,11 @@ public class MBeanInfo
private MBeanNotificationInfo[] notifications;
/**
+ * The <code>toString()</code> result of this instance.
+ */
+ private transient String string;
+
+ /**
* Constructs a new [EMAIL PROTECTED] MBeanInfo} using the supplied
* class name and description with the given attributes,
* operations, constructors and notifications. The class
@@ -358,4 +363,35 @@ public class MBeanInfo
+ Arrays.hashCode(operations) + Arrays.hashCode(notifications);
}
+ /**
+ * <p>
+ * Returns a textual representation of this instance. This
+ * is constructed using the class name
+ * (<code>javax.management.MBeanInfo</code>),
+ * the name and description of the bean and the contents
+ * of the four arrays.
+ * </p>
+ * <p>
+ * As instances of this class are immutable, the return value
+ * is computed just once for each instance and reused
+ * throughout its life.
+ * </p>
+ *
+ * @return a @link{java.lang.String} instance representing
+ * the instance in textual form.
+ */
+ public String toString()
+ {
+ if (string == null)
+ string = getClass().getName()
+ + "[name=" + className
+ + ",desc=" + description
+ + ",attributes=" + Arrays.toString(attributes)
+ + ",constructors=" + Arrays.toString(constructors)
+ + ",operations=" + Arrays.toString(operations)
+ + ",notifications=" + Arrays.toString(notifications)
+ + "]";
+ return string;
+ }
+
}
Index: javax/management/MBeanNotificationInfo.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/management/MBeanNotificationInfo.java,v
retrieving revision 1.3
diff -u -3 -p -u -r1.3 MBeanNotificationInfo.java
--- javax/management/MBeanNotificationInfo.java 25 Jul 2006 21:27:19 -0000
1.3
+++ javax/management/MBeanNotificationInfo.java 29 Jul 2006 21:17:37 -0000
@@ -195,4 +195,33 @@ public class MBeanNotificationInfo
return super.hashCode() + Arrays.hashCode(types);
}
+ /**
+ * <p>
+ * Returns a textual representation of this instance. This
+ * is constructed using the class name
+ * (<code>javax.management.MBeanNotificationInfo</code>),
+ * the name and description of the notification and the
+ * contents of the array of types.
+ * </p>
+ * <p>
+ * As instances of this class are immutable, the return value
+ * is computed just once for each instance and reused
+ * throughout its life.
+ * </p>
+ *
+ * @return a @link{java.lang.String} instance representing
+ * the instance in textual form.
+ */
+ public String toString()
+ {
+ if (string == null)
+ {
+ super.toString();
+ string = string.substring(0, string.length() - 1)
+ + ",types=" + Arrays.toString(types)
+ + "]";
+ }
+ return string;
+ }
+
}
Index: javax/management/MBeanOperationInfo.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/management/MBeanOperationInfo.java,v
retrieving revision 1.1
diff -u -3 -p -u -r1.1 MBeanOperationInfo.java
--- javax/management/MBeanOperationInfo.java 26 Jul 2006 21:03:47 -0000
1.1
+++ javax/management/MBeanOperationInfo.java 29 Jul 2006 21:17:37 -0000
@@ -292,4 +292,53 @@ public class MBeanOperationInfo
+ type.hashCode() + Integer.valueOf(impact).hashCode();
}
+ /**
+ * <p>
+ * Returns a textual representation of this instance. This
+ * is constructed using the class name
+ * (<code>javax.management.MBeanOperationInfo</code>),
+ * the name, description, return type and impact of the
+ * operation and the contents of the array of parameters.
+ * </p>
+ * <p>
+ * As instances of this class are immutable, the return value
+ * is computed just once for each instance and reused
+ * throughout its life.
+ * </p>
+ *
+ * @return a @link{java.lang.String} instance representing
+ * the instance in textual form.
+ */
+ public String toString()
+ {
+ if (string == null)
+ {
+ String impactString;
+ switch (impact)
+ {
+ case INFO:
+ impactString = "INFO";
+ break;
+ case ACTION:
+ impactString = "ACTION";
+ break;
+ case ACTION_INFO:
+ impactString = "ACTION_INFO";
+ break;
+ case UNKNOWN:
+ impactString = "UNKNOWN";
+ break;
+ default:
+ impactString = "ERRONEOUS VALUE";
+ }
+ super.toString();
+ string = string.substring(0, string.length() - 1)
+ + ",returnType=" + type
+ + ",impact=" + impactString
+ + ",signature=" + Arrays.toString(signature)
+ + "]";
+ }
+ return string;
+ }
+
}
Index: javax/management/MBeanParameterInfo.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/javax/management/MBeanParameterInfo.java,v
retrieving revision 1.1
diff -u -3 -p -u -r1.1 MBeanParameterInfo.java
--- javax/management/MBeanParameterInfo.java 25 Jul 2006 21:27:19 -0000
1.1
+++ javax/management/MBeanParameterInfo.java 29 Jul 2006 21:17:37 -0000
@@ -145,4 +145,32 @@ public class MBeanParameterInfo
return super.hashCode() + type.hashCode();
}
+ /**
+ * <p>
+ * Returns a textual representation of this instance. This
+ * is constructed using the class name
+ * (<code>javax.management.MBeanParameterInfo</code>) along
+ * with the name, description and type of the parameter.
+ * </p>
+ * <p>
+ * As instances of this class are immutable, the return value
+ * is computed just once for each instance and reused
+ * throughout its life.
+ * </p>
+ *
+ * @return a @link{java.lang.String} instance representing
+ * the instance in textual form.
+ */
+ public String toString()
+ {
+ if (string == null)
+ {
+ super.toString();
+ string = string.substring(0, string.length() - 1)
+ + ",type=" + type
+ + "]";
+ }
+ return string;
+ }
+
}
Index: javax/management/StandardMBean.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/management/StandardMBean.java,v
retrieving revision 1.1
diff -u -3 -p -u -r1.1 StandardMBean.java
--- javax/management/StandardMBean.java 26 Jul 2006 21:03:47 -0000 1.1
+++ javax/management/StandardMBean.java 29 Jul 2006 21:17:40 -0000
@@ -562,7 +562,13 @@ public class StandardMBean
methods[a].getParameterTypes().length == 0)
{
Method[] amethods;
- String attrib = name.substring(3);
+ String attrib;
+ if (name.startsWith("is"))
+ attrib = name.substring(2,3).toLowerCase()
+ + name.substring(3);
+ else
+ attrib = name.substring(3,4).toLowerCase()
+ + name.substring(4);
if (attributes.containsKey(attrib))
amethods = (Method[]) attributes.get(attrib);
else
@@ -577,7 +583,8 @@ public class StandardMBean
methods[a].getParameterTypes().length == 1)
{
Method[] amethods;
- String attrib = name.substring(3);
+ String attrib = name.substring(3,4).toLowerCase()
+ + name.substring(4);
if (attributes.containsKey(attrib))
amethods = (Method[]) attributes.get(attrib);
else
@@ -625,8 +632,7 @@ public class StandardMBean
MBeanConstructorInfo[] cinfo = new MBeanConstructorInfo[cons.length];
for (int a = 0; a < cinfo.length; ++a)
{
- MBeanConstructorInfo oldInfo = new MBeanConstructorInfo("",
- cons[a]);
+ MBeanConstructorInfo oldInfo = new MBeanConstructorInfo("", cons[a]);
String desc = getDescription(oldInfo);
MBeanParameterInfo[] params = oldInfo.getSignature();
MBeanParameterInfo[] pinfo = new MBeanParameterInfo[params.length];
signature.asc
Description: Digital signature
