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