ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/MonitorManager.java
Revision: 1.16
Committed: Tue May 21 16:47:16 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.15: +3 -2 lines
Log Message:
Added URL to GPL headers.

File Contents

# User Rev Content
1 tdb 1.15 /*
2     * i-scream central monitoring system
3 tdb 1.16 * http://www.i-scream.org.uk
4 tdb 1.15 * Copyright (C) 2000-2002 i-scream
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.14 package uk.org.iscream.cms.server.client;
23 tdb 1.1
24     //---IMPORTS---
25 tdb 1.14 import uk.org.iscream.cms.server.componentmanager.*;
26     import uk.org.iscream.cms.server.core.*;
27     import uk.org.iscream.cms.server.util.*;
28 tdb 1.1 import java.util.*;
29    
30     /**
31     * A manager for the Monitors.
32     *
33 ajm 1.13 * This class starts by loading all the monitors as specificed in the configuration.
34     * These monitors should implement the PluginMonitor interface.
35     *
36     * This class then takes the feed of data coming in over CORBA from
37     * the ClientServant queue. This data is then looked at to determine
38     * type. It then places it into either a data queue, a heartbeat queue or
39     * an other queue, and all data in the the all queue.
40     *
41     * Monitors then read the data off the queue that they are interested in
42     * and process the data accordingly.
43     *
44 tdb 1.15 * @author $Author: tdb $
45 tdb 1.16 * @version $Id: MonitorManager.java,v 1.15 2002/05/18 18:16:00 tdb Exp $
46 tdb 1.1 */
47 ajm 1.11 public class MonitorManager extends Thread {
48 tdb 1.1
49     //---FINAL ATTRIBUTES---
50    
51     /**
52     * The current CVS revision of this class
53     */
54 tdb 1.16 public static final String REVISION = "$Revision: 1.15 $";
55 tdb 1.1
56     //---STATIC METHODS---
57    
58 ajm 1.3 /**
59     * Return a reference to the single class.
60     * Construct it if it does not already exist, otherwise just return the reference.
61     */
62 tdb 1.12 public synchronized static MonitorManager getInstance() {
63 ajm 1.3 if (_instance == null){
64     _instance = new MonitorManager();
65     }
66     return _instance;
67     }
68    
69 tdb 1.1 //---CONSTRUCTORS---
70    
71 ajm 1.13 /**
72     * Constructs a new MonitorManager.
73     * This initialises all the queues and loads
74     * all the Monitors that have been specified in the configuration
75     */
76 ajm 1.3 private MonitorManager() {
77 tdb 1.5 // set our name
78     setName("client.MonitorManager");
79    
80 ajm 1.3 _queue = ClientMain._monitorQueue;
81     _qID = _queue.getQueue();
82     _logger.write(toString(), Logger.SYSINIT, "Initialising");
83     _logger.write(toString(), Logger.SYSMSG, "Creating monitor pipeline for plugin monitors ...");
84    
85 tdb 1.8 // get the config proxy
86     ConfigurationProxy cp = ConfigurationProxy.getInstance();
87    
88 tdb 1.9 // get the configuration for our outgoing queue's
89    
90     // see if these Queue's need a size limit
91     try {
92     int queueSizeLimit = Integer.parseInt(cp.getProperty(_name, "Queue.SizeLimit"));
93     String queueRemoveAlgorithm = cp.getProperty(_name, "Queue.RemoveAlgorithm");
94     int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms);
95     if(algorithm != -1) {
96 tdb 1.10 _logger.write(toString(), Logger.DEBUG, "Starting 4 Queues with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm);
97 tdb 1.9 // we have valid values, so lets start it.
98     _dataQueue = new Queue(queueSizeLimit, algorithm);
99     _heartbeatQueue = new Queue(queueSizeLimit, algorithm);
100     _otherQueue = new Queue(queueSizeLimit, algorithm);
101 tdb 1.10 _allQueue = new Queue(queueSizeLimit, algorithm);
102 tdb 1.9 }
103     else {
104     _logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm);
105     // just don't activate a limit
106     _dataQueue = new Queue();
107     _heartbeatQueue = new Queue();
108     _otherQueue = new Queue();
109 tdb 1.10 _allQueue = new Queue();
110 tdb 1.9 }
111     } catch (PropertyNotFoundException e) {
112     _logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e);
113     // just don't activate a limit
114     _dataQueue = new Queue();
115     _heartbeatQueue = new Queue();
116     _otherQueue = new Queue();
117 tdb 1.10 _allQueue = new Queue();
118 tdb 1.9 } catch (NumberFormatException e) {
119     _logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e);
120     // just don't activate a limit
121     _dataQueue = new Queue();
122     _heartbeatQueue = new Queue();
123     _otherQueue = new Queue();
124 tdb 1.10 _allQueue = new Queue();
125 tdb 1.9 }
126    
127     // startup monitors on these queues
128     try {
129     // try to get the interval, if this fails, we won't start up the monitor
130     int queueMonitorInterval = Integer.parseInt(cp.getProperty(_name, "Queue.MonitorInterval"));
131     _dataQueue.startMonitor(queueMonitorInterval*1000, ClientMain._monitorQueue, _name + " DataQueue");
132     _heartbeatQueue.startMonitor(queueMonitorInterval*1000, ClientMain._monitorQueue, _name + " HeartbeatQueue");
133     _otherQueue.startMonitor(queueMonitorInterval*1000, ClientMain._monitorQueue, _name + " OtherQueue");
134 tdb 1.10 _allQueue.startMonitor(queueMonitorInterval*1000, ClientMain._monitorQueue, _name + " AllQueue");
135 tdb 1.9 } catch (PropertyNotFoundException e) {
136     _logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e);
137     }
138    
139 ajm 1.3 // get the configuration for this plug-in setup
140 tdb 1.8 String pluginsPackage, pluginsList;
141     try {
142     pluginsPackage = cp.getProperty(_name, "Monitor.PluginsPackage");
143     pluginsList = cp.getProperty(_name, "Monitor.Plugins");
144     } catch(PropertyNotFoundException e) {
145     _logger.write(toString(), Logger.WARNING, "Unable to get required configuration, Monitor's will not be activated: "+e);
146     // setting these will ensure we don't build a pipeline
147     pluginsPackage = "";
148     pluginsList = "";
149     }
150 ajm 1.3
151     StringTokenizer st = new StringTokenizer(pluginsList, ";");
152    
153     while(st.hasMoreTokens()) {
154     String className = pluginsPackage + "." + st.nextToken() + _suffix;
155     _logger.write(toString(), Logger.DEBUG, "Attempting to create plugin: "+className);
156    
157     // Create an instance of the specified PluginMonitor to include
158     // within the monitorPipe. Add it to the monitorPipeline
159     try {
160     PluginMonitor pm = (PluginMonitor)ClassLoader.getSystemClassLoader().loadClass(className).newInstance();
161     _monitorPipeline.add(pm);
162     _logger.write(toString(), Logger.DEBUG, "Added monitor: "+className+" ("+pm.getDescription()+")");
163     }
164     catch (InstantiationException e){
165     _logger.write(toString(), Logger.ERROR, "Failed to instantiate "+className+" to the plugin monitor pipeline.");
166     _logger.write(toString(), Logger.ERROR, e.getMessage());
167     }
168     catch (Exception e){
169     _logger.write(toString(), Logger.ERROR, "Failed to add "+className+" to the plugin monitor pipeline.");
170     _logger.write(toString(), Logger.ERROR, e.toString());
171     }
172     }
173     _logger.write(toString(), Logger.SYSMSG, "The monitor pipeline has been set up with "+_monitorPipeline.size()+" plugin monitors.");
174 tdb 1.1 }
175    
176     //---PUBLIC METHODS---
177    
178 ajm 1.13 /**
179     * Runs the MonitorManager. This reads data from the
180     * inbound queue (from the ClientServant). And passes
181     * it into the appropriate queue for processing by the monitor
182     * threads.
183     */
184 tdb 1.1 public void run() {
185 tdb 1.4 // construct now, and use multiple times
186     XMLPacketMaker xmlPacketMaker = new XMLPacketMaker();
187 ajm 1.3
188 tdb 1.1 boolean run=true;
189 tdb 1.7
190     // keep these out here, saves recreating the object
191     String xml = null;
192 tdb 1.1 while(run) {
193     try {
194 ajm 1.3 xml = (String) _queue.get(_qID);
195 tdb 1.1 }
196     catch(InvalidQueueException e) {
197     _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
198     }
199    
200     // make an XML packet
201 tdb 1.2 XMLPacket packet = null;
202 tdb 1.1
203 tdb 1.2 try {
204 tdb 1.4 packet = xmlPacketMaker.createXMLPacket(xml);
205 tdb 1.2 } catch(InvalidXMLException e) {
206     _logger.write(toString(), Logger.ERROR, "Invalid XML: "+e);
207     // skip the rest of this loop iteration
208     continue;
209     }
210 tdb 1.9
211     // examine the packet and place it in the relevant outgoing queue
212     if(packet.getParam("packet.attributes.type").equals("data")) {
213     _dataQueue.add(packet);
214     }
215     else if(packet.getParam("packet.attributes.type").equals("heartbeat")) {
216     _heartbeatQueue.add(packet);
217     }
218     else {
219     _otherQueue.add(packet);
220 tdb 1.1 }
221 tdb 1.10 // always add to all queue
222     _allQueue.add(packet);
223 tdb 1.1 }
224     }
225    
226     /**
227     * Overrides the {@link java.lang.Object#toString() Object.toString()}
228     * method to provide clean logging (every class should have this).
229     *
230 tdb 1.14 * This uses the uk.org.iscream.cms.server.util.FormatName class
231 tdb 1.1 * to format the toString()
232     *
233     * @return the name of this class and its CVS revision
234     */
235     public String toString() {
236     return FormatName.getName(
237     _name,
238     getClass().getName(),
239     REVISION);
240     }
241    
242     //---PRIVATE METHODS---
243    
244     //---ACCESSOR/MUTATOR METHODS---
245 tdb 1.9
246 ajm 1.13 /**
247     * Allows Monitors to obtain
248     * the queue of data packets
249     */
250 tdb 1.9 public Queue getDataQueue() {
251     return _dataQueue;
252     }
253    
254 ajm 1.13 /**
255     * Allows Monitors to obtain
256     * the queue of heatbeat packets
257     */
258 tdb 1.9 public Queue getHeartbeatQueue() {
259     return _heartbeatQueue;
260     }
261    
262 ajm 1.13 /**
263     * Allows Monitors to obtain
264     * the queue of all other packets
265     */
266 tdb 1.9 public Queue getOtherQueue() {
267     return _otherQueue;
268     }
269    
270 ajm 1.13 /**
271     * In case a Monitor wants more
272     * than one type of packet,
273     * this queue can be obtained.
274     */
275 tdb 1.10 public Queue getAllQueue() {
276     return _allQueue;
277     }
278    
279 tdb 1.1 //---ATTRIBUTES---
280    
281     /**
282     * This is the friendly identifier of the
283     * component this class is running in.
284     * eg, a Filter may be called "filter1",
285     * If this class does not have an owning
286     * component, a name from the configuration
287     * can be placed here. This name could also
288     * be changed to null for utility classes.
289     */
290     private String _name = ClientMain.NAME;
291    
292     /**
293     * This holds a reference to the
294     * system logger that is being used.
295     */
296     private Logger _logger = ReferenceManager.getInstance().getLogger();
297    
298     /**
299 ajm 1.3 * A reference to the reference manager in use
300     */
301     private ReferenceManager _refman = ReferenceManager.getInstance();
302    
303     /**
304 tdb 1.9 * A reference to our incoming Queue
305 tdb 1.1 */
306     private Queue _queue;
307 ajm 1.3
308     /**
309 tdb 1.9 * Our incoming queue ID
310 ajm 1.3 */
311     private int _qID;
312    
313     /**
314     * file name suffix for plugin monitor classes:
315     */
316     private final String _suffix = "__Monitor";
317    
318     /**
319     * LinkedList for holding the PluginMonitor objects (the pipeline).
320     */
321     private LinkedList _monitorPipeline = new LinkedList();
322 tdb 1.9
323     /**
324     * Outgoing data Queue
325     */
326     private Queue _dataQueue;
327    
328     /**
329     * Outgoing heartbeat Queue
330     */
331     private Queue _heartbeatQueue;
332    
333     /**
334     * Outgoing other Queue
335     */
336     private Queue _otherQueue;
337 tdb 1.10
338     /**
339     * Outgoing ALL Queue
340     */
341     private Queue _allQueue;
342 tdb 1.9
343 tdb 1.1 //---STATIC ATTRIBUTES---
344 ajm 1.3
345     /**
346     * A reference to the single instance of this class
347     */
348     private static MonitorManager _instance;
349 tdb 1.1
350     }