1 21 22 23 package uk.org.primrose.pool.jmx; 24 25 26 import java.util.Date ; 27 import java.util.Timer ; 28 import java.util.TimerTask ; 29 import javax.management.ObjectName ; 30 import javax.management.MBeanNotificationInfo ; 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.InstanceNotFoundException ; 33 import javax.management.MBeanException ; 34 import javax.management.ReflectionException ; 35 import javax.management.NotificationBroadcasterSupport ; 36 import javax.management.AttributeChangeNotification ; 37 import java.lang.reflect.*; 38 import java.io.*; 39 40 41 public class ObjectMonitor extends NotificationBroadcasterSupport implements ObjectMonitorMBean { 42 private boolean active = false; 43 private int elementCount = 1; 44 private Object object; 45 private String methodName; 46 private Object [] params; 47 private String 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 name = ""; 58 private Timer timer; 59 60 public void setName(String name) { 61 this.name = name; 62 } 63 64 65 public void setObservedObject(Object object) { 66 this.object = object; 67 } 68 69 public void setObservedMethod(String methodName, Object [] 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 checkValue) { 79 this.checkValue = checkValue; 80 } 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 timer = new Timer (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 } 107 } 108 109 public boolean isActive() { 110 return active; 111 } 112 113 private void setNotify(String newVal) { 114 116 if (notificationType == THRESHOLD) { 117 int newValue = Integer.parseInt(newVal); 119 int chkValue = Integer.parseInt(checkValue); 120 121 if (newValue == 0) { 122 if (checkThreshHoldReset) { 124 checkThreshHoldReset = false; 125 sendNotification(methodName, "monitor.threshold.reset", "", checkValue, newVal); 126 } else { 128 return; 129 } 130 } 131 132 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 } else { 143 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 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 206 } 207 208 public void notifyAlarmClock(int element) { 209 Class targetClass = object.getClass(); 210 Method[] publicMethods = targetClass.getMethods(); 211 for (int j = 0; j < publicMethods.length; j++) { 212 String fieldName = publicMethods[j].getName(); 213 Class typeClass = publicMethods[j].getReturnType(); 214 String fieldType = typeClass.getName(); 215 217 if (fieldName.equalsIgnoreCase(methodName)) { 218 try { 219 Object val = publicMethods[j].invoke(object, params); 221 setNotify(val+""); 223 224 } catch (IllegalAccessException 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 attName, String attType, String msg, Object oldVal, Object newVal) { 235 synchronized(this) { 237 AttributeChangeNotification acn = new AttributeChangeNotification ( 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 { 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 |