KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > monitor > MX4JStringMonitor


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.monitor;
10
11 import javax.management.MBeanNotificationInfo JavaDoc;
12 import javax.management.NotCompliantMBeanException JavaDoc;
13 import javax.management.ObjectName JavaDoc;
14 import javax.management.monitor.MonitorNotification JavaDoc;
15
16 import mx4j.log.Logger;
17
18 /**
19  * @version $Revision: 1.3 $
20  */

21 public class MX4JStringMonitor extends MX4JMonitor implements MX4JStringMonitorMBean
22 {
23    private static final String JavaDoc EMPTY = "";
24
25    private String JavaDoc stringToCompare = EMPTY;
26    private boolean notifyMatch;
27    private boolean notifyDiffer;
28
29    public MX4JStringMonitor() throws NotCompliantMBeanException JavaDoc
30    {
31       super(MX4JStringMonitorMBean.class);
32    }
33
34    public MX4JStringMonitor(Class JavaDoc management) throws NotCompliantMBeanException JavaDoc
35    {
36       super(management);
37    }
38
39    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
40    {
41       // TODO
42
return new MBeanNotificationInfo JavaDoc[0];
43    }
44
45    public synchronized String JavaDoc getStringToCompare()
46    {
47       return stringToCompare;
48    }
49
50    public synchronized void setStringToCompare(String JavaDoc value) throws IllegalArgumentException JavaDoc
51    {
52       if (value == null) throw new IllegalArgumentException JavaDoc("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 JavaDoc getDerivedGauge(ObjectName JavaDoc objectName)
77    {
78       StringMonitorInfo info = (StringMonitorInfo)getMonitorInfo(objectName);
79       return info.getGauge();
80    }
81
82    public long getDerivedGaugeTimeStamp(ObjectName JavaDoc 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 JavaDoc left, String JavaDoc right)
94    {
95       return left == null ? right == null ? 0 : -1 : right == null ? 1 : left.compareTo(right);
96    }
97
98    protected void monitor(ObjectName JavaDoc name, String JavaDoc attribute, Object JavaDoc value, MonitorInfo monitorInfo)
99    {
100       if (!(value instanceof String JavaDoc))
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 JavaDoc gauge = (String JavaDoc)value;
107
108       String JavaDoc 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 JavaDoc gauge, String JavaDoc reference, StringMonitorInfo info, ObjectName JavaDoc name, String JavaDoc 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 JavaDoc gauge;
183       private long timestamp;
184       private boolean matchNotified;
185       private boolean differNotified;
186
187       public String JavaDoc getGauge()
188       {
189          return gauge;
190       }
191
192       public void setGauge(String JavaDoc 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 JavaDoc toString()
235       {
236          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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