KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > MonitorRunnable


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.mx.util;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.management.InstanceNotFoundException JavaDoc;
27 import javax.management.MBeanAttributeInfo JavaDoc;
28 import javax.management.MBeanInfo JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.monitor.Monitor JavaDoc;
32 import javax.management.monitor.MonitorNotification JavaDoc;
33
34 import org.jboss.logging.Logger;
35
36 /**
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 37459 $
39  */

40 public class MonitorRunnable
41    extends SchedulableRunnable
42 {
43    private static final Logger log = Logger.getLogger(MonitorRunnable.class);
44    
45    /**
46     * The scheduler.
47     */

48    static RunnableScheduler scheduler;
49
50    /**
51     * Start the scheduler
52     */

53    static
54    {
55       scheduler = new RunnableScheduler();
56       scheduler.start();
57    }
58
59    // Attributes ----------------------------------------------------
60

61    // The monitoring to perform
62
private Monitor JavaDoc monitor;
63    private ObjectName JavaDoc monitorName;
64    private MonitorCallback callback;
65    private Map JavaDoc observedObjects;
66    private MBeanServer JavaDoc server;
67
68    // Constructors --------------------------------------------------
69

70    /**
71     * Create a monitor runnable to periodically perform monitoring.
72     *
73     * @param monitor the monitoring to perform.
74     */

75    public MonitorRunnable(Monitor JavaDoc monitor, ObjectName JavaDoc monitorName,
76       MonitorCallback callback, Map JavaDoc observedObjects, MBeanServer JavaDoc server)
77    {
78       this.monitor = monitor;
79       this.monitorName = monitorName;
80       this.callback = callback;
81       this.observedObjects = observedObjects;
82       this.server = server;
83       setScheduler(scheduler);
84    }
85
86    // Public --------------------------------------------------------
87

88    /**
89     * Run the monitor.<p>
90     *
91     * Retrieves the monitored attribute and passes it to each service.<p>
92     *
93     * Peforms the common error processing.
94     *
95     * @param object the mbean to run the monitor on
96     */

97    void runMonitor(ObservedObject object)
98    {
99       // Monitor for uncaught errors
100
try
101       {
102          MBeanInfo JavaDoc mbeanInfo = null;
103          try
104          {
105             mbeanInfo = server.getMBeanInfo(object.getObjectName());
106          }
107          catch (InstanceNotFoundException JavaDoc e)
108          {
109             sendObjectErrorNotification(object, "The observed object is not registered.");
110             return;
111          }
112
113          // Get the attribute information
114
MBeanAttributeInfo JavaDoc[] mbeanAttributeInfo = mbeanInfo.getAttributes();
115          MBeanAttributeInfo JavaDoc attributeInfo = null;
116          for (int i = 0; i < mbeanAttributeInfo.length; i++)
117          {
118             if (mbeanAttributeInfo[i].getName().equals(monitor.getObservedAttribute()))
119             {
120                attributeInfo = mbeanAttributeInfo[i];
121                break;
122             }
123          }
124
125          // The attribute must exist
126
if (attributeInfo == null)
127          {
128             sendAttributeErrorNotification(object,
129                "The observed attribute does not exist");
130             return;
131          }
132
133          // The attribute must exist
134
if (!attributeInfo.isReadable())
135          {
136             sendAttributeErrorNotification(object, "Attribute not readable.");
137             return;
138          }
139
140          // Get the value
141
Object JavaDoc value = null;
142          try
143          {
144             value = server.getAttribute(object.getObjectName(), monitor.getObservedAttribute());
145          }
146          catch (InstanceNotFoundException JavaDoc e)
147          {
148             sendObjectErrorNotification(object, "The observed object is not registered.");
149             return;
150          }
151
152          // Check for null value
153
if (value == null)
154          {
155             sendAttributeTypeErrorNotification(object, "Attribute is null");
156             return;
157          }
158
159          // Now pass the value to the respective monitor.
160
callback.monitorCallback(object, attributeInfo, value);
161       }
162       // Notify an unexcepted error
163
catch (Throwable JavaDoc e)
164       {
165          log.debug("Error in monitor ", e);
166          sendRuntimeErrorNotification(object, "General error: " + e.toString());
167       }
168    }
169
170    /**
171     * Run the montior
172     */

173    public void doRun()
174    {
175       // Perform the monitoring
176
runMonitor();
177  
178       // Reschedule
179
setNextRun(System.currentTimeMillis() + monitor.getGranularityPeriod());
180    }
181
182    /**
183     * Run the monitor on each observed object
184     */

185    void runMonitor()
186    {
187       // Loop through each mbean
188
boolean isActive = monitor.isActive();
189       for (Iterator JavaDoc i = observedObjects.values().iterator(); i.hasNext() && isActive;)
190          runMonitor((ObservedObject) i.next());
191    }
192
193    /**
194     * Sends the notification
195     *
196     * @param object the observedObject.
197     * @param type the notification type.
198     * @param timestamp the time of the notification.
199     * @param message the human readable message to send.
200     * @param attribute the attribute name.
201     * @param gauge the derived gauge.
202     * @param trigger the trigger value.
203     */

204    void sendNotification(ObservedObject object, String JavaDoc type, long timestamp, String JavaDoc message,
205       String JavaDoc attribute, Object JavaDoc gauge, Object JavaDoc trigger)
206    {
207       MonitorNotification JavaDoc n = callback.createNotification(type,
208          monitorName, timestamp, message, gauge, attribute,
209          object.getObjectName(), trigger);
210       monitor.sendNotification(n);
211    }
212
213    /**
214     * Send a runtime error notification.
215     *
216     * @param object the observedObject.
217     * @param message the human readable message to send.
218     */

219    void sendRuntimeErrorNotification(ObservedObject object, String JavaDoc message)
220    {
221       if (object.notAlreadyNotified(ObservedObject.RUNTIME_ERROR_NOTIFIED))
222          sendNotification(object, MonitorNotification.RUNTIME_ERROR, 0,
223             message, monitor.getObservedAttribute(), null, null);
224    }
225
226    /**
227     * Send an object error notification.
228     *
229     * @param object the observedObject.
230     * @param message the human readable message to send.
231     */

232    void sendObjectErrorNotification(ObservedObject object, String JavaDoc message)
233    {
234       if (object.notAlreadyNotified(ObservedObject.OBSERVED_OBJECT_ERROR_NOTIFIED))
235          sendNotification(object, MonitorNotification.OBSERVED_OBJECT_ERROR, 0,
236             message, monitor.getObservedAttribute(), null, null);
237    }
238
239    /**
240     * Send an attribute error notification.
241     *
242     * @param object the observedObject.
243     * @param message the human readable message to send.
244     */

245    void sendAttributeErrorNotification(ObservedObject object, String JavaDoc message)
246    {
247       if (object.notAlreadyNotified(ObservedObject.OBSERVED_ATTRIBUTE_ERROR_NOTIFIED))
248          sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 0,
249             message, monitor.getObservedAttribute(), null, null);
250    }
251
252    /**
253     * Send an attribute type error notification.
254     *
255     * @param object the observedObject.
256     * @param message the human readable message to send.
257     */

258    void sendAttributeTypeErrorNotification(ObservedObject object, String JavaDoc message)
259    {
260       if (object.notAlreadyNotified(ObservedObject.OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
261          sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 0,
262             message, monitor.getObservedAttribute(), null, null);
263    }
264
265 }
266
Popular Tags