1 8 9 package mx4j.monitor; 10 11 import javax.management.MBeanNotificationInfo ; 12 import javax.management.NotCompliantMBeanException ; 13 import javax.management.ObjectName ; 14 import javax.management.monitor.MonitorNotification ; 15 16 import mx4j.log.Logger; 17 18 21 public class MX4JStringMonitor extends MX4JMonitor implements MX4JStringMonitorMBean 22 { 23 private static final String EMPTY = ""; 24 25 private String stringToCompare = EMPTY; 26 private boolean notifyMatch; 27 private boolean notifyDiffer; 28 29 public MX4JStringMonitor() throws NotCompliantMBeanException 30 { 31 super(MX4JStringMonitorMBean.class); 32 } 33 34 public MX4JStringMonitor(Class management) throws NotCompliantMBeanException 35 { 36 super(management); 37 } 38 39 public MBeanNotificationInfo [] getNotificationInfo() 40 { 41 return new MBeanNotificationInfo [0]; 43 } 44 45 public synchronized String getStringToCompare() 46 { 47 return stringToCompare; 48 } 49 50 public synchronized void setStringToCompare(String value) throws IllegalArgumentException 51 { 52 if (value == null) throw new IllegalArgumentException ("String to compare cannot be null"); 53 this.stringToCompare = value; 54 } 55 56 public synchronized boolean getNotifyMatch() 57 { 58 return notifyMatch; 59 } 60 61 public synchronized void setNotifyMatch(boolean notifyMatch) 62 { 63 this.notifyMatch = notifyMatch; 64 } 65 66 public synchronized boolean getNotifyDiffer() 67 { 68 return notifyDiffer; 69 } 70 71 public synchronized void setNotifyDiffer(boolean notifyDiffer) 72 { 73 this.notifyDiffer = notifyDiffer; 74 } 75 76 public String getDerivedGauge(ObjectName objectName) 77 { 78 StringMonitorInfo info = (StringMonitorInfo)getMonitorInfo(objectName); 79 return info.getGauge(); 80 } 81 82 public long getDerivedGaugeTimeStamp(ObjectName objectName) 83 { 84 StringMonitorInfo info = (StringMonitorInfo)getMonitorInfo(objectName); 85 return info.getTimestamp(); 86 } 87 88 protected MonitorInfo createMonitorInfo() 89 { 90 return new StringMonitorInfo(); 91 } 92 93 protected int compare(String left, String right) 94 { 95 return left == null ? right == null ? 0 : -1 : right == null ? 1 : left.compareTo(right); 96 } 97 98 protected void monitor(ObjectName name, String attribute, Object value, MonitorInfo monitorInfo) 99 { 100 if (!(value instanceof String )) 101 { 102 sendErrorNotification(monitorInfo, MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, "Attribute type must be a String, not " + value.getClass(), name, attribute); 103 return; 104 } 105 106 String gauge = (String )value; 107 108 String reference = null; 109 synchronized (this) 110 { 111 reference = getStringToCompare(); 112 } 113 114 Logger logger = getLogger(); 115 116 StringMonitorInfo info = (StringMonitorInfo)monitorInfo; 117 if (logger.isEnabledFor(Logger.DEBUG)) 118 { 119 logger.debug("Computing gauge, previous values are: " + info); 120 logger.debug("Current values are: gauge=" + gauge + ", stringToCompare=" + reference); 121 } 122 123 compareAndSendNotification(gauge, reference, info, name, attribute); 124 125 info.setGauge(gauge); 126 info.setTimestamp(System.currentTimeMillis()); 127 } 128 129 private void compareAndSendNotification(String gauge, String reference, StringMonitorInfo info, ObjectName name, String attribute) 130 { 131 Logger logger = getLogger(); 132 133 boolean equals = compare(gauge, reference) == 0; 134 135 if (info.isDifferNotified() && !equals) 136 { 137 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Difference already notified, gauge=" + gauge + ", string-to-compare=" + reference); 138 return; 139 } 140 if (info.isMatchNotified() && equals) 141 { 142 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Match already notified, gauge=" + gauge + ", string-to-compare=" + reference); 143 return; 144 } 145 146 if (equals) 147 { 148 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Gauge matches, gauge=" + gauge + ", string-to-compare=" + reference); 149 info.setDifferNotified(false); 150 if (getNotifyMatch()) 151 { 152 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Sending string match notification"); 153 info.setMatchNotified(true); 154 sendNotification(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED, "Gauge " + gauge + " matched " + reference, name, attribute, gauge, reference); 155 } 156 else 157 { 158 info.setMatchNotified(false); 159 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("StringMonitor is configured in non-match-notification mode"); 160 } 161 } 162 else 163 { 164 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Gauge differs, gauge=" + gauge + ", string-to-compare=" + reference); 165 info.setMatchNotified(false); 166 if (getNotifyDiffer()) 167 { 168 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Sending string differ notification"); 169 info.setDifferNotified(true); 170 sendNotification(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED, "Gauge " + gauge + " differs from " + reference, name, attribute, gauge, reference); 171 } 172 else 173 { 174 info.setDifferNotified(false); 175 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("StringMonitor is configured in non-differ-notification mode"); 176 } 177 } 178 } 179 180 protected class StringMonitorInfo extends MonitorInfo 181 { 182 private String gauge; 183 private long timestamp; 184 private boolean matchNotified; 185 private boolean differNotified; 186 187 public String getGauge() 188 { 189 return gauge; 190 } 191 192 public void setGauge(String gauge) 193 { 194 this.gauge = gauge; 195 } 196 197 public long getTimestamp() 198 { 199 return timestamp; 200 } 201 202 public void setTimestamp(long timestamp) 203 { 204 this.timestamp = timestamp; 205 } 206 207 public boolean isMatchNotified() 208 { 209 return matchNotified; 210 } 211 212 public void setMatchNotified(boolean matchNotified) 213 { 214 this.matchNotified = matchNotified; 215 } 216 217 public boolean isDifferNotified() 218 { 219 return differNotified; 220 } 221 222 public void setDifferNotified(boolean differNotified) 223 { 224 this.differNotified = differNotified; 225 } 226 227 public void clearNotificationStatus() 228 { 229 super.clearNotificationStatus(); 230 differNotified = false; 231 matchNotified = false; 232 } 233 234 public String toString() 235 { 236 StringBuffer buffer = new StringBuffer (super.toString()); 237 buffer.append(", gauge=").append(getGauge()); 238 buffer.append(", matchNotified=").append(isMatchNotified()); 239 buffer.append(", differNotified=").append(isDifferNotified()); 240 return buffer.toString(); 241 } 242 } 243 } 244 | Popular Tags |