// $Id: SampleADM.java,v 1.8 2007/07/20 01:32:39 blaine Exp $ package tst; import java.util.*; import mx4j.AbstractDynamicMBean; import javax.management.MBeanAttributeInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanConstructorInfo; /** * Exemplifies MX4J AbstractDynamicMBean usage, including some very * useful idioms. * * This shows the most straight forward strategy of implementing a * Dynamic MBean: subclassing AbstractDynamicMBean directly in * the same class that has the methods to be exposed. *
* The members of this class are laid out in a sequence that shows * method/cons implementations along with the JMX methods needed to * publish them. * For a production use class, you will normally group members * according to the interface that they satisfy, etc. * * @see mx4j.AbstractDynamicMBean */ public class SampleADM extends AbstractDynamicMBean { private ArrayList al = new ArrayList(); private Map hm = new HashMap(); private double da[] = { 1.1, 2.2, 3.3 }; private String[] sa = { "eins", "zwei", "drei" }; private Integer i = new Integer(7); private double d = 7.8; private int h = 9; String[] three = { "one", "two", "three" }; protected String getMBeanDescription() { return "Class Descript for " + getClass().getName(); } // Getter/Setter implementations. public ArrayList getAL() { return al; } public Map getHM() { return hm; } public double[] getDA() { return da; } public String[] getSA() { return sa; } public Integer getI() { return i; } public double getD() { return d; } public int getH() { return h; } public void setAL(ArrayList inAL) { al = inAL; } public void setHM(Map inHM) { hm = inHM; } public void setDA(double[] inDA) { da = inDA; } public void setSA(String[] inSA) { sa = inSA; } public void setI(Integer inI) { i = inI; } public void setD(double inD) { d = inD; } public void setH(int inH) { h = inH; } // Publish Attributes corresponding to Getter/setter methods. protected MBeanAttributeInfo[] createMBeanAttributeInfo() { return new MBeanAttributeInfo[] { // new MBeanAttributeInfo("attName", "class String", "descript", // readable, writable, isable), new MBeanAttributeInfo("AL", ArrayList.class.getName(), "AL att descript.", true, true, false), new MBeanAttributeInfo("DA", "[D", "DA att descript.", true, true, false), new MBeanAttributeInfo("SA", "[Ljava.lang.String;", "SA att descript.", true, true, false), new MBeanAttributeInfo("I", Integer.class.getName(), "I att descript.", true, false, false), new MBeanAttributeInfo("D", double.class.getName(), "D att descript.", true, true, false), new MBeanAttributeInfo("H", int.class.getName(), "H att descript.", false, true, false), new MBeanAttributeInfo("HM", HashMap.class.getName(), "HM att descript.", true, true, false), }; // Purposefully not publishing getH() and setI(). } // Operation implementations. (I.e., all published methods that // are neither cons nor getter/setters of form get() or set(x). public String[] get3(String s) { return three; } public float get22(int i) { return 2.2F; } public boolean classOk(String s, Class c) { return true; } public void times2() { d *= 2; } public String hidden() { return "A secret"; } protected MBeanOperationInfo[] createMBeanOperationInfo() { Class[] caGet3 = { String.class }; Class[] caGet22 = { int.class }; Class[] caClassOk = { String.class, Class.class }; try { return new MBeanOperationInfo[] { // new MBeanOperationInfo("descript", // getClass().getMethod("methodName", paramClassArray)), new MBeanOperationInfo("3 op descript.", getClass().getMethod("get3", caGet3)), new MBeanOperationInfo("22 op descript.", getClass().getMethod("get22", caGet22)), new MBeanOperationInfo("classOk op descript.", getClass().getMethod("classOk", caClassOk)), new MBeanOperationInfo("times2 op descript.", getClass().getMethod("times2", (Class[]) null)), }; // Purposefully not publishing method "hidden()". } catch (NoSuchMethodException nsme) { System.err.println("Bad ADM Operation setup for '" + getClass().getName() + "': " + nsme); return null; } } // Public constructor implementations public SampleADM(Integer iIn, Class cIn, double dIn) { this(); } public SampleADM(Integer iIn, double dIn) { this(); i = iIn; d = dIn; } public SampleADM() { al.add("one"); al.add("two"); al.add("three"); hm.put("1", "uno"); hm.put("2", "dos"); hm.put("3", "tres"); } protected MBeanConstructorInfo[] createMBeanConstructorInfo() { try { Class[] ca3arg = { Integer.class, Class.class, double.class }; Class[] ca2arg = { Integer.class, double.class }; return new MBeanConstructorInfo[] { // new MBeanConstructorInfo("descript", // getClass().getConstructor(parameterClassArray)), new MBeanConstructorInfo( "Instantiates w/ given Integer and double", getClass().getConstructor(ca2arg)), new MBeanConstructorInfo("No-arg constructor", getClass().getConstructor((Class[]) null)), new MBeanConstructorInfo( "Like 2-arg cons, but also takes a Class", getClass().getConstructor(ca3arg)), }; // You should publish ALL public constructors, since JMX // does not hide them whether you publish or not. } catch (NoSuchMethodException nsme) { System.err.println("Bad ADM Constructor setup for '" + getClass().getName() + "': " + nsme); return null; } } }