| 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: ajm4 $ |
| 21 |
* @version $Id: AlerterSkeleton.java,v 1.4 2001/03/23 01:32:38 ajm4 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.4 $"; |
| 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." + _name + ".level"); |
| 74 |
} catch (PropertyNotFoundException e) { |
| 75 |
levelName = DEFAULT_LEVEL; |
| 76 |
_logger.write(toString(), Logger.WARNING, "Alerter." + _name + ".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 timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime()), "%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%", timeFirstSince); |
| 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 |
//---ATTRIBUTES--- |
| 160 |
|
| 161 |
/** |
| 162 |
* This is the friendly identifier of the |
| 163 |
* component this class is running in. |
| 164 |
* eg, a Filter may be called "filter1", |
| 165 |
* If this class does not have an owning |
| 166 |
* component, a name from the configuration |
| 167 |
* can be placed here. This name could also |
| 168 |
* be changed to null for utility classes. |
| 169 |
*/ |
| 170 |
protected String _name = "AlerterSkeleton"; |
| 171 |
|
| 172 |
/** |
| 173 |
* This holds a reference to the |
| 174 |
* system logger that is being used. |
| 175 |
*/ |
| 176 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 177 |
|
| 178 |
/** |
| 179 |
* A reference to the singleton configuration proxy |
| 180 |
* in use, through which alerter configuration can be obtained |
| 181 |
*/ |
| 182 |
protected ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
| 183 |
|
| 184 |
/** |
| 185 |
* A reference to the Alerter queue, into which |
| 186 |
* all new alerts will be placed. |
| 187 |
*/ |
| 188 |
protected Queue _alerterQueue = AlerterManager.getInstance().getQueue(); |
| 189 |
|
| 190 |
/** |
| 191 |
* The ID of the queue the alerter will use. |
| 192 |
* Initially -1, but initialised on first use. |
| 193 |
*/ |
| 194 |
protected int _qID = -1; |
| 195 |
|
| 196 |
/** |
| 197 |
* The state of the alerter thread |
| 198 |
*/ |
| 199 |
protected boolean _running = true; |
| 200 |
|
| 201 |
//---STATIC ATTRIBUTES--- |
| 202 |
|
| 203 |
} |