1 package javax.management.monitor; 2 import java.util.Date ; 5 import java.util.Timer ; 6 import java.util.TimerTask ; 7 8 import javax.management.ObjectName ; 11 import javax.management.AttributeNotFoundException ; 13 import javax.management.InstanceNotFoundException ; 14 import javax.management.MBeanException ; 15 import javax.management.ReflectionException ; 16 17 22 public class PeriodicMonitor extends Monitor implements PeriodicMonitorMBean { 23 24 29 public static final String PERIODIC_SAMPLING = new String ("jmx.monitor.periodic"); 30 31 36 44 45 50 private transient Object derivedGauge[] = new Object [capacityIncrement]; 51 52 57 private transient long derivedGaugeTimestamp[] = new long[capacityIncrement]; 58 59 62 private transient Timer timer = null; 63 64 69 70 73 public synchronized void start() { 74 if (!isActive()) { 75 isActive = true; 76 timer = new Timer (); 78 timer.schedule(new PeriodicAlarmClock(this), 79 getGranularityPeriod(), getGranularityPeriod()); 80 } 81 } 82 83 86 91 public void stop() { 92 if (isActive()) { 93 if (timer != null) { 95 timer.cancel(); 96 timer = null; 97 } 98 isActive = false; 99 } 100 } 101 102 115 public synchronized void setGranularityPeriod(long period) 116 throws java.lang.IllegalArgumentException { 117 super.setGranularityPeriod(period); 118 if (isActive()) { 121 timer.cancel(); 122 timer = new Timer (); 123 timer.schedule(new PeriodicAlarmClock(this), 124 getGranularityPeriod(), getGranularityPeriod()); 125 } 126 } 127 128 140 public Object getDerivedGauge(ObjectName object) { 141 int index = indexOf(object); 142 if (index != -1) 143 return derivedGauge[index]; 144 else 145 return null; 146 } 147 148 161 public long getDerivedGaugeTimeStamp(ObjectName object) { 162 int index = indexOf(object); 163 if (index != -1) 164 return derivedGaugeTimestamp[index]; 165 else 166 return 0; 167 } 168 169 174 183 private void updateDerivedGauge(Object scanObj, int index) { 184 derivedGaugeTimestamp[index] = (new Date ()).getTime(); 185 derivedGauge[index] = scanObj; 186 } 187 188 196 private void updateNotifications(int index) { 197 sendNotification( 198 PERIODIC_SAMPLING, 199 derivedGaugeTimestamp[index], "", 200 derivedGauge[index], null, index); 201 } 202 203 208 215 void notifyAlarmClock(int index) { 216 Object scan_obj = null; 217 String notif_type = null; 218 try { 219 if (isActive()) { 220 221 224 if ((getObservedObject(index) == null) || (getObservedAttribute() == null)) { 229 return; 230 } 231 232 try { 236 scan_obj = server.getAttribute(getObservedObject(index), 237 getObservedAttribute()); 238 if (scan_obj == null) 239 return; 240 } catch (NullPointerException np_ex) { 241 if ((alreadyNotifieds[index] & RUNTIME_ERROR_NOTIFIED) != RESET_FLAGS_ALREADY_NOTIFIED) { 242 return; 243 } else { 244 notif_type = MonitorNotification.RUNTIME_ERROR; 245 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 246 throw new MonitorSettingException ( 247 "The periodic monitor must be registered in the MBean server."); 248 } 249 } catch (InstanceNotFoundException inf_ex) { 250 if ((alreadyNotifieds[index] & OBSERVED_OBJECT_ERROR_NOTIFIED) != RESET_FLAGS_ALREADY_NOTIFIED) { 251 return; 252 } else { 253 notif_type = MonitorNotification.OBSERVED_OBJECT_ERROR; 254 setAlreadyNotified(index, 255 OBSERVED_OBJECT_ERROR_NOTIFIED); 256 throw new MonitorSettingException ( 257 "The observed object must be registered in the MBean server."); 258 } 259 } catch (AttributeNotFoundException anf_ex) { 260 if ((alreadyNotifieds[index] & OBSERVED_ATTRIBUTE_ERROR_NOTIFIED) != RESET_FLAGS_ALREADY_NOTIFIED) { 261 return; 262 } else { 263 notif_type = MonitorNotification.OBSERVED_ATTRIBUTE_ERROR; 264 setAlreadyNotified(index, 265 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED); 266 throw new MonitorSettingException ( 267 "The observed attribute must be accessible in the observed object."); 268 } 269 } catch (MBeanException mb_ex) { 270 if ((alreadyNotifieds[index] & RUNTIME_ERROR_NOTIFIED) != RESET_FLAGS_ALREADY_NOTIFIED) { 271 return; 272 } else { 273 notif_type = MonitorNotification.RUNTIME_ERROR; 274 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 275 throw new MonitorSettingException (mb_ex.getMessage()); 276 } 277 } catch (ReflectionException ref_ex) { 278 if ((alreadyNotifieds[index] & OBSERVED_ATTRIBUTE_ERROR_NOTIFIED) != RESET_FLAGS_ALREADY_NOTIFIED) { 279 return; 280 } else { 281 notif_type = MonitorNotification.OBSERVED_ATTRIBUTE_ERROR; 282 setAlreadyNotified(index, 283 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED); 284 throw new MonitorSettingException (ref_ex.getMessage()); 285 } 286 } 287 resetAllAlreadyNotified(index); 289 updateDerivedGauge(scan_obj, index); 291 updateNotifications(index); 293 } 294 } catch (MonitorSettingException ms_ex) { 295 sendNotification(notif_type, derivedGaugeTimestamp[index], ms_ex 297 .getMessage(), derivedGauge[index], null, index); 298 } 299 } 300 301 308 void insertSpecificElementAt(int index) { 309 insertObjectElementAt(derivedGauge, "", index); 311 insertlongElementAt(derivedGaugeTimestamp, (new Date ()).getTime(), 312 index); 313 } 314 315 322 void removeSpecificElementAt(int index) { 323 removeObjectElementAt(derivedGauge, index); 325 removelongElementAt(derivedGaugeTimestamp, index); 326 } 327 328 336 void insertObjectElementAt(Object [] array, Object value, int index) { ensureObjectCapacity(array, elementCount + 1); 338 System.arraycopy(array, index, array, index + 1, elementCount - index); 339 array[index] = value; 340 } 341 342 349 void removeObjectElementAt(Object [] array, int index) { 350 if (index < 0 || index >= elementCount) 351 return; 352 int j = elementCount - index - 1; 353 if (j > 0) { 354 System.arraycopy(array, index + 1, array, index, j); 355 } 356 } 357 358 366 void ensureObjectCapacity(Object [] array, int minCapacity) { int oldCapacity = array.length; 368 if (minCapacity > oldCapacity) { 371 Object oldArray[] = array; 372 int newCapacity = oldCapacity + capacityIncrement; 373 if (newCapacity < minCapacity) { 374 newCapacity = minCapacity; 375 } 376 array = new Number [newCapacity]; 377 System.arraycopy(oldArray, 0, array, 0, elementCount); 378 } 379 } 380 381 386 private static class PeriodicAlarmClock extends TimerTask { 387 PeriodicMonitor listener = null; 388 393 public PeriodicAlarmClock(PeriodicMonitor listener) { 394 this.listener = listener; 395 } 396 401 405 public void run() { 406 if (listener.isActive()) { 407 for (int i = 0; i < listener.elementCount; i++) { 408 listener.notifyAlarmClock(i); 409 } 410 } 411 } 412 } 413 } 414 | Popular Tags |