The object instance that actually embodies the behavior of the managed resource is called the target object. The last step in creating a model MBean is to give the MBean skeleton and its defined management interface a reference to the target object. Thereafter, the model MBean can handle management requests, forward them to the target object, and handle the response.
Example 3–2 implements the TestBean class that is the simple managed resource in our example. Its methods provide the implementation for two attributes and one operation.
public class TestBean implements java.io.Serializable { // Constructor // public TestBean() { echo("\n\tTestBean Constructor Invoked: State " + state + " nbChanges: " + nbChanges + " nbResets: " + nbResets); } // Getter and setter for the "State" attribute // public String getState() { echo("\n\tTestBean: getState invoked: " + state); return state; } public void setState(String s) { state = s; nbChanges++; echo("\n\tTestBean: setState to " + state + " nbChanges: " + nbChanges); } // Getter for the read-only "NbChanges" attribute // public Integer getNbChanges() { echo("\n\tTestBean: getNbChanges invoked: " + nbChanges); return new Integer(nbChanges); } // Method of the "Reset" operation // public void reset() { echo("\n\tTestBean: reset invoked "); state = "reset initial state"; nbChanges = 0; nbResets++; } // Other public method; looks like a getter, // but no NbResets attribute is defined in // the management interface of the model MBean // public Integer getNbResets() { echo("\n\tTestBean: getNbResets invoked: " + nbResets); return new Integer(nbResets); } // Internals // private void echo(String outstr) { System.out.println(outstr); } private String state = "initial state"; private int nbChanges = 0; private int nbResets = 0; } |
By default, the model MBean handles a managed resource that is contained in one object instance. This target is specified through the setManagedResource method defined by the ModelMBean interface. The resource can encompass several programmatic objects because individual attributes or operations can be handled by different target objects. This behavior is configured through the optional targetObject and targetType descriptor fields of each attribute or operation.
In Example 3–3, one of the operations is handled by an instance of the TestBeanFriend class. In the definition of this operation's descriptor, we set this instance as the target object. We then create the operation's ModelMBeanOperationInfo with this descriptor and add it to the list of operations in the metadata for our model MBean.
MBeanParameterInfo[] params = null; Descriptor getNbResetsDesc = new DescriptorSupport(new String[] { "name=getNbResets", "class=TestBeanFriend", "descriptorType=operation", "role=operation"}); TestBeanFriend tbf = new TestBeanFriend(); getNbResetsDesc.setField("targetObject",tbf); getNbResetsDesc.setField("targetType","objectReference"); dOperations[1] = new ModelMBeanOperationInfo( "getNbResets", "getNbResets(): get number of resets performed", params , "java.lang.Integer", MBeanOperationInfo.INFO, getNbResetsDesc); |