KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > beans > object > ObjectManagementMonitor


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.management.beans.object;
5
6 import com.tc.logging.TCLogger;
7 import com.tc.logging.TCLogging;
8 import com.tc.management.AbstractTerracottaMBean;
9
10 import javax.management.NotCompliantMBeanException JavaDoc;
11
12 public class ObjectManagementMonitor extends AbstractTerracottaMBean implements ObjectManagementMonitorMBean {
13
14   private static final TCLogger logger = TCLogging.getLogger(ObjectManagementMonitor.class);
15
16   private volatile GCComptroller gcController;
17   private final GCRunner gcRunner;
18
19   public ObjectManagementMonitor() throws NotCompliantMBeanException JavaDoc {
20     super(ObjectManagementMonitorMBean.class, false);
21
22     gcRunner = new GCRunner() {
23       private boolean isRunning = false;
24
25       public void run() {
26         setRunningState();
27         gcController.startGC();
28         setStopState();
29       }
30
31       private synchronized void setRunningState() {
32         if (isRunning) { throw new UnsupportedOperationException JavaDoc("Cannot run GC because GC is already running."); }
33         isRunning = true;
34         logger.info("Running GC.");
35       }
36
37       private synchronized void setStopState() {
38         if (!isRunning) { throw new UnsupportedOperationException JavaDoc("Cannot stop GC because GC is not running."); }
39         isRunning = false;
40         logger.info("GC finished.");
41       }
42
43       public synchronized boolean isGCRunning() {
44         return isRunning;
45       }
46     };
47   }
48
49   public boolean isGCRunning() {
50     return gcRunner.isGCRunning();
51   }
52
53   public synchronized void runGC() {
54     if (!isEnabled()) { throw new UnsupportedOperationException JavaDoc("Cannot run GC because mBean is not enabled."); }
55     if (gcController == null) { throw new RuntimeException JavaDoc("Failure: see log for more information"); }
56     if (gcController.gcEnabledInConfig()) { throw new UnsupportedOperationException JavaDoc(
57         "Cannot run GC externally because GC is enabled through config."); }
58     if (isGCRunning()) { throw new UnsupportedOperationException JavaDoc("Cannot run GC because GC is already running."); }
59
60     Thread JavaDoc gcRunnerThread = new Thread JavaDoc(gcRunner);
61     gcRunnerThread.setName("GCRunnerThread");
62     gcRunnerThread.start();
63   }
64
65   public synchronized void reset() {
66     // nothing to reset
67
}
68
69   public void registerGCController(GCComptroller controller) {
70     if (isEnabled()) {
71       if (gcController != null) {
72         logger.warn("Not registering new gc-controller because one was already registered.");
73         return;
74       }
75       gcController = controller;
76     }
77   }
78
79   public static interface GCComptroller {
80     void startGC();
81
82     boolean gcEnabledInConfig();
83   }
84
85   static interface GCRunner extends Runnable JavaDoc {
86     boolean isGCRunning();
87   }
88 }
89
Popular Tags