1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.client; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.componentmanager.*; |
6 |
import uk.ac.ukc.iscream.core.*; |
7 |
import uk.ac.ukc.iscream.util.*; |
8 |
import java.util.*; |
9 |
|
10 |
/** |
11 |
* A manager for the Monitors. |
12 |
* |
13 |
* @author $Author: tdb1 $ |
14 |
* @version $Id: MonitorManager.java,v 1.2 2001/03/01 16:52:25 tdb1 Exp $ |
15 |
*/ |
16 |
class AlerterManager 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.2 $"; |
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 static AlerterManager getInstance() { |
32 |
if (_instance == null){ |
33 |
_instance = new AlerterManager(); |
34 |
} |
35 |
return _instance; |
36 |
} |
37 |
|
38 |
//---CONSTRUCTORS--- |
39 |
|
40 |
public AlerterManager() { |
41 |
_queue = ClientMain._alerterQueue; |
42 |
_qID = _queue.getQueue(); |
43 |
_logger.write(toString(), Logger.SYSINIT, "Initialising"); |
44 |
_logger.write(toString(), Logger.SYSMSG, "Creating alerter pipeline for plugin alerters ..."); |
45 |
|
46 |
// get the configuration for this plug-in setup |
47 |
Configuration config = _refman.getCM().getConfiguration(_name); |
48 |
String pluginsPackage = config.getProperty("Alerter.PluginsPackage"); |
49 |
String pluginsList = config.getProperty("Alerter.Plugins"); |
50 |
|
51 |
StringTokenizer st = new StringTokenizer(pluginsList, ";"); |
52 |
|
53 |
while(st.hasMoreTokens()) { |
54 |
String className = pluginsPackage + "." + st.nextToken() + _suffix; |
55 |
_logger.write(toString(), Logger.DEBUG, "Attempting to create plugin: "+className); |
56 |
|
57 |
// Create an instance of the specified PluginAlerter to include |
58 |
// within the alerterPipe. Add it to the alerterPipeline |
59 |
try { |
60 |
PluginAlerter pa = (PluginAlerter)ClassLoader.getSystemClassLoader().loadClass(className).newInstance(); |
61 |
_alerterPipeline.add(pa); |
62 |
_logger.write(toString(), Logger.DEBUG, "Added alerter: "+className+" ("+pa.getDescription()+")"); |
63 |
} |
64 |
catch (InstantiationException e){ |
65 |
_logger.write(toString(), Logger.ERROR, "Failed to instantiate "+className+" to the plugin alerter pipeline."); |
66 |
_logger.write(toString(), Logger.ERROR, e.getMessage()); |
67 |
} |
68 |
catch (Exception e){ |
69 |
_logger.write(toString(), Logger.ERROR, "Failed to add "+className+" to the plugin alerter pipeline."); |
70 |
_logger.write(toString(), Logger.ERROR, e.toString()); |
71 |
} |
72 |
} |
73 |
_logger.write(toString(), Logger.SYSMSG, "The alerter pipeline has been set up with "+_alerterPipeline.size()+" plugin alerters."); |
74 |
} |
75 |
|
76 |
//---PUBLIC METHODS--- |
77 |
|
78 |
public void run() { |
79 |
|
80 |
boolean run=true; |
81 |
while(run) { |
82 |
// attempt to get some data from the Queue |
83 |
Alert alert = null; |
84 |
try { |
85 |
alert = (Alert) _queue.get(_qID); |
86 |
} |
87 |
catch(InvalidQueueException e) { |
88 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
89 |
} |
90 |
|
91 |
// for each alerter in the pipeline... |
92 |
Iterator pluginAlerters = _alerterPipeline.iterator(); |
93 |
while (pluginAlerters.hasNext()){ |
94 |
PluginAlerter alerter = (PluginAlerter)pluginAlerters.next(); |
95 |
alerter.sendAlert(alert); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
/** |
101 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
102 |
* method to provide clean logging (every class should have this). |
103 |
* |
104 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
105 |
* to format the toString() |
106 |
* |
107 |
* @return the name of this class and its CVS revision |
108 |
*/ |
109 |
public String toString() { |
110 |
return FormatName.getName( |
111 |
_name, |
112 |
getClass().getName(), |
113 |
REVISION); |
114 |
} |
115 |
|
116 |
//---PRIVATE METHODS--- |
117 |
|
118 |
//---ACCESSOR/MUTATOR METHODS--- |
119 |
|
120 |
//---ATTRIBUTES--- |
121 |
|
122 |
/** |
123 |
* This is the friendly identifier of the |
124 |
* component this class is running in. |
125 |
* eg, a Filter may be called "filter1", |
126 |
* If this class does not have an owning |
127 |
* component, a name from the configuration |
128 |
* can be placed here. This name could also |
129 |
* be changed to null for utility classes. |
130 |
*/ |
131 |
private String _name = ClientMain.NAME; |
132 |
|
133 |
/** |
134 |
* This holds a reference to the |
135 |
* system logger that is being used. |
136 |
*/ |
137 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
138 |
|
139 |
/** |
140 |
* A reference to the reference manager in use |
141 |
*/ |
142 |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
143 |
|
144 |
/** |
145 |
* A reference to our Queue |
146 |
*/ |
147 |
private Queue _queue; |
148 |
|
149 |
/** |
150 |
* Our queue ID |
151 |
*/ |
152 |
private int _qID; |
153 |
|
154 |
/** |
155 |
* file name suffix for plugin alerter classes: |
156 |
*/ |
157 |
private final String _suffix = "__Alerter"; |
158 |
|
159 |
/** |
160 |
* LinkedList for holding the PluginAlerter objects (the pipeline). |
161 |
*/ |
162 |
private LinkedList _alerterPipeline = new LinkedList(); |
163 |
|
164 |
//---STATIC ATTRIBUTES--- |
165 |
|
166 |
/** |
167 |
* A reference to the single instance of this class |
168 |
*/ |
169 |
private static AlerterManager _instance; |
170 |
|
171 |
} |