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