001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.monitor;
023:
024: import org.jboss.util.NestedRuntimeException;
025:
026: /**
027: * Comment
028: *
029: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
030: * @version $Revision: 57210 $
031: *
032: **/
033: public class ThresholdMonitor extends JBossMonitor implements
034: ThresholdMonitorMBean, Runnable {
035: protected Number thresholdValue;
036: protected int compareTo;
037: protected Class attributeClass;
038:
039: public ThresholdMonitor() {
040: }
041:
042: protected void parseThresholdValue() {
043: if (attributeClass.equals(Long.class)) {
044: thresholdValue = new Long(Long
045: .parseLong(thresholdString == null ? "0"
046: : thresholdString));
047: return;
048: } else if (attributeClass.equals(Integer.class)) {
049: thresholdValue = new Integer(Integer
050: .parseInt(thresholdString == null ? "0"
051: : thresholdString));
052: return;
053: } else if (attributeClass.equals(Double.class)) {
054: thresholdValue = new Double(Double
055: .parseDouble(thresholdString == null ? "0"
056: : thresholdString));
057: return;
058: } else if (attributeClass.equals(Float.class)) {
059: thresholdValue = new Float(Float
060: .parseFloat(thresholdString == null ? "0"
061: : thresholdString));
062: return;
063: } else if (attributeClass.equals(Short.class)) {
064: thresholdValue = new Short(Short
065: .parseShort(thresholdString == null ? "0"
066: : thresholdString));
067: return;
068: } else if (attributeClass.equals(Byte.class)) {
069: thresholdValue = new Byte(Byte
070: .parseByte(thresholdString == null ? "0"
071: : thresholdString));
072: return;
073: }
074: throw new RuntimeException("Failed to parse threshold string: "
075: + thresholdString + " attributeClass: "
076: + attributeClass);
077:
078: }
079:
080: protected int compare(Object value) {
081: parseThresholdValue();
082: if (attributeClass.equals(Long.class)) {
083: return ((Long) thresholdValue).compareTo((Long) value);
084: } else if (attributeClass.equals(Integer.class)) {
085: return ((Integer) thresholdValue)
086: .compareTo((Integer) value);
087: } else if (attributeClass.equals(Double.class)) {
088: return ((Double) thresholdValue).compareTo((Double) value);
089: } else if (attributeClass.equals(Float.class)) {
090: return ((Float) thresholdValue).compareTo((Float) value);
091: } else if (attributeClass.equals(Short.class)) {
092: return ((Short) thresholdValue).compareTo((Short) value);
093: } else if (attributeClass.equals(Byte.class)) {
094: return ((Byte) thresholdValue).compareTo((Byte) value);
095: }
096: throw new RuntimeException(
097: "Failed to compare threshold, unknown type");
098: }
099:
100: protected void startService() throws Exception {
101: Object val = this .getServer().getAttribute(observedObject,
102: attribute);
103: attributeClass = val.getClass();
104: super .startService();
105: }
106:
107: protected void testThreshold() {
108: if (alertSent)
109: return;
110: Object value = null;
111: try {
112: value = getServer().getAttribute(observedObject, attribute);
113: if (compare(value) != compareTo)
114: return;
115: } catch (Exception e) {
116: throw new NestedRuntimeException(
117: "Failed to compare threshold, mbean failure", e);
118: }
119:
120: alertSent = true;
121: triggerTime = System.currentTimeMillis();
122: triggeredAttributeValue = value;
123:
124: ThresholdNotification notification = new ThresholdNotification(
125: monitorName, getServiceName(), observedObject,
126: attribute, (Number) value, thresholdValue,
127: getNextNotificationSequenceNumber());
128: this .sendNotification(notification);
129: }
130:
131: public int getCompareTo() {
132: return compareTo;
133: }
134:
135: public void setCompareTo(int compare) {
136: compareTo = compare;
137: }
138:
139: public Number getThresholdValue() {
140: return thresholdValue;
141: }
142:
143: public void setThreshold(String val) {
144: thresholdString = val;
145: }
146: }
|