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