KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > ThresholdMonitor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.monitor;
23
24 import org.jboss.util.NestedRuntimeException;
25
26 /**
27  * Comment
28  *
29  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
30  * @version $Revision: 37459 $
31  *
32  **/

33 public class ThresholdMonitor extends JBossMonitor
34    implements ThresholdMonitorMBean, Runnable JavaDoc
35 {
36    protected Number JavaDoc thresholdValue;
37    protected int compareTo;
38    protected Class JavaDoc attributeClass;
39
40    public ThresholdMonitor() {}
41
42    protected void parseThresholdValue()
43    {
44       if (attributeClass.equals(Long JavaDoc.class))
45       {
46          thresholdValue = new Long JavaDoc(Long.parseLong(thresholdString == null ? "0" : thresholdString));
47          return;
48       }
49       else if (attributeClass.equals(Integer JavaDoc.class))
50       {
51          thresholdValue = new Integer JavaDoc(Integer.parseInt(thresholdString == null ? "0" : thresholdString));
52          return;
53       }
54       else if (attributeClass.equals(Double JavaDoc.class))
55       {
56          thresholdValue = new Double JavaDoc(Double.parseDouble(thresholdString == null ? "0" : thresholdString));
57          return;
58       }
59       else if (attributeClass.equals(Float JavaDoc.class))
60       {
61          thresholdValue = new Float JavaDoc(Float.parseFloat(thresholdString == null ? "0" : thresholdString));
62          return;
63       }
64       else if (attributeClass.equals(Short JavaDoc.class))
65       {
66          thresholdValue = new Short JavaDoc(Short.parseShort(thresholdString == null ? "0" : thresholdString));
67          return;
68       }
69       else if (attributeClass.equals(Byte JavaDoc.class))
70       {
71          thresholdValue = new Byte JavaDoc(Byte.parseByte(thresholdString == null ? "0" : thresholdString));
72          return;
73       }
74       throw new RuntimeException JavaDoc("Failed to parse threshold string: " + thresholdString + " attributeClass: " + attributeClass);
75
76    }
77
78    protected int compare(Object JavaDoc value)
79    {
80       parseThresholdValue();
81       if (attributeClass.equals(Long JavaDoc.class))
82       {
83          return ((Long JavaDoc) thresholdValue).compareTo((Long JavaDoc)value);
84       }
85       else if (attributeClass.equals(Integer JavaDoc.class))
86       {
87          return ((Integer JavaDoc) thresholdValue).compareTo((Integer JavaDoc)value);
88       }
89       else if (attributeClass.equals(Double JavaDoc.class))
90       {
91          return ((Double JavaDoc) thresholdValue).compareTo((Double JavaDoc) value);
92       }
93       else if (attributeClass.equals(Float JavaDoc.class))
94       {
95          return ((Float JavaDoc) thresholdValue).compareTo((Float JavaDoc) value);
96       }
97       else if (attributeClass.equals(Short JavaDoc.class))
98       {
99          return ((Short JavaDoc) thresholdValue).compareTo((Short JavaDoc) value);
100       }
101       else if (attributeClass.equals(Byte JavaDoc.class))
102       {
103          return ((Byte JavaDoc) thresholdValue).compareTo((Byte JavaDoc) value);
104       }
105       throw new RuntimeException JavaDoc("Failed to compare threshold, unknown type");
106    }
107
108    protected void startService() throws Exception JavaDoc
109    {
110       Object JavaDoc 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 JavaDoc value = null;
119       try
120       {
121          value = getServer().getAttribute(observedObject, attribute);
122          if (compare(value) != compareTo) return;
123       }
124       catch (Exception JavaDoc 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 JavaDoc) 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 JavaDoc getThresholdValue()
150    {
151       return thresholdValue;
152    }
153
154    public void setThreshold(String JavaDoc val)
155    {
156       thresholdString = val;
157    }
158 }
159
Popular Tags