KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webjmx > example > SimpleDynamic


1 package org.webjmx.example;
2
3
4 // Java imports
5
//
6
import java.lang.reflect.Constructor JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import javax.management.*;
10
11 /**
12  * Simple definition of a dynamic MBean, named "SimpleDynamic".
13  *
14  * The "SimpleDynamic" dynamic MBean shows how to expose for management
15  * attributes and operations, at runtime, by implementing the
16  * "javax.management.DynamicMBean" interface.
17  *
18  * This MBean exposes for management two attributes and one operation:
19  * - the read/write "State" attribute,
20  * - the read only "NbChanges" attribute,
21  * - the "reset()" operation.
22  * It does so by putting this information in an MBeanInfo object that
23  * is returned by the getMBeanInfo() method of the DynamicMBean interface.
24  *
25  * It implements the access to its attributes through the getAttribute(),
26  * getAttributes(), setAttribute(), and setAttributes() methods of the
27  * DynamicMBean interface.
28  *
29  * It implements the invocation of its reset() operation through the
30  * invoke() method of the DynamicMBean interface.
31  *
32  * Note that as "SimpleDynamic" explicitly defines one constructor,
33  * this constructor must be public and exposed for management through
34  * the MBeanInfo object.
35  */

36
37 public class SimpleDynamic implements DynamicMBean
38 {
39     public SimpleDynamic()
40     {
41     // build the management information to be exposed by the dynamic MBean
42
//
43
buildDynamicMBeanInfo();
44     }
45
46     /*
47      * -----------------------------------------------------
48      * IMPLEMENTATION OF THE DynamicMBean INTERFACE
49      * -----------------------------------------------------
50      */

51
52     /**
53      * Allows the value of the specified attribute of the Dynamic MBean to be obtained.
54      */

55     public Object JavaDoc getAttribute(String JavaDoc attribute_name)
56     throws AttributeNotFoundException, MBeanException, ReflectionException
57     {
58
59     // Check attribute_name is not null to avoid NullPointerException later on
60
if (attribute_name == null)
61         throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Attribute name cannot be null"),
62                          "Cannot invoke a getter of " + dClassName + " with null attribute name");
63     
64     // Check for a recognized attribute_name and call the corresponding getter
65
if (attribute_name.equals("State"))
66         return getState();
67     
68     if (attribute_name.equals("NbChanges"))
69         return getNbChanges();
70     
71     // If attribute_name has not been recognized throw an AttributeNotFoundException
72
throw(new AttributeNotFoundException("Cannot find " + attribute_name + " attribute in " + dClassName));
73     }
74
75     /**
76      * Sets the value of the specified attribute of the Dynamic MBean.
77      */

78     public void setAttribute(Attribute attribute)
79     throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException
80     {
81
82     // Check attribute is not null to avoid NullPointerException later on
83
if (attribute == null) {
84         throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Attribute cannot be null"),
85                          "Cannot invoke a setter of " + dClassName + " with null attribute");
86     }
87     String JavaDoc name = attribute.getName();
88     Object JavaDoc value = attribute.getValue();
89
90     if (name == null) {
91         throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Attribute name cannot be null"),
92                          "Cannot invoke the setter of " + dClassName + " with null attribute name");
93     }
94     // Check for a recognized attribute name and call the corresponding setter
95
//
96
if (name.equals("State")) {
97         // if null value, try and see if the setter returns any exception
98
if (value == null) {
99         try {
100             setState( null );
101         } catch (Exception JavaDoc e) {
102             throw(new InvalidAttributeValueException("Cannot set attribute "+ name +" to null"));
103         }
104         }
105         // if non null value, make sure it is assignable to the attribute
106
else {
107         try {
108             if ((Class.forName("java.lang.String")).isAssignableFrom(value.getClass())) {
109             setState((String JavaDoc) value);
110             }
111             else {
112             throw(new InvalidAttributeValueException("Cannot set attribute "+ name +" to a " +
113                                  value.getClass().getName() + " object, String expected"));
114             }
115         } catch (ClassNotFoundException JavaDoc e) {
116             e.printStackTrace();
117         }
118         }
119     }
120     // recognize an attempt to set "NbChanges" attribute (read-only):
121
else if (name.equals("NbChanges")) {
122         throw(new AttributeNotFoundException("Cannot set attribute "+ name +" because it is read-only"));
123     }
124     // unrecognized attribute name:
125
else {
126         throw(new AttributeNotFoundException("Attribute " + name +
127                          " not found in " + this.getClass().getName()));
128     }
129     }
130
131     /**
132      * Enables the to get the values of several attributes of the Dynamic MBean.
133      */

134     public AttributeList getAttributes(String JavaDoc[] attributeNames) {
135
136     // Check attributeNames is not null to avoid NullPointerException later on
137
if (attributeNames == null) {
138         throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("attributeNames[] cannot be null"),
139                          "Cannot invoke a getter of " + dClassName);
140     }
141     AttributeList resultList = new AttributeList();
142
143     // if attributeNames is empty, return an empty result list
144
if (attributeNames.length == 0)
145         return resultList;
146         
147     // build the result attribute list
148
for (int i=0 ; i<attributeNames.length ; i++){
149         try {
150         Object JavaDoc value = getAttribute((String JavaDoc) attributeNames[i]);
151         resultList.add(new Attribute(attributeNames[i],value));
152         } catch (Exception JavaDoc e) {
153         e.printStackTrace();
154         }
155     }
156     return(resultList);
157     }
158
159     /**
160      * Sets the values of several attributes of the Dynamic MBean, and returns the
161      * list of attributes that have been set.
162      */

163     public AttributeList setAttributes(AttributeList attributes) {
164
165     // Check attributes is not null to avoid NullPointerException later on
166
if (attributes == null) {
167         throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("AttributeList attributes cannot be null"),
168                          "Cannot invoke a setter of " + dClassName);
169     }
170     AttributeList resultList = new AttributeList();
171
172     // if attributeNames is empty, nothing more to do
173
if (attributes.isEmpty())
174         return resultList;
175
176     // for each attribute, try to set it and add to the result list if successfull
177
for (Iterator JavaDoc i = attributes.iterator(); i.hasNext();) {
178         Attribute attr = (Attribute) i.next();
179         try {
180         setAttribute(attr);
181         String JavaDoc name = attr.getName();
182         Object JavaDoc value = getAttribute(name);
183         resultList.add(new Attribute(name,value));
184         } catch(Exception JavaDoc e) {
185         e.printStackTrace();
186         }
187     }
188     return(resultList);
189     }
190
191     /**
192      * Allows an operation to be invoked on the Dynamic MBean.
193      */

194     public Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc params[], String JavaDoc signature[])
195     throws MBeanException,
196            ReflectionException {
197
198     // Check operationName is not null to avoid NullPointerException later on
199
if (operationName == null) {
200         throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Operation name cannot be null"),
201                          "Cannot invoke a null operation in " + dClassName);
202     }
203     // Check for a recognized operation name and call the corresponding operation
204
if (operationName.equals("reset")){
205         reset();
206         return null;
207     } else {
208         // unrecognized operation name:
209
throw new ReflectionException(new NoSuchMethodException JavaDoc(operationName),
210                       "Cannot find the operation " + operationName + " in " + dClassName);
211     }
212     }
213
214     /**
215      * This method provides the exposed attributes and operations of the Dynamic MBean.
216      * It provides this information using an MBeanInfo object.
217      */

218     public MBeanInfo getMBeanInfo() {
219
220     // return the information we want to expose for management:
221
// the dMBeanInfo private field has been built at instanciation time,
222
return(dMBeanInfo);
223     }
224
225
226     /*
227      * -----------------------------------------------------
228      * OTHER PUBLIC METHODS
229      * -----------------------------------------------------
230      */

231
232     /**
233      * Getter: get the "State" attribute of the "SimpleDynamic" dynamic MBean.
234      */

235     public String JavaDoc getState() {
236     return state;
237     }
238
239     /**
240      * Setter: set the "State" attribute of the "SimpleDynamic" dynamic MBean.
241      */

242     public void setState(String JavaDoc s) {
243     state = s;
244     nbChanges++;
245     }
246
247     /**
248      * Getter: get the "NbChanges" attribute of the "SimpleDynamic" dynamic MBean.
249      */

250     public Integer JavaDoc getNbChanges() {
251     return new Integer JavaDoc(nbChanges);
252     }
253
254     /**
255      * Operation: reset to their initial values the "State" and "NbChanges"
256      * attributes of the "SimpleDynamic" dynamic MBean.
257      */

258     public void reset() {
259     state = "initial state";
260     nbChanges = 0;
261     nbResets++;
262     }
263
264     /**
265      * Return the "NbResets" property.
266      * This method is not a Getter in the JMX sense because
267      * it is not returned by the getMBeanInfo() method.
268      */

269     public Integer JavaDoc getNbResets() {
270     return new Integer JavaDoc(nbResets);
271     }
272
273     /*
274      * -----------------------------------------------------
275      * PRIVATE METHODS
276      * -----------------------------------------------------
277      */

278   
279     /**
280      * Build the private dMBeanInfo field,
281      * which represents the management interface exposed by the MBean;
282      * that is, the set of attributes, constructors, operations and notifications
283      * which are available for management.
284      *
285      * A reference to the dMBeanInfo object is returned by the getMBeanInfo() method
286      * of the DynamicMBean interface. Note that, once constructed, an MBeanInfo object is immutable.
287      */

288     private void buildDynamicMBeanInfo() {
289
290     dAttributes[0] = new MBeanAttributeInfo("State",
291                         "java.lang.String",
292                         "State: state string.",
293                         true,
294                         true,
295                         false);
296     dAttributes[1] = new MBeanAttributeInfo("NbChanges",
297                         "java.lang.Integer",
298                         "NbChanges: number of times the State string has been changed.",
299                         true,
300                         false,
301                         false);
302         
303     Constructor JavaDoc[] constructors = this.getClass().getConstructors();
304     dConstructors[0] = new MBeanConstructorInfo("SimpleDynamic(): Constructs a SimpleDynamic object",
305                             constructors[0]);
306         
307     MBeanParameterInfo[] params = null;
308     dOperations[0] = new MBeanOperationInfo("reset",
309                         "reset(): reset State and NbChanges attributes to their initial values",
310                         params ,
311                         "void",
312                         MBeanOperationInfo.ACTION);
313         
314     dMBeanInfo = new MBeanInfo(dClassName,
315                    dDescription,
316                    dAttributes,
317                    dConstructors,
318                    dOperations,
319                    new MBeanNotificationInfo[0]);
320     }
321
322     /*
323      * -----------------------------------------------------
324      * PRIVATE VARIABLES
325      * -----------------------------------------------------
326      */

327
328     private String JavaDoc state = "initial state";
329     private int nbChanges = 0;
330     private int nbResets = 0;
331
332
333     private String JavaDoc dClassName = this.getClass().getName();
334     private String JavaDoc dDescription = "Simple implementation of a dynamic MBean.";
335
336     private MBeanAttributeInfo[] dAttributes = new MBeanAttributeInfo[2];
337     private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
338     private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1];
339     private MBeanInfo dMBeanInfo = null;
340   
341 }
342
Free Books   Free Magazines  
Popular Tags