KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jmx > JEApplicationMBean


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: JEApplicationMBean.java,v 1.6 2006/10/30 21:14:05 bostic Exp $
7  */

8
9 package jmx;
10
11 import java.io.File JavaDoc;
12 import java.lang.reflect.Constructor JavaDoc;
13 import java.util.List JavaDoc;
14
15 import javax.management.Attribute JavaDoc;
16 import javax.management.AttributeList JavaDoc;
17 import javax.management.AttributeNotFoundException JavaDoc;
18 import javax.management.DynamicMBean JavaDoc;
19 import javax.management.InvalidAttributeValueException JavaDoc;
20 import javax.management.MBeanAttributeInfo JavaDoc;
21 import javax.management.MBeanConstructorInfo JavaDoc;
22 import javax.management.MBeanException JavaDoc;
23 import javax.management.MBeanInfo JavaDoc;
24 import javax.management.MBeanNotificationInfo JavaDoc;
25 import javax.management.MBeanOperationInfo JavaDoc;
26 import javax.management.MBeanParameterInfo JavaDoc;
27
28 import com.sleepycat.je.DatabaseException;
29 import com.sleepycat.je.Environment;
30 import com.sleepycat.je.jmx.JEMBeanHelper;
31
32 /**
33  * JEApplicationMBean is an example of how a JE application can incorporate JE
34  * monitoring into its existing MBean. It may be installed as is, or used as a
35  * starting point for building a MBean which includes JE support.
36  * <p>
37  * JE management is divided between the JEApplicationMBean class and
38  * JEMBeanHelper class. JEApplicationMBean contains an instance of
39  * JEMBeanHelper, which knows about JE attributes, operations and
40  * notifications. JEApplicationMBean itself has the responsibility of
41  * configuring, opening and closing the JE environment along with any other
42  * resources used by the application, and maintains a
43  * com.sleepycat.je.Environment handle.
44  * <p>
45  * The approach taken for accessing the environment is an application specific
46  * choice. Some of the salient considerations are:
47  * <ul>
48  * <li>Applications may open one or many Environment objects per process
49  * against a given environment.</li>
50  *
51  * <li>All Environment handles reference the same underlying JE environment
52  * implementation object.</li>
53
54  * <li> The first Environment object instantiated in the process does the real
55  * work of configuring and opening the environment. Follow-on instantiations of
56  * Environment merely increment a reference count. Likewise,
57  * Environment.close() only does real work when it's called by the last
58  * Environment object in the process. </li>
59  * </ul>
60  * <p>
61  * Another MBean approach for environment access can be seen in
62  * com.sleepycat.je.jmx.JEMonitor. That MBean does not take responsibility for
63  * opening and closing environments, and can only operate against already-open
64  * environments.
65  */

66
67 public class JEApplicationMBean implements DynamicMBean JavaDoc {
68
69     private static final String JavaDoc DESCRIPTION =
70         "A MBean for an application which uses JE. Provides open and close " +
71         "operations which configure and open a JE environment as part of the "+
72         "applications's resources. Also supports general JE monitoring.";
73
74     private MBeanInfo JavaDoc mbeanInfo; // this MBean's visible interface.
75
private JEMBeanHelper jeHelper; // gets JE management interface
76
private Environment targetEnv; // saved environment handle
77

78     /**
79      * This MBean provides an open operation to open the JE environment.
80      */

81     public static final String JavaDoc OP_OPEN = "openJE";
82
83     /**
84      * This MBean provides a close operation to release the JE environment.
85      * Note that environments must be closed to release resources.
86      */

87     public static final String JavaDoc OP_CLOSE = "closeJE";
88
89     /**
90      * Instantiate a JEApplicationMBean
91      *
92      * @param environmentHome home directory of the target JE environment.
93      */

94     public JEApplicationMBean(String JavaDoc environmentHome) {
95
96         File JavaDoc environmentDirectory = new File JavaDoc(environmentHome);
97         jeHelper = new JEMBeanHelper(environmentDirectory, true);
98         resetMBeanInfo();
99     }
100
101     /**
102      * @see DynamicMBean#getAttribute
103      */

104     public Object JavaDoc getAttribute(String JavaDoc attributeName)
105         throws AttributeNotFoundException JavaDoc,
106                MBeanException JavaDoc {
107         
108         return jeHelper.getAttribute(targetEnv, attributeName);
109     }
110
111     /**
112      * @see DynamicMBean#setAttribute
113      */

114     public void setAttribute(Attribute JavaDoc attribute)
115         throws AttributeNotFoundException JavaDoc,
116                InvalidAttributeValueException JavaDoc {
117                
118         jeHelper.setAttribute(targetEnv, attribute);
119     }
120
121     /**
122      * @see DynamicMBean#getAttributes
123      */

124     public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes) {
125
126         /* Sanity checking. */
127     if (attributes == null) {
128         throw new IllegalArgumentException JavaDoc("Attributes cannot be null");
129     }
130
131         /* Get each requested attribute. */
132         AttributeList JavaDoc results = new AttributeList JavaDoc();
133         for (int i = 0; i < attributes.length; i++) {
134             try {
135                 String JavaDoc name = attributes[i];
136                 Object JavaDoc value = jeHelper.getAttribute(targetEnv, name);
137                 results.add(new Attribute JavaDoc(name, value));
138             } catch (Exception JavaDoc e) {
139                 e.printStackTrace();
140             }
141         }
142         return results;
143     }
144
145     /**
146      * @see DynamicMBean#setAttributes
147      */

148     public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes) {
149
150         /* Sanity checking. */
151     if (attributes == null) {
152         throw new IllegalArgumentException JavaDoc("attribute list can't be null");
153     }
154
155         /* Set each attribute specified. */
156     AttributeList JavaDoc results = new AttributeList JavaDoc();
157         for (int i = 0; i < attributes.size(); i++) {
158             Attribute JavaDoc attr = (Attribute JavaDoc) attributes.get(i);
159             try {
160                 /* Set new value. */
161                 jeHelper.setAttribute(targetEnv, attr);
162
163                 /*
164                  * Add the name and new value to the result list. Be sure
165                  * to ask the MBean for the new value, rather than simply
166                  * using attr.getValue(), because the new value may not
167                  * be same if it is modified according to the JE
168                  * implementation.
169                  */

170                 String JavaDoc name = attr.getName();
171                 Object JavaDoc newValue = jeHelper.getAttribute(targetEnv, name);
172                 results.add(new Attribute JavaDoc(name, newValue));
173             } catch (Exception JavaDoc e) {
174                 e.printStackTrace();
175             }
176         }
177         return results;
178     }
179
180     /**
181      * @see DynamicMBean#invoke
182      */

183     public Object JavaDoc invoke(String JavaDoc actionName,
184                          Object JavaDoc[] params,
185                          String JavaDoc[] signature)
186         throws MBeanException JavaDoc {
187
188         Object JavaDoc result = null;
189         
190         if (actionName == null) {
191             throw new IllegalArgumentException JavaDoc("actionName cannot be null");
192         }
193
194         if (actionName.equals(OP_OPEN)) {
195             openEnvironment();
196             return null;
197         } else if (actionName.equals(OP_CLOSE)) {
198             closeEnvironment();
199             return null;
200         } else {
201             result = jeHelper.invoke(targetEnv, actionName, params, signature);
202         }
203
204         return result;
205     }
206
207     /**
208      * @see DynamicMBean#getMBeanInfo
209      */

210     public MBeanInfo JavaDoc getMBeanInfo() {
211     return mbeanInfo;
212     }
213
214     /**
215      * Create the available management interface for this environment.
216      * The attributes and operations available vary according to
217      * environment configuration.
218      *
219      */

220     private synchronized void resetMBeanInfo() {
221         
222         /*
223          * Get JE attributes, operation and notification information
224          * from JEMBeanHelper. An application may choose to add functionality
225          * of its own when constructing the MBeanInfo.
226          */

227         
228         /* Attributes. */
229         List JavaDoc attributeList = jeHelper.getAttributeList(targetEnv);
230         MBeanAttributeInfo JavaDoc [] attributeInfo =
231             new MBeanAttributeInfo JavaDoc[attributeList.size()];
232         attributeList.toArray(attributeInfo);
233
234         /* Constructors. */
235         Constructor JavaDoc [] constructors = this.getClass().getConstructors();
236         MBeanConstructorInfo JavaDoc [] constructorInfo =
237             new MBeanConstructorInfo JavaDoc[constructors.length];
238         for (int i = 0; i < constructors.length; i++) {
239             constructorInfo[i] =
240                 new MBeanConstructorInfo JavaDoc(this.getClass().getName(),
241                                          constructors[i]);
242         }
243
244         /* Operations. */
245
246         /*
247          * Get the list of operations available from the jeHelper. Then add
248          * an open and close operation.
249          */

250         List JavaDoc operationList = jeHelper.getOperationList(targetEnv);
251         if (targetEnv == null) {
252             operationList.add(
253              new MBeanOperationInfo JavaDoc(OP_OPEN,
254                                     "Configure and open the JE environment.",
255                                     new MBeanParameterInfo JavaDoc[0], // no params
256
"java.lang.Boolean",
257                                     MBeanOperationInfo.ACTION_INFO));
258         } else {
259             operationList.add(
260              new MBeanOperationInfo JavaDoc(OP_CLOSE,
261                                     "Close the JE environment.",
262                                     new MBeanParameterInfo JavaDoc[0], // no params
263
"void",
264                                     MBeanOperationInfo.ACTION_INFO));
265         }
266         
267         MBeanOperationInfo JavaDoc [] operationInfo =
268             new MBeanOperationInfo JavaDoc[operationList.size()];
269         operationList.toArray(operationInfo);
270
271         /* Notifications. */
272         MBeanNotificationInfo JavaDoc [] notificationInfo =
273             jeHelper.getNotificationInfo(targetEnv);
274
275         /* Generate the MBean description. */
276         mbeanInfo = new MBeanInfo JavaDoc(this.getClass().getName(),
277                                   DESCRIPTION,
278                                   attributeInfo,
279                                   constructorInfo,
280                                   operationInfo,
281                                   notificationInfo);
282     }
283
284     /**
285      * Open a JE environment using the configuration specified through
286      * MBean attributes and recorded within the JEMBeanHelper.
287      */

288     private void openEnvironment()
289         throws MBeanException JavaDoc {
290
291         try {
292             if (targetEnv == null) {
293                 /*
294                  * The environment configuration has been set through
295                  * mbean attributes managed by the JEMBeanHelper.
296                  */

297                 targetEnv =
298                     new Environment(jeHelper.getEnvironmentHome(),
299                                     jeHelper.getEnvironmentOpenConfig());
300                 resetMBeanInfo();
301             }
302         } catch (DatabaseException e) {
303             throw new MBeanException JavaDoc(e);
304         }
305     }
306
307     /**
308      * Release the environment handle contained within the MBean to properly
309      * release resources.
310      */

311     private void closeEnvironment()
312         throws MBeanException JavaDoc {
313
314         try {
315             if (targetEnv != null) {
316                 targetEnv.close();
317                 targetEnv = null;
318                 resetMBeanInfo();
319             }
320         } catch (DatabaseException e) {
321             throw new MBeanException JavaDoc(e);
322         }
323     }
324 }
325
Popular Tags