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.system.ServiceMBeanSupport;
025: import org.jboss.logging.Logger;
026:
027: import javax.management.ObjectName;
028: import java.util.ArrayList;
029:
030: /**
031: * Comment
032: *
033: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
034: * @version $Revision: 57210 $
035: *
036: **/
037: public abstract class JBossMonitor extends ServiceMBeanSupport
038: implements Runnable, JBossMonitorMBean {
039: protected Logger log;
040: protected String monitorName;
041: protected ObjectName observedObject;
042: protected String attribute;
043: protected boolean enabled;
044: protected boolean alertSent = false;
045: protected long period;
046: protected ArrayList alertListeners = null;
047: protected String thresholdString;
048: protected Object triggeredAttributeValue;
049: protected long triggerTime;
050:
051: protected void startService() throws Exception {
052: super .startService();
053: log = Logger.getLogger(monitorName);
054: if (alertListeners != null) {
055: for (int i = 0; i < alertListeners.size(); i++) {
056: ObjectName aname = (ObjectName) alertListeners.get(i);
057: getServer().addNotificationListener(getServiceName(),
058: aname, null, null);
059: }
060: }
061: if (enabled) {
062: startMonitorThread();
063: }
064: }
065:
066: protected void stopService() {
067: enabled = false; // to shutdown monitor thread
068: }
069:
070: protected void startMonitorThread() {
071: Thread t = new Thread(this , "JBoss JMX Attribute Monitor "
072: + monitorName);
073: t.start();
074: }
075:
076: protected abstract void testThreshold();
077:
078: public String getMonitorName() {
079: return monitorName;
080: }
081:
082: public void setMonitorName(String name) {
083: monitorName = name;
084: }
085:
086: public ObjectName getObservedObject() {
087: return observedObject;
088: }
089:
090: public void setObservedObject(ObjectName oname) {
091: this .observedObject = oname;
092: }
093:
094: public String getObservedAttribute() {
095: return attribute;
096: }
097:
098: public void setObservedAttribute(String attr) {
099: attribute = attr;
100: }
101:
102: public boolean alerted() {
103: return alertSent;
104: }
105:
106: public void clearAlert() {
107: alertSent = false;
108: triggeredAttributeValue = null;
109: triggerTime = 0;
110: }
111:
112: public boolean getEnabled() {
113: return enabled;
114: }
115:
116: public void setEnabled(boolean start) {
117: if (start == enabled)
118: return;
119: enabled = start;
120:
121: // only start monitor thread if mbean is started and
122: // we have a state change from enabled == false to enabled == true
123: if (start && getState() == STARTED) {
124: startMonitorThread();
125: }
126: }
127:
128: public long getPeriod() {
129: return period;
130: }
131:
132: public void setPeriod(long period) {
133: this .period = period;
134: }
135:
136: public ArrayList getAlertListeners() {
137: return alertListeners;
138: }
139:
140: public void setAlertListeners(ArrayList listeners) {
141: if (alertListeners != null && getState() == STARTED) {
142: // remove old listeners
143: ArrayList copy = new ArrayList(listeners);
144: for (int i = 0; i < alertListeners.size(); i++) {
145: ObjectName oname = (ObjectName) alertListeners.get(i);
146: int idx = copy.indexOf(oname);
147: if (idx == -1) {
148: try {
149: getServer().removeNotificationListener(
150: getServiceName(), oname);
151: } catch (Exception ex) {
152: getLog().warn("failed to remove listener", ex);
153: }
154: } else {
155: copy.remove(idx);
156: }
157: }
158: // copy has all the new listeners
159: for (int i = 0; i < copy.size(); i++) {
160: ObjectName aname = (ObjectName) copy.get(i);
161: try {
162: getServer().addNotificationListener(
163: getServiceName(), aname, null, null);
164: } catch (Exception ex) {
165: getLog().warn("failed to remove listener", ex);
166: }
167: }
168: }
169: alertListeners = listeners;
170: }
171:
172: public Object getTriggeredAttributeValue() {
173: return triggeredAttributeValue;
174: }
175:
176: public long getTriggerTime() {
177: return triggerTime;
178: }
179:
180: public void run() {
181: while (this .getState() == STARTED
182: || this .getState() == STARTING) {
183: if (enabled) {
184: try {
185: testThreshold();
186: } catch (Exception ex) {
187: log.error(monitorName
188: + " had error while monitoring", ex);
189: }
190: }
191: try {
192: Thread.sleep(period);
193: } catch (InterruptedException ignored) {
194: }
195: }
196: }
197:
198: public String getThreshold() {
199: return thresholdString;
200: }
201:
202: public void setThreshold(String val) {
203: thresholdString = val;
204: }
205: }
|