KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > jmx > JEMBeanHelper


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

8
9 package com.sleepycat.je.jmx;
10
11 import java.io.File JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import javax.management.Attribute JavaDoc;
16 import javax.management.AttributeNotFoundException JavaDoc;
17 import javax.management.InvalidAttributeValueException JavaDoc;
18 import javax.management.MBeanAttributeInfo JavaDoc;
19 import javax.management.MBeanException JavaDoc;
20 import javax.management.MBeanNotificationInfo JavaDoc;
21 import javax.management.MBeanOperationInfo JavaDoc;
22 import javax.management.MBeanParameterInfo JavaDoc;
23
24 import com.sleepycat.je.CheckpointConfig;
25 import com.sleepycat.je.Database;
26 import com.sleepycat.je.DatabaseConfig;
27 import com.sleepycat.je.DatabaseException;
28 import com.sleepycat.je.DatabaseStats;
29 import com.sleepycat.je.DbInternal;
30 import com.sleepycat.je.Environment;
31 import com.sleepycat.je.EnvironmentConfig;
32 import com.sleepycat.je.EnvironmentMutableConfig;
33 import com.sleepycat.je.StatsConfig;
34
35 /**
36  * JEMBeanHelper is a utility class for the MBean implementation which wants to
37  * add management of a JE environment to its capabilities. MBean
38  * implementations can contain a JEMBeanHelper instance to get MBean metadata
39  * for JE and to set attributes, get attributes, and invoke operations.
40  * <p>
41  * com.sleepycat.je.jmx.JEMonitor and
42  * the example program jmx.JEApplicationMBean are two MBean implementations
43  * which provide support different application use cases. See those classes for
44  * examples of how to use JEMBeanHelper.
45  */

46
47 public class JEMBeanHelper {
48
49     /*
50      * A note to JE developers: all available JE attributes and operations are
51      * described in the following static info arrays. New management
52      * functionality can be added to the helper by adding to the appropriate
53      * set of static definitions. For example, if we want to add a new JE
54      * attribute called "foo", which is available for open environments, we
55      * need to define a new MBeanAttributeInfo in the OPEN_ATTR array. The
56      * helper then needs to provide an implementation in set/getAttribute.
57      */

58     
59     /* --------------------- Attributes -------------------------- */
60
61     /* Attribute names. */
62     public static final String JavaDoc ATT_ENV_HOME = "environmentHome";
63     public static final String JavaDoc ATT_OPEN = "isOpen";
64     public static final String JavaDoc ATT_IS_READ_ONLY = "isReadOnly";
65     public static final String JavaDoc ATT_IS_TRANSACTIONAL = "isTransactional";
66     public static final String JavaDoc ATT_CACHE_SIZE = "cacheSize";
67     public static final String JavaDoc ATT_CACHE_PERCENT = "cachePercent";
68     public static final String JavaDoc ATT_LOCK_TIMEOUT = "lockTimeout";
69     public static final String JavaDoc ATT_IS_SERIALIZABLE = "isSerializableIsolation";
70     public static final String JavaDoc ATT_TXN_TIMEOUT = "transactionTimeout";
71     public static final String JavaDoc ATT_SET_READ_ONLY = "openReadOnly";
72     public static final String JavaDoc ATT_SET_TRANSACTIONAL = "openTransactional";
73     public static final String JavaDoc ATT_SET_SERIALIZABLE =
74         "openSerializableIsolation";
75     
76     /* COMMON_ATTR attributes are available for any environment. */
77     private static final MBeanAttributeInfo JavaDoc [] COMMON_ATTR = {
78         
79         new MBeanAttributeInfo JavaDoc(ATT_ENV_HOME,
80                                "java.lang.String",
81                                "Environment home directory.",
82                                true, // readable
83
false, // writable
84
false), // isIs
85
new MBeanAttributeInfo JavaDoc(ATT_OPEN,
86                                "java.lang.Boolean",
87                                "True if this environment is open.",
88                                true, // readable
89
false, // writable
90
true) // isIs
91
};
92
93     /* OPEN_ATTR attributes are available for all open environments. */
94     private static final MBeanAttributeInfo JavaDoc [] OPEN_ATTR = {
95
96         new MBeanAttributeInfo JavaDoc(ATT_IS_READ_ONLY,
97                                "java.lang.Boolean",
98                                "True if this environment is read only.",
99                                true, // readable
100
false, // writable
101
true), // isIs
102
new MBeanAttributeInfo JavaDoc(ATT_IS_TRANSACTIONAL,
103                                "java.lang.Boolean",
104                              "True if this environment supports transactions.",
105                                true, // readable
106
false, // writable
107
true), // isIs
108
new MBeanAttributeInfo JavaDoc(ATT_CACHE_SIZE,
109                                "java.lang.Long",
110                                "Cache size, in bytes.",
111                                true, // readable
112
true, // writable
113
false), // isIs
114
new MBeanAttributeInfo JavaDoc(ATT_CACHE_PERCENT,
115                                "java.lang.Integer",
116                                "By default, cache size is (cachePercent * " +
117                                "JVM maximum memory. To change the cache size "+
118                                "using a percentage of the heap size, set " +
119                                "the cache size to 0 and cachePercent to the "+
120                                "desired percentage value.",
121                                true, // readable
122
true, // writable
123
false), // isIs
124
new MBeanAttributeInfo JavaDoc(ATT_LOCK_TIMEOUT,
125                                "java.lang.Long",
126                                "Lock timeout, in microseconds.",
127                                true, // readable
128
false, // writable
129
false), // isIs
130
};
131
132     /*
133      * TRANSACTIONAL_ATTR attributes are available only for open, transactional
134      * environments.
135      */

136     private static final MBeanAttributeInfo JavaDoc [] TRANSACTIONAL_ATTR = {
137
138         new MBeanAttributeInfo JavaDoc(ATT_IS_SERIALIZABLE,
139                                "java.lang.Boolean",
140                                "True if this environment provides " +
141                                "Serializable (degree 3) isolation. The " +
142                                "default is RepeatableRead isolation.",
143                                true, // readable
144
false, // writable
145
true), // isIs
146
new MBeanAttributeInfo JavaDoc(ATT_TXN_TIMEOUT,
147                                "java.lang.Long",
148                                "Transaction timeout, in seconds. A value " +
149                                "of 0 means there is no timeout.",
150                                true, // readable
151
false, // writable
152
false) // isIs
153
};
154
155     /*
156      * CREATE_ATTR attributes are available when the mbean is configured to
157      * support configuration and opening by the mbean. They express the
158      * configuration settings.
159      */

160     private static final MBeanAttributeInfo JavaDoc [] CREATE_ATTR = {
161         
162         new MBeanAttributeInfo JavaDoc(ATT_SET_READ_ONLY,
163                                "java.lang.Boolean",
164                                "True if this environment should be opened " +
165                                "in readonly mode.",
166                                true, // readable
167
true, // writable
168
false), // isIs
169
new MBeanAttributeInfo JavaDoc(ATT_SET_TRANSACTIONAL,
170                                "java.lang.Boolean",
171                                "True if this environment should be opened " +
172                                "in transactional mode.",
173                                true, // readable
174
true, // writable
175
false), // isIs
176
new MBeanAttributeInfo JavaDoc(ATT_SET_SERIALIZABLE,
177                                "java.lang.Boolean",
178                                "True if this environment should be opened " +
179                                "with serializableIsolation. The default is "+
180                                "false.",
181                                true, // readable
182
true, // writable
183
false), // isIs
184
};
185
186     /* --------------------- Operations -------------------------- */
187
188     /* Operation names */
189     static final String JavaDoc OP_CLEAN = "cleanLog";
190     static final String JavaDoc OP_EVICT = "evictMemory";
191     static final String JavaDoc OP_CHECKPOINT = "checkpoint";
192     static final String JavaDoc OP_SYNC = "sync";
193     static final String JavaDoc OP_ENV_STAT = "getEnvironmentStats";
194     static final String JavaDoc OP_LOCK_STAT = "getLockStats";
195     static final String JavaDoc OP_TXN_STAT = "getTxnStats";
196     static final String JavaDoc OP_DB_NAMES = "getDatabaseNames";
197     static final String JavaDoc OP_DB_STAT = "getDatabaseStats";
198
199     private static final MBeanOperationInfo JavaDoc OP_CLEAN_INFO =
200         new MBeanOperationInfo JavaDoc(OP_CLEAN,
201                                "Remove obsolete environment log files. " +
202                                "Zero or more log files will be cleaned as " +
203                                "necessary to bring the disk space " +
204                                "utilization of the environment above the " +
205                                "configured minimum utilization threshold " +
206                                "as determined by the setting " +
207                                "je.cleaner.minUtilization. Returns the " +
208                                "number of files cleaned, that will be " +
209                                "deleted at the next qualifying checkpoint.",
210                                new MBeanParameterInfo JavaDoc[0], // no params
211
"java.lang.Integer",
212                                MBeanOperationInfo.UNKNOWN);
213
214     private static final MBeanOperationInfo JavaDoc OP_EVICT_INFO =
215         new MBeanOperationInfo JavaDoc(OP_EVICT,
216                                "Reduce cache usage to the threshold " +
217                                "determined by the setting " +
218                                "je.evictor.useMemoryFloor. ",
219                                new MBeanParameterInfo JavaDoc[0], // no params
220
"void",
221                                MBeanOperationInfo.UNKNOWN);
222
223     /* parameter for checkpoint operation. */
224     private static final MBeanParameterInfo JavaDoc [] checkpointParams = {
225         new MBeanParameterInfo JavaDoc ("force", "java.lang.Boolean",
226                                 "If true, force a checkpoint even if " +
227                                 "there has been no activity since the last " +
228                                 "checkpoint. Returns true if a checkpoint " +
229                                 "executed.")
230     };
231
232     private static final MBeanOperationInfo JavaDoc OP_CHECKPOINT_INFO =
233         new MBeanOperationInfo JavaDoc(OP_CHECKPOINT,
234                                "Checkpoint the environment.",
235                                checkpointParams,
236                                "void",
237                                MBeanOperationInfo.UNKNOWN);
238
239     private static final MBeanOperationInfo JavaDoc OP_SYNC_INFO =
240         new MBeanOperationInfo JavaDoc(OP_SYNC,
241                                "Flush the environment to stable storage.",
242                                new MBeanParameterInfo JavaDoc[0], // no params
243
"void",
244                                MBeanOperationInfo.UNKNOWN);
245
246     private static final MBeanParameterInfo JavaDoc [] statParams = {
247         new MBeanParameterInfo JavaDoc ("clear", "java.lang.Boolean",
248                                 "If true, reset statistics after reading."),
249         new MBeanParameterInfo JavaDoc ("fast", "java.lang.Boolean",
250                                 "If true, only return statistics which do " +
251                                 "not require expensive computation.")
252
253     };
254
255     private static final MBeanOperationInfo JavaDoc OP_ENV_STAT_INFO =
256         new MBeanOperationInfo JavaDoc(OP_ENV_STAT,
257                                "Get environment statistics.",
258                                statParams,
259                                "com.sleepycat.je.EnvironmentStats",
260                                MBeanOperationInfo.INFO);
261
262     private static final MBeanOperationInfo JavaDoc OP_LOCK_STAT_INFO =
263         new MBeanOperationInfo JavaDoc(OP_LOCK_STAT,
264                                "Get locking statistics.",
265                                statParams,
266                                "com.sleepycat.je.LockStats",
267                                MBeanOperationInfo.INFO);
268
269     private static final MBeanOperationInfo JavaDoc OP_TXN_STAT_INFO =
270         new MBeanOperationInfo JavaDoc(OP_TXN_STAT,
271                                "Get transactional statistics.",
272                                statParams,
273                                "com.sleepycat.je.TransactionStats",
274                                MBeanOperationInfo.INFO);
275
276     private static final MBeanOperationInfo JavaDoc OP_DB_NAMES_INFO =
277         new MBeanOperationInfo JavaDoc(OP_DB_NAMES,
278                               "Get the names of databases in the environment.",
279                                new MBeanParameterInfo JavaDoc[0], // no params
280
"java.util.ArrayList",
281                                MBeanOperationInfo.INFO);
282
283     private static final MBeanParameterInfo JavaDoc [] dbStatParams = {
284         new MBeanParameterInfo JavaDoc ("clear", "java.lang.Boolean",
285                                 "If true, reset statistics after reading."),
286         new MBeanParameterInfo JavaDoc ("fast", "java.lang.Boolean",
287                                 "If true, only return statistics which do " +
288                                 "not require expensive computation. " +
289                                 "Currently all database stats are not fast."),
290         new MBeanParameterInfo JavaDoc ("databaseName", "java.lang.String",
291                                 "database name")
292
293     };
294
295     private static final MBeanOperationInfo JavaDoc OP_DB_STAT_INFO =
296         new MBeanOperationInfo JavaDoc(OP_DB_STAT,
297                                "Get database statistics.",
298                                dbStatParams,
299                                "com.sleepycat.je.DatabaseStats",
300                                MBeanOperationInfo.INFO);
301
302
303     /* target JE environment home directory. */
304     private File JavaDoc environmentHome;
305
306     /*
307      * If canConfigure is true, this helper will make environment configuration
308      * attributes available in the mbean metadata. Configuration attributes
309      * will be saved in the openConfig instance.
310      */

311     private boolean canConfigure;
312     private EnvironmentConfig openConfig;
313
314     /* true if the mbean metadata needs to be refreshed. */
315     private boolean needReset;
316
317     /*
318      * Save whether the environment was open the last time we fetched
319      * mbean attributes. Use to detect a change in environment status.
320      */

321     private boolean envWasOpen;
322     
323     /**
324      * Instantiate a helper, specifying environment home and open capabilities.
325      *
326      * @param environmentHome home directory of the target JE environment.
327      * @param canConfigure If true, the helper will show environment
328      * configuration attributes.
329      */

330     public JEMBeanHelper(File JavaDoc environmentHome, boolean canConfigure) {
331
332         if (environmentHome == null) {
333             throw new IllegalArgumentException JavaDoc(
334                                         "Environment home cannot be null");
335         }
336         this.environmentHome = environmentHome;
337         this.canConfigure = canConfigure;
338         if (canConfigure) {
339             openConfig = new EnvironmentConfig();
340         }
341     }
342
343     /**
344      * Return the target environment directory.
345      * @return the environment directory.
346      */

347     public File JavaDoc getEnvironmentHome() {
348         return environmentHome;
349     }
350
351     /**
352      * If the helper was instantiated with canConfigure==true, it shows
353      * environment configuration attributes. Those attributes are returned
354      * within this EnvironmentConfig object for use in opening environments.
355      *
356      * @return EnvironmentConfig object which saves configuration attributes
357      * recorded through MBean attributes.
358      */

359     public EnvironmentConfig getEnvironmentOpenConfig() {
360         return openConfig;
361     }
362
363     /**
364      * Return an Environment only if the environment has already been opened
365      * in this process. A helper method for MBeans which want to only access
366      * open environments.
367      * @return Environment if already open, null if not open.
368      */

369     public Environment getEnvironmentIfOpen() {
370         if (environmentHome == null) {
371             return null;
372         }
373
374         return DbInternal.getEnvironmentShell(environmentHome);
375     }
376
377     /**
378      * Tell the MBean if the available set of functionality has changed.
379      *
380      * @return true if the MBean should regenerate its JE metadata.
381      */

382     public synchronized boolean getNeedReset() {
383         return needReset;
384     }
385
386     /********************************************************************/
387     /* MBean Attributes */
388     /********************************************************************/
389
390     /**
391      * Get MBean attribute metadata for this environment.
392      * @param targetEnv The target JE environment. May be null if the
393      * environment is not open.
394      * @return list of MBeanAttributeInfo objects describing the available
395      * attributes.
396      */

397     public List JavaDoc getAttributeList(Environment targetEnv) {
398
399         /* Turn off reset because the mbean metadata is being refreshed. */
400         setNeedReset(false);
401
402         ArrayList JavaDoc attrList = new ArrayList JavaDoc();
403         
404         /* Add attributes for all JE environments. */
405         for (int i = 0; i < COMMON_ATTR.length; i++) {
406             attrList.add(COMMON_ATTR[i]);
407         }
408
409         if (targetEnv == null) {
410             if (canConfigure) {
411                 /* Add attributes for configuring an environment. */
412                 for (int i = 0; i < CREATE_ATTR.length; i++) {
413                     attrList.add(CREATE_ATTR[i]);
414                 }
415             }
416         } else {
417             /* Add attributes for an open environment. */
418             for (int i = 0; i < OPEN_ATTR.length; i++) {
419                 attrList.add(OPEN_ATTR[i]);
420             }
421
422             /* Add attributes for an open, transactional environment. */
423             try {
424                 EnvironmentConfig config = targetEnv.getConfig();
425                 if (config.getTransactional()) {
426                     for (int i = 0; i < TRANSACTIONAL_ATTR.length; i++) {
427                         attrList.add(TRANSACTIONAL_ATTR[i]);
428                     }
429                 }
430             } catch (DatabaseException ignore) {
431                 /* ignore */
432             }
433         }
434
435         return attrList;
436     }
437
438     /**
439      * Get an attribute value for the given environment. Check
440      * JEMBeanHelper.getNeedReset() after this call because the helper may
441      * detect that the environment has changed and that the MBean metadata
442      * should be reset.
443      *
444      * @param targetEnv The target JE environment. May be null if the
445      * environment is not open.
446      * @param attributeName attribute name.
447      * @return attribute value.
448      */

449     public Object JavaDoc getAttribute(Environment targetEnv,
450                                String JavaDoc attributeName)
451         throws AttributeNotFoundException JavaDoc,
452                MBeanException JavaDoc {
453
454         /* Sanity check. */
455         if (attributeName == null) {
456             throw new AttributeNotFoundException JavaDoc(
457                                             "Attribute name cannot be null");
458         }
459
460         /* These attributes are available regardless of environment state. */
461         try {
462             if (attributeName.equals(ATT_ENV_HOME)) {
463                 return environmentHome.getCanonicalPath();
464             } else if (attributeName.equals(ATT_OPEN)) {
465                 boolean envIsOpen = (targetEnv != null);
466                 resetIfOpenStateChanged(envIsOpen);
467                 return new Boolean JavaDoc(envIsOpen);
468             } else if (attributeName.equals(ATT_SET_READ_ONLY)) {
469                 return new Boolean JavaDoc(openConfig.getReadOnly());
470             } else if (attributeName.equals(ATT_SET_TRANSACTIONAL)) {
471                 return new Boolean JavaDoc(openConfig.getTransactional());
472             } else if (attributeName.equals(ATT_SET_SERIALIZABLE)) {
473                 return new Boolean JavaDoc(openConfig.getTxnSerializableIsolation());
474             } else {
475                 /* The rest are JE environment attributes. */
476                 if (targetEnv != null) {
477
478                     EnvironmentConfig config = targetEnv.getConfig();
479
480                     if (attributeName.equals(ATT_IS_READ_ONLY)) {
481                         return new Boolean JavaDoc(config.getReadOnly());
482                     } else if (attributeName.equals(ATT_IS_TRANSACTIONAL)) {
483                         return new Boolean JavaDoc(config.getTransactional());
484                     } else if (attributeName.equals(ATT_CACHE_SIZE)) {
485                         return new Long JavaDoc(config.getCacheSize());
486                     } else if (attributeName.equals(ATT_CACHE_PERCENT)) {
487                         return new Integer JavaDoc(config.getCachePercent());
488                     } else if (attributeName.equals(ATT_LOCK_TIMEOUT)) {
489                         return new Long JavaDoc(config.getLockTimeout());
490                     } else if (attributeName.equals(ATT_IS_SERIALIZABLE)) {
491                         return new
492                             Boolean JavaDoc(config.getTxnSerializableIsolation());
493                     } else if (attributeName.equals(ATT_TXN_TIMEOUT)) {
494                         return new Long JavaDoc(config.getTxnTimeout());
495                     } else {
496                         throw new AttributeNotFoundException JavaDoc("attribute " +
497                                                              attributeName +
498                                                              " is not valid.");
499                     }
500                 }
501                 return null;
502             }
503         } catch (Exception JavaDoc e) {
504             /*
505              * Add both the message and the exception for easiest deciphering
506              * of the problem. Sometimes the original exception stacktrace gets
507              * hidden in server logs.
508              */

509             throw new MBeanException JavaDoc(e, e.getMessage());
510         }
511     }
512
513     /**
514      * Set an attribute value for the given environment.
515      *
516      * @param targetEnv The target JE environment. May be null if the
517      * environment is not open.
518      * @param attribute name/value pair
519      */

520     public void setAttribute(Environment targetEnv,
521                              Attribute JavaDoc attribute)
522         throws AttributeNotFoundException JavaDoc,
523                InvalidAttributeValueException JavaDoc {
524
525         if (attribute == null) {
526             throw new AttributeNotFoundException JavaDoc("Attribute cannot be null");
527         }
528
529         /* Sanity check parameters. */
530         String JavaDoc name = attribute.getName();
531         Object JavaDoc value = attribute.getValue();
532
533     if (name == null) {
534         throw new AttributeNotFoundException JavaDoc(
535                                      "Attribute name cannot be null");
536     }
537
538     if (value == null) {
539         throw new InvalidAttributeValueException JavaDoc(
540                                       "Attribute value for attribute " +
541                                       name + " cannot be null");
542     }
543
544         try {
545             if (name.equals(ATT_SET_READ_ONLY)) {
546                 openConfig.setReadOnly(((Boolean JavaDoc) value).booleanValue());
547             } else if (name.equals(ATT_SET_TRANSACTIONAL)) {
548                 openConfig.setTransactional(((Boolean JavaDoc) value).booleanValue());
549             } else if (name.equals(ATT_SET_SERIALIZABLE)) {
550                 openConfig.setTxnSerializableIsolation(
551                                              ((Boolean JavaDoc) value).booleanValue());
552             } else {
553                 /* Set the specified attribute if the environment is open. */
554                 if (targetEnv != null) {
555
556                     EnvironmentMutableConfig config =
557                         targetEnv.getMutableConfig();
558
559                     if (name.equals(ATT_CACHE_SIZE)) {
560                         config.setCacheSize(((Long JavaDoc) value).longValue());
561                         targetEnv.setMutableConfig(config);
562                     } else if (name.equals(ATT_CACHE_PERCENT)) {
563                         config.setCachePercent(((Integer JavaDoc) value).intValue());
564                         targetEnv.setMutableConfig(config);
565                     } else {
566                         throw new AttributeNotFoundException JavaDoc("attribute " +
567                                                              name +
568                                                              " is not valid.");
569                     }
570                 } else {
571                     throw new AttributeNotFoundException JavaDoc("attribute " +
572                                                          name +
573                                                          " is not valid.");
574                 }
575             }
576         } catch (NumberFormatException JavaDoc e) {
577             throw new InvalidAttributeValueException JavaDoc("attribute name=" + name);
578         } catch (DatabaseException e) {
579             throw new InvalidAttributeValueException JavaDoc("attribute name=" + name +
580                                                      e.getMessage());
581         }
582     }
583
584     /********************************************************************/
585     /* JE Operations */
586     /********************************************************************/
587
588     /**
589      * Get mbean operation metadata for this environment.
590      *
591      * @param targetEnv The target JE environment. May be null if the
592      * environment is not open.
593      * @return List of MBeanOperationInfo describing available operations.
594      */

595     public List JavaDoc getOperationList(Environment targetEnv) {
596         setNeedReset(false);
597
598         List JavaDoc operationList = new ArrayList JavaDoc();
599
600         if (targetEnv != null) {
601             /*
602              * These operations are only available if the environment is
603              * open.
604              */

605             operationList.add(OP_CLEAN_INFO);
606             operationList.add(OP_EVICT_INFO);
607             operationList.add(OP_ENV_STAT_INFO);
608             operationList.add(OP_LOCK_STAT_INFO);
609             operationList.add(OP_DB_NAMES_INFO);
610             operationList.add(OP_DB_STAT_INFO);
611
612             /* Add checkpoint only for transactional environments. */
613             boolean isTransactional = false;
614             try {
615                 EnvironmentConfig config = targetEnv.getConfig();
616                 isTransactional = config.getTransactional();
617             } catch (DatabaseException e) {
618                 /* Don't make any operations available. */
619                 return new ArrayList JavaDoc();
620             }
621             
622             if (isTransactional) {
623                 operationList.add(OP_CHECKPOINT_INFO);
624                 operationList.add(OP_TXN_STAT_INFO);
625             } else {
626                 operationList.add(OP_SYNC_INFO);
627             }
628         }
629
630         return operationList;
631     }
632
633     /**
634      * Invoke an operation for the given environment.
635      *
636      * @param targetEnv The target JE environment. May be null if the
637      * environment is not open.
638      * @param actionName operation name.
639      * @param params operation parameters. May be null.
640      * @param signature operation signature. May be null.
641      * @return the operation result
642      */

643     public Object JavaDoc invoke(Environment targetEnv,
644                          String JavaDoc actionName,
645                          Object JavaDoc [] params,
646                          String JavaDoc [] signature)
647         throws MBeanException JavaDoc {
648
649         /* Sanity checking. */
650         if (actionName == null) {
651             throw new IllegalArgumentException JavaDoc("actionName cannot be null");
652         }
653
654         try {
655             if (targetEnv != null) {
656                 if (actionName.equals(OP_CLEAN)) {
657                     int numFiles = targetEnv.cleanLog();
658                     return new Integer JavaDoc(numFiles);
659                 } else if (actionName.equals(OP_EVICT)) {
660                     targetEnv.evictMemory();
661                     return null;
662                 } else if (actionName.equals(OP_CHECKPOINT)) {
663                     CheckpointConfig config = new CheckpointConfig();
664                     if ((params != null) && (params.length > 0)) {
665                         Boolean JavaDoc force = (Boolean JavaDoc) params[0];
666                         config.setForce(force.booleanValue());
667                     }
668                     targetEnv.checkpoint(config);
669                     return null;
670                 } else if (actionName.equals(OP_SYNC)) {
671                     targetEnv.sync();
672                     return null;
673                 } else if (actionName.equals(OP_ENV_STAT)) {
674                     return targetEnv.getStats(getStatsConfig(params));
675                 } else if (actionName.equals(OP_LOCK_STAT)) {
676                     return targetEnv.getLockStats(getStatsConfig(params));
677                 } else if (actionName.equals(OP_TXN_STAT)) {
678                     return targetEnv.getTransactionStats(
679                                                        getStatsConfig(params));
680                 } else if (actionName.equals(OP_DB_NAMES)) {
681                     return targetEnv.getDatabaseNames();
682                 } else if (actionName.equals(OP_DB_STAT)) {
683                     return getDatabaseStats(targetEnv, params);
684                 }
685             }
686
687             return new IllegalArgumentException JavaDoc("actionName: " +
688                                                 actionName +
689                                                 " is not valid");
690         } catch (DatabaseException e) {
691             /*
692              * Add both the message and the exception for easiest
693              * deciphering of the problem. Sometimes the original exception
694              * stacktrace gets hidden in server logs.
695              */

696             throw new MBeanException JavaDoc(e, e.getMessage());
697         }
698     }
699
700     /**
701      * Helper for creating a StatsConfig object to use as an operation
702      * parameter.
703      */

704     private StatsConfig getStatsConfig(Object JavaDoc [] params) {
705         StatsConfig statsConfig = new StatsConfig();
706         if ((params != null) && (params.length > 0) && (params[0] != null)) {
707             Boolean JavaDoc clear = (Boolean JavaDoc) params[0];
708             statsConfig.setClear(clear.booleanValue());
709         }
710         if ((params != null) && (params.length > 1) && (params[1] != null)) {
711             Boolean JavaDoc fast = (Boolean JavaDoc) params[1];
712             statsConfig.setFast(fast.booleanValue());
713         }
714         return statsConfig;
715     }
716
717     /**
718      * Helper to get statistics for a given database.
719      * @param params operation parameters
720      * @return DatabaseStats object
721      */

722     private DatabaseStats getDatabaseStats(Environment targetEnv,
723                                            Object JavaDoc [] params)
724         throws IllegalArgumentException JavaDoc,
725            DatabaseException {
726
727         if ((params == null) || (params.length < 3)) {
728             return null;
729         }
730         String JavaDoc dbName = (String JavaDoc)params[2];
731
732         Database db = null;
733         try {
734             DatabaseConfig dbConfig = new DatabaseConfig();
735             dbConfig.setReadOnly(true);
736             DbInternal.setUseExistingConfig(dbConfig, true);
737             db = targetEnv.openDatabase(null, dbName, dbConfig);
738             return db.getStats(getStatsConfig(params));
739         } finally {
740             if (db != null) {
741                 db.close();
742             }
743         }
744     }
745
746     /********************************************************************/
747     /* JE Notifications.
748     /********************************************************************/

749
750     /**
751      * No notifications are supported.
752      * @return List of MBeanNotificationInfo for available notifications.
753      */

754     public MBeanNotificationInfo JavaDoc []
755         getNotificationInfo(Environment targetEnv) {
756         return null;
757     }
758
759     /********************************************************************/
760     /* private helpers.
761     /********************************************************************/

762
763     private synchronized void setNeedReset(boolean reset) {
764         needReset = reset;
765     }
766
767     private synchronized void resetIfOpenStateChanged(boolean isOpen) {
768         if (isOpen != envWasOpen) {
769             setNeedReset(true);
770             envWasOpen = isOpen;
771         }
772     }
773 }
774
775
Popular Tags