KickJava   Java API By Example, From Geeks To Geeks.

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


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.system.ServiceMBeanSupport;
25 import org.jboss.logging.Logger;
26
27 import javax.management.ObjectName JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * Comment
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @version $Revision: 37459 $
35  *
36  **/

37 public abstract class JBossMonitor extends ServiceMBeanSupport implements Runnable JavaDoc, JBossMonitorMBean
38 {
39    protected Logger log;
40    protected String JavaDoc monitorName;
41    protected ObjectName JavaDoc observedObject;
42    protected String JavaDoc attribute;
43    protected boolean enabled;
44    protected boolean alertSent = false;
45    protected long period;
46    protected ArrayList JavaDoc alertListeners = null;
47    protected String JavaDoc thresholdString;
48    protected Object JavaDoc triggeredAttributeValue;
49    protected long triggerTime;
50
51    protected void startService
52            () throws Exception JavaDoc
53    {
54       super.startService();
55       log = Logger.getLogger(monitorName);
56       if (alertListeners != null)
57       {
58          for (int i = 0; i < alertListeners.size(); i++)
59          {
60             ObjectName JavaDoc aname = (ObjectName JavaDoc)alertListeners.get(i);
61             getServer().addNotificationListener(getServiceName(), aname, null, null);
62          }
63       }
64       if (enabled)
65       {
66          startMonitorThread();
67       }
68    }
69
70    protected void stopService()
71    {
72       enabled = false; // to shutdown monitor thread
73
}
74
75    protected void startMonitorThread()
76    {
77       Thread JavaDoc t = new Thread JavaDoc(this, "JBoss JMX Attribute Monitor " + monitorName);
78       t.start();
79    }
80
81    protected abstract void testThreshold();
82
83    public String JavaDoc getMonitorName()
84    {
85       return monitorName;
86    }
87
88    public void setMonitorName(String JavaDoc name)
89    {
90       monitorName = name;
91    }
92
93    public ObjectName JavaDoc getObservedObject()
94    {
95       return observedObject;
96    }
97
98    public void setObservedObject(ObjectName JavaDoc oname)
99    {
100       this.observedObject = oname;
101    }
102
103    public String JavaDoc getObservedAttribute()
104    {
105       return attribute;
106    }
107
108    public void setObservedAttribute(String JavaDoc attr)
109    {
110       attribute = attr;
111    }
112
113    public boolean alerted()
114    {
115       return alertSent;
116    }
117
118    public void clearAlert()
119    {
120       alertSent = false;
121       triggeredAttributeValue = null;
122       triggerTime = 0;
123    }
124
125    public boolean getEnabled()
126    {
127       return enabled;
128    }
129
130    public void setEnabled(boolean start)
131    {
132       if (start == enabled) return;
133       enabled = start;
134
135       // only start monitor thread if mbean is started and
136
// we have a state change from enabled == false to enabled == true
137
if (start && getState() == STARTED)
138       {
139          startMonitorThread();
140       }
141    }
142
143    public long getPeriod()
144    {
145       return period;
146    }
147
148    public void setPeriod(long period)
149    {
150       this.period = period;
151    }
152
153    public ArrayList JavaDoc getAlertListeners()
154    {
155       return alertListeners;
156    }
157
158    public void setAlertListeners(ArrayList JavaDoc listeners)
159    {
160       if (alertListeners != null && getState() == STARTED)
161       {
162          // remove old listeners
163
ArrayList JavaDoc copy = new ArrayList JavaDoc(listeners);
164          for (int i = 0; i < alertListeners.size(); i++)
165          {
166             ObjectName JavaDoc oname = (ObjectName JavaDoc)alertListeners.get(i);
167             int idx = copy.indexOf(oname);
168             if (idx == -1)
169             {
170                try
171                {
172                   getServer().removeNotificationListener(getServiceName(), oname);
173                }
174                catch (Exception JavaDoc ex)
175                {
176                   getLog().warn("failed to remove listener", ex);
177                }
178             }
179             else
180             {
181                copy.remove(idx);
182             }
183          }
184          // copy has all the new listeners
185
for (int i = 0; i < copy.size(); i++)
186          {
187             ObjectName JavaDoc aname = (ObjectName JavaDoc)copy.get(i);
188             try
189             {
190                getServer().addNotificationListener(getServiceName(), aname, null, null);
191             }
192             catch (Exception JavaDoc ex)
193             {
194                getLog().warn("failed to remove listener", ex);
195             }
196          }
197       }
198       alertListeners = listeners;
199    }
200
201    public Object JavaDoc getTriggeredAttributeValue()
202    {
203       return triggeredAttributeValue;
204    }
205
206    public long getTriggerTime()
207    {
208       return triggerTime;
209    }
210
211    public void run()
212    {
213       while (this.getState() == STARTED || this.getState() == STARTING)
214       {
215          if (enabled)
216          {
217             try
218             {
219                testThreshold();
220             }
221             catch (Exception JavaDoc ex)
222             {
223                log.error(monitorName + " had error while monitoring", ex);
224             }
225          }
226          try
227          {
228             Thread.sleep(period);
229          }
230          catch (InterruptedException JavaDoc ignored)
231          {
232          }
233       }
234    }
235
236    public String JavaDoc getThreshold()
237    {
238       return thresholdString;
239    }
240
241    public void setThreshold(String JavaDoc val)
242    {
243       thresholdString = val;
244    }
245 }
246
Popular Tags