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