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