1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.client; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.org.iscream.client.*; |
6 |
import uk.org.iscream.core.*; |
7 |
import uk.org.iscream.util.*; |
8 |
import uk.org.iscream.componentmanager.*; |
9 |
import java.text.*; |
10 |
import java.util.*; |
11 |
|
12 |
/** |
13 |
* Skeleton class for Alerters |
14 |
* This skeleton reads alerts from the alert queue designated |
15 |
* to the Alerter, it then feeds the alerts to the sendAlert |
16 |
* method, which the extending class should implement. The class |
17 |
* should then handle sending the message. This class also has a |
18 |
* method to allow formatting of messages. |
19 |
* |
20 |
* @author $Author: tdb1 $ |
21 |
* @version $Id: AlerterSkeleton.java,v 1.7 2001/05/13 01:47:42 tdb1 Exp $ |
22 |
*/ |
23 |
public abstract class AlerterSkeleton extends Thread implements PluginAlerter { |
24 |
|
25 |
//---FINAL ATTRIBUTES--- |
26 |
|
27 |
/** |
28 |
* The current CVS revision of this class |
29 |
*/ |
30 |
public final String REVISION = "$Revision: 1.7 $"; |
31 |
|
32 |
/** |
33 |
* The default level to send alerts at |
34 |
*/ |
35 |
public final String DEFAULT_LEVEL = Alert.alertLevels[0]; |
36 |
|
37 |
/** |
38 |
* If a configuration property is not |
39 |
* configured, this will be used instead. |
40 |
*/ |
41 |
public final String NOT_CONFIGURED = "<NOT CONFIGURED>"; |
42 |
|
43 |
//---STATIC METHODS--- |
44 |
|
45 |
//---CONSTRUCTORS--- |
46 |
|
47 |
/** |
48 |
* Constructs and starts the alerter reading alerts |
49 |
*/ |
50 |
public AlerterSkeleton() { |
51 |
_logger.write(toString(), Logger.SYSINIT, "started."); |
52 |
this.start(); |
53 |
} |
54 |
|
55 |
//---PUBLIC METHODS--- |
56 |
|
57 |
/** |
58 |
* Reads the next alert from the alerter queue for this |
59 |
* alerter. It then looks up Alerter.<alerter name>.level in |
60 |
* the configuration to determine the lowest level of alert |
61 |
* the extending alerter should send. If the alert is |
62 |
* above this level, or if the alert is an OK for an alert |
63 |
* escalation which did rise above this alerters level, it then |
64 |
* calls sendAlert to tell the alerter to send the alert. If it |
65 |
* isn't above this level, it ignores the alert. |
66 |
*/ |
67 |
public void run() { |
68 |
while(_running) { |
69 |
try { |
70 |
Alert alert = (Alert) getQueue().get(getQueueId()); |
71 |
String levelName; |
72 |
try { |
73 |
levelName = _cp.getProperty(_name, "Alerter." + getFName() + ".level"); |
74 |
} catch (PropertyNotFoundException e) { |
75 |
levelName = DEFAULT_LEVEL; |
76 |
_logger.write(toString(), Logger.WARNING, "Alerter." + getFName() + ".level value unavailable using default of " + levelName); |
77 |
} |
78 |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
79 |
// only send if it's equal (or above) our level |
80 |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
81 |
sendAlert(alert); |
82 |
} |
83 |
} catch (InvalidQueueException e) { |
84 |
_logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
/** |
90 |
* Sends an alert object using whatever transport |
91 |
* mechanism the extending class is implementing. |
92 |
* |
93 |
* @param alert the Alert to send |
94 |
*/ |
95 |
protected abstract void sendAlert(Alert alert); |
96 |
|
97 |
/** |
98 |
* Processes a message, replace key fields (eg. %level%) with |
99 |
* representative text from the given Alert object. |
100 |
* |
101 |
* @param message The message to process |
102 |
* @param alert The Alert object to get data from |
103 |
* @return the processed message |
104 |
*/ |
105 |
protected String processAlertMessage(String message, Alert alert) { |
106 |
// get some values |
107 |
String alertType = Alert.alertLevels[alert.getLevel()]; |
108 |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
109 |
String timeSinceFirst = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
110 |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
111 |
|
112 |
// replace fields in message |
113 |
message = StringUtils.replaceText(message, "%level%", alertType); |
114 |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
115 |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
116 |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
117 |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
118 |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
119 |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
120 |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeSinceFirst); |
121 |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
122 |
return message; |
123 |
} |
124 |
|
125 |
/** |
126 |
* Return the String representation of what the alerter does |
127 |
* |
128 |
* @return the description |
129 |
*/ |
130 |
public abstract String getDescription(); |
131 |
|
132 |
|
133 |
//---PRIVATE METHODS--- |
134 |
|
135 |
//---ACCESSOR/MUTATOR METHODS--- |
136 |
|
137 |
/** |
138 |
* Gets hold of the Alerts queue from the AlerterManager. |
139 |
* |
140 |
* @return The Alerts queue |
141 |
*/ |
142 |
protected Queue getQueue() { |
143 |
return AlerterManager.getInstance().getQueue(); |
144 |
} |
145 |
|
146 |
/** |
147 |
* Gets an id to an individual queue |
148 |
* |
149 |
* @return the id to the queue |
150 |
*/ |
151 |
protected int getQueueId() { |
152 |
if (_qID == -1) { |
153 |
_qID = getQueue().getQueue(); |
154 |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
155 |
} |
156 |
return _qID; |
157 |
} |
158 |
|
159 |
/** |
160 |
* Returns the "friendly" name of this class. This |
161 |
* is simply an accessor for _name, required due to |
162 |
* inheritance issues with extending AlerterSkeleton. |
163 |
* |
164 |
* @return the friendly name |
165 |
*/ |
166 |
protected abstract String getFName(); |
167 |
|
168 |
//---ATTRIBUTES--- |
169 |
|
170 |
/** |
171 |
* This is the friendly identifier of the |
172 |
* component this class is running in. |
173 |
* eg, a Filter may be called "filter1", |
174 |
* If this class does not have an owning |
175 |
* component, a name from the configuration |
176 |
* can be placed here. This name could also |
177 |
* be changed to null for utility classes. |
178 |
*/ |
179 |
protected String _name = "AlerterSkeleton"; |
180 |
|
181 |
/** |
182 |
* This holds a reference to the |
183 |
* system logger that is being used. |
184 |
*/ |
185 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
186 |
|
187 |
/** |
188 |
* A reference to the singleton configuration proxy |
189 |
* in use, through which alerter configuration can be obtained |
190 |
*/ |
191 |
protected ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
192 |
|
193 |
/** |
194 |
* The ID of the queue the alerter will use. |
195 |
* Initially -1, but initialised on first use. |
196 |
*/ |
197 |
protected int _qID = -1; |
198 |
|
199 |
/** |
200 |
* The state of the alerter thread |
201 |
*/ |
202 |
protected boolean _running = true; |
203 |
|
204 |
//---STATIC ATTRIBUTES--- |
205 |
|
206 |
} |