1 22 package org.jboss.monitor; 23 24 import org.jboss.util.NestedRuntimeException; 25 26 33 public class ThresholdMonitor extends JBossMonitor 34 implements ThresholdMonitorMBean, Runnable ![JavaDoc](../../../../cmn/javadoc.gif) 35 { 36 protected Number thresholdValue; 37 protected int compareTo; 38 protected Class attributeClass; 39 40 public ThresholdMonitor() {} 41 42 protected void parseThresholdValue() 43 { 44 if (attributeClass.equals(Long .class)) 45 { 46 thresholdValue = new Long (Long.parseLong(thresholdString == null ? "0" : thresholdString)); 47 return; 48 } 49 else if (attributeClass.equals(Integer .class)) 50 { 51 thresholdValue = new Integer (Integer.parseInt(thresholdString == null ? "0" : thresholdString)); 52 return; 53 } 54 else if (attributeClass.equals(Double .class)) 55 { 56 thresholdValue = new Double (Double.parseDouble(thresholdString == null ? "0" : thresholdString)); 57 return; 58 } 59 else if (attributeClass.equals(Float .class)) 60 { 61 thresholdValue = new Float (Float.parseFloat(thresholdString == null ? "0" : thresholdString)); 62 return; 63 } 64 else if (attributeClass.equals(Short .class)) 65 { 66 thresholdValue = new Short (Short.parseShort(thresholdString == null ? "0" : thresholdString)); 67 return; 68 } 69 else if (attributeClass.equals(Byte .class)) 70 { 71 thresholdValue = new Byte (Byte.parseByte(thresholdString == null ? "0" : thresholdString)); 72 return; 73 } 74 throw new RuntimeException ("Failed to parse threshold string: " + thresholdString + " attributeClass: " + attributeClass); 75 76 } 77 78 protected int compare(Object value) 79 { 80 parseThresholdValue(); 81 if (attributeClass.equals(Long .class)) 82 { 83 return ((Long ) thresholdValue).compareTo((Long )value); 84 } 85 else if (attributeClass.equals(Integer .class)) 86 { 87 return ((Integer ) thresholdValue).compareTo((Integer )value); 88 } 89 else if (attributeClass.equals(Double .class)) 90 { 91 return ((Double ) thresholdValue).compareTo((Double ) value); 92 } 93 else if (attributeClass.equals(Float .class)) 94 { 95 return ((Float ) thresholdValue).compareTo((Float ) value); 96 } 97 else if (attributeClass.equals(Short .class)) 98 { 99 return ((Short ) thresholdValue).compareTo((Short ) value); 100 } 101 else if (attributeClass.equals(Byte .class)) 102 { 103 return ((Byte ) thresholdValue).compareTo((Byte ) value); 104 } 105 throw new RuntimeException ("Failed to compare threshold, unknown type"); 106 } 107 108 protected void startService() throws Exception ![JavaDoc](../../../../cmn/javadoc.gif) 109 { 110 Object val = this.getServer().getAttribute(observedObject, attribute); 111 attributeClass = val.getClass(); 112 super.startService(); 113 } 114 115 protected void testThreshold() 116 { 117 if (alertSent) return; 118 Object value = null; 119 try 120 { 121 value = getServer().getAttribute(observedObject, attribute); 122 if (compare(value) != compareTo) return; 123 } 124 catch (Exception e) 125 { 126 throw new NestedRuntimeException("Failed to compare threshold, mbean failure", e); 127 } 128 129 alertSent = true; 130 triggerTime = System.currentTimeMillis(); 131 triggeredAttributeValue = value; 132 133 ThresholdNotification notification = new ThresholdNotification(monitorName, getServiceName(), observedObject, 134 attribute, (Number ) value, thresholdValue, 135 getNextNotificationSequenceNumber()); 136 this.sendNotification(notification); 137 } 138 139 public int getCompareTo() 140 { 141 return compareTo; 142 } 143 144 public void setCompareTo(int compare) 145 { 146 compareTo = compare; 147 } 148 149 public Number getThresholdValue() 150 { 151 return thresholdValue; 152 } 153 154 public void setThreshold(String val) 155 { 156 thresholdString = val; 157 } 158 } 159 | Popular Tags |