KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > jmx > ObjectMonitor


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

21
22
23 package uk.org.primrose.pool.jmx;
24
25
26 import java.util.Date JavaDoc;
27 import java.util.Timer JavaDoc;
28 import java.util.TimerTask JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.MBeanNotificationInfo JavaDoc;
31 import javax.management.AttributeNotFoundException JavaDoc;
32 import javax.management.InstanceNotFoundException JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.management.NotificationBroadcasterSupport JavaDoc;
36 import javax.management.AttributeChangeNotification JavaDoc;
37 import java.lang.reflect.*;
38 import java.io.*;
39
40
41 public class ObjectMonitor extends NotificationBroadcasterSupport JavaDoc implements ObjectMonitorMBean {
42     private boolean active = false;
43     private int elementCount = 1;
44     private Object JavaDoc object;
45     private String JavaDoc methodName;
46     private Object JavaDoc[] params;
47     private String JavaDoc checkValue;
48     public static int MATCHING = 0;
49     public static int DIFFERING = 1;
50     public static int THRESHOLD = 2;
51     private int sequenceNumber = 0;
52     private int notificationType;
53     private boolean incrementThreshold;
54     private int initialThresholdValue;
55     private int refreshInterval = 5000;
56     private boolean checkThreshHoldReset = false;
57     private String JavaDoc name = "";
58     private Timer JavaDoc timer;
59
60     public void setName(String JavaDoc name) {
61         this.name = name;
62     }
63
64
65     public void setObservedObject(Object JavaDoc object) {
66         this.object = object;
67     }
68
69     public void setObservedMethod(String JavaDoc methodName, Object JavaDoc[] params) {
70         this.methodName = methodName;
71         this.params = params;
72     }
73
74     public void setObservedMethodNotificationType(int notificationType) {
75         this.notificationType = notificationType;
76     }
77
78     public void setCheckValue(String JavaDoc checkValue) {
79         this.checkValue = checkValue;
80         //System.err.println(this + " SETTING CHECK VALUE TO " +checkValue);
81
}
82
83     public void setIncrementThreshold(boolean incrementThreshold) {
84         this.incrementThreshold = incrementThreshold;
85         this.initialThresholdValue = Integer.parseInt(checkValue);
86     }
87
88     public void setRefreshInterval(int refreshInterval) {
89         this.refreshInterval = refreshInterval;
90     }
91
92     public void start() {
93         if (!active) {
94             //System.out.println("starting monitor " +this);
95
timer = new Timer JavaDoc(true);
96             timer.schedule(new ObjectMonitorAlarmClock(this), refreshInterval, refreshInterval);
97             active = true;
98         }
99     }
100
101     public void stop() {
102         if (timer != null && active) {
103             timer.cancel();
104             active = false;
105             //System.out.println("stopping monitor " +this);
106
}
107     }
108
109     public boolean isActive() {
110         return active;
111     }
112
113     private void setNotify(String JavaDoc newVal) {
114         //System.err.println(notificationType +" waiting " +newVal +", check " +checkValue);
115

116         if (notificationType == THRESHOLD) {
117             //System.err.println(this + " " +methodName +" newValue : " +newVal +", checkValue : " +checkValue);
118
int newValue = Integer.parseInt(newVal);
119             int chkValue = Integer.parseInt(checkValue);
120
121             if (newValue == 0) {
122                 // Send a notification to the listener to reset itself
123
if (checkThreshHoldReset) {
124                     checkThreshHoldReset = false;
125                     sendNotification(methodName, "monitor.threshold.reset", "", checkValue, newVal);
126                 // No problem, just return
127
} else {
128                     return;
129                 }
130             }
131
132             // trigger has been broken - raise the trigger and send notification
133
if (newValue > chkValue) {
134                 checkThreshHoldReset = true;
135                 if (incrementThreshold) {
136                     setCheckValue((chkValue +initialThresholdValue) +"");
137                     sendNotification(methodName, "monitor.threshold.increasing", "Increasing Threshold - value(" +newValue +") broke trigger(" +chkValue +"), increasing trigger to " +checkValue, checkValue, newVal);
138                 } else {
139                     sendNotification(methodName, "monitor.threshold.increasing", "Threshold reached (increment is disabled) - trigger(" +checkValue +"), value(" +newVal +")", checkValue, newVal);
140                 }
141             // trigger has not been broken, but we are still above 0 problems
142
} else {
143                 // Have we gone below trigger below (ie are we in a decreasing movement) ?
144
// Set the trigger down by a step
145
if ((newValue < (chkValue - initialThresholdValue)) && (chkValue - initialThresholdValue >= initialThresholdValue)) {
146                     setCheckValue((chkValue - initialThresholdValue) +"");
147                     sendNotification(methodName, "monitor.threshold.decreasing", "Decreasing Threshold - value(" +newVal +") went below trigger(" +chkValue +"), setting new trigger to " +checkValue, checkValue, newVal);
148
149                 }
150
151
152             // if (chkValue > initialThresholdValue) {
153
// if (newValue < chkValue) {
154
// setCheckValue((chkValue - initialThresholdValue) +"");
155
// sendNotification(methodName, "monitor.threshold.decreasing", "Decreasing Threshold - trigger(" +checkValue +"), value(" +newVal +")", checkValue, newVal);
156
// } else {
157
// sendNotification(methodName, "monitor.threshold.holding", "Threshold holding exceeded - trigger(" +checkValue +"), value(" +newVal +")", checkValue, newVal);
158
// }
159
// }
160

161             }
162
163
164
165         } else if (notificationType == DIFFERING) {
166             if (!((newVal)+"").equals(checkValue)) {
167                 sendNotification(methodName, "monitor.differing", "", checkValue, newVal);
168                 setCheckValue(newVal);
169             }
170         } else if (notificationType == MATCHING) {
171             if (((newVal)+"").equals(checkValue)) {
172                 sendNotification(methodName, "monitor.matching", "", checkValue, newVal);
173             }
174         }
175
176
177         /*
178         if (notificationType == THRESHOLD && newVal.equals("0") && checkThreshHoldReset) {
179             checkThreshHoldReset = false;
180             sendNotification(methodName, "monitor.threshold.reset", "", checkValue, newVal);
181         } else if (notificationType == DIFFERING && !((newVal)+"").equals(checkValue)) {
182             sendNotification(methodName, "monitor.differing", "", checkValue, newVal);
183             setCheckValue(newVal);
184         } else if (notificationType == MATCHING && ((newVal)+"").equals(checkValue)) {
185             sendNotification(methodName, "monitor.matching", "", checkValue, newVal);
186             //setCheckValue(newVal);
187         } else if (notificationType == THRESHOLD && (Integer.parseInt(newVal) > Integer.parseInt(checkValue))) {
188             checkThreshHoldReset = true;
189             //sendNotification(methodName, "monitor.threshold", "", checkValue, newVal);
190             if (incrementThreshold) {
191                 setCheckValue((Integer.parseInt(checkValue) +initialThresholdValue) +"");
192                 sendNotification(methodName, "monitor.threshold.increasing", "Increasing Threshold - trigger(" +checkValue +"), value(" +newVal +")", checkValue, newVal);
193             } else {
194                 sendNotification(methodName, "monitor.threshold.increasing", "Threshold reached (increment disabled) - trigger(" +checkValue +"), value(" +newVal +")", checkValue, newVal);
195             }
196         } else if (notificationType == THRESHOLD && (Integer.parseInt(newVal) <= Integer.parseInt(checkValue))) {
197             if ((Integer.parseInt(checkValue) > initialThresholdValue)) {
198                 setCheckValue((Integer.parseInt(checkValue) -initialThresholdValue) +"");
199                 if (newVal.equals("0")) {
200                     checkThreshHoldReset = true;
201                 }
202                 sendNotification(methodName, "monitor.threshold.decreasing", "Decreasing Threshold - trigger(" +checkValue +"), value(" +newVal +")", checkValue, newVal);
203             }
204         }
205         */

206     }
207
208     public void notifyAlarmClock(int element) {
209         Class JavaDoc targetClass = object.getClass();
210         Method[] publicMethods = targetClass.getMethods();
211         for (int j = 0; j < publicMethods.length; j++) {
212             String JavaDoc fieldName = publicMethods[j].getName();
213             Class JavaDoc typeClass = publicMethods[j].getReturnType();
214             String JavaDoc fieldType = typeClass.getName();
215             //System.err.println(" Name: " + fieldName +", Type: " + fieldType);
216

217             if (fieldName.equalsIgnoreCase(methodName)) {
218                 try {
219                     //System.err.println(" Name: " + fieldName +", Type: " + fieldType);
220
Object JavaDoc val = publicMethods[j].invoke(object, params);
221                     //System.err.println(val);
222
setNotify(val+"");
223
224                 } catch (IllegalAccessException JavaDoc iae) {
225                     iae.printStackTrace(System.err);
226                 } catch (InvocationTargetException ite) {
227                     ite.printStackTrace(System.err);
228                 }
229
230             }
231         }
232     }
233
234     void sendNotification(String JavaDoc attName, String JavaDoc attType, String JavaDoc msg, Object JavaDoc oldVal, Object JavaDoc newVal) {
235         //System.err.println("Sending " +attName +" " +attType +" " +oldVal +" " +newVal);
236
synchronized(this) {
237             AttributeChangeNotification JavaDoc acn = new AttributeChangeNotification JavaDoc(
238                                 this,
239                                 sequenceNumber++,
240                                 System.currentTimeMillis(),
241                                 msg,
242                                 attName,
243                                 attType,
244                                 oldVal,
245                                 newVal);
246             sendNotification(acn);
247         }
248     }
249
250     private static class ObjectMonitorAlarmClock extends TimerTask JavaDoc {
251         ObjectMonitor listener = null;
252
253         public ObjectMonitorAlarmClock(ObjectMonitor listener) {
254             this.listener = listener;
255         }
256
257         public void run() {
258             if (listener.isActive()) {
259                 for (int i = 0; i < listener.elementCount; i++) {
260                     listener.notifyAlarmClock(i);
261                 }
262             }
263         }
264     }
265 }
266
Popular Tags