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 |
* |
15 |
* @author $Author: ajm4 $ |
16 |
* @version $Id: AlerterSkeleton.java,v 1.1 2001/03/22 23:35:11 ajm4 Exp $ |
17 |
*/ |
18 |
public abstract class AlerterSkeleton extends Thread implements PluginAlerter { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
public final String DEFAULT_LEVEL = Alert.alertLevels[0]; |
23 |
|
24 |
//---STATIC METHODS--- |
25 |
|
26 |
//---CONSTRUCTORS--- |
27 |
|
28 |
public AlerterSkeleton() { |
29 |
_logger.write(toString(), Logger.SYSINIT, "started."); |
30 |
this.start(); |
31 |
} |
32 |
|
33 |
//---PUBLIC METHODS--- |
34 |
|
35 |
public void run() { |
36 |
while(_running) { |
37 |
try { |
38 |
Alert alert = (Alert) getQueue().get(getQueueId()); |
39 |
String levelName; |
40 |
try { |
41 |
levelName = _cp.getProperty(_name, "Alerter." + _name + ".level"); |
42 |
} catch (PropertyNotFoundException e) { |
43 |
levelName = DEFAULT_LEVEL; |
44 |
_logger.write(toString(), Logger.WARNING, "Alerter." + _name + ".level value unavailable using default of " + levelName); |
45 |
} |
46 |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
47 |
// only send if it's equal (or above) our level |
48 |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
49 |
sendAlert(alert); |
50 |
} |
51 |
} catch (InvalidQueueException e) { |
52 |
_logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); |
53 |
} |
54 |
} |
55 |
} |
56 |
|
57 |
/** |
58 |
* Sends an alert object using whatever transport |
59 |
* mechanism the extending class is implementing. |
60 |
* |
61 |
* @param alert the Alert to send |
62 |
*/ |
63 |
protected abstract void sendAlert(Alert alert); |
64 |
|
65 |
/** |
66 |
* Processes a message, replace key fields (eg. %level%) with |
67 |
* representative text from the given Alert object. |
68 |
* |
69 |
* @param message The message to process |
70 |
* @param alert The Alert object to get data from |
71 |
* @return the processed message |
72 |
*/ |
73 |
protected String processAlertMessage(String message, Alert alert) { |
74 |
// get some values |
75 |
String alertType = Alert.alertLevels[alert.getLevel()]; |
76 |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
77 |
String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
78 |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
79 |
|
80 |
// replace fields in message |
81 |
message = StringUtils.replaceText(message, "%level%", alertType); |
82 |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
83 |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
84 |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
85 |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
86 |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
87 |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
88 |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
89 |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
90 |
return message; |
91 |
} |
92 |
|
93 |
/** |
94 |
* return the String representation of what the monitor does |
95 |
*/ |
96 |
public abstract String getDescription(); |
97 |
|
98 |
|
99 |
//---PRIVATE METHODS--- |
100 |
|
101 |
//---ACCESSOR/MUTATOR METHODS--- |
102 |
|
103 |
/** |
104 |
* Gets hold of the Alerts queue from the AlerterManager. |
105 |
* |
106 |
* @return The Alerts queue |
107 |
*/ |
108 |
protected Queue getQueue() { |
109 |
return AlerterManager.getInstance().getQueue(); |
110 |
} |
111 |
|
112 |
/** |
113 |
* Gets an id to an individual queue |
114 |
* |
115 |
* @return the id to the queue |
116 |
*/ |
117 |
protected int getQueueId() { |
118 |
if (_qID == -1) { |
119 |
_qID = getQueue().getQueue(); |
120 |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
121 |
} |
122 |
return _qID; |
123 |
} |
124 |
|
125 |
//---ATTRIBUTES--- |
126 |
|
127 |
/** |
128 |
* This is the friendly identifier of the |
129 |
* component this class is running in. |
130 |
* eg, a Filter may be called "filter1", |
131 |
* If this class does not have an owning |
132 |
* component, a name from the configuration |
133 |
* can be placed here. This name could also |
134 |
* be changed to null for utility classes. |
135 |
*/ |
136 |
protected String _name = "AlerterSkeleton"; |
137 |
|
138 |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
139 |
|
140 |
protected ConfigurationProxy _cp = ConfigurationProxy.getInstance(); |
141 |
|
142 |
protected Queue _alerterQueue = ClientMain._alerterQueue; |
143 |
|
144 |
protected int _qID = -1; |
145 |
|
146 |
protected boolean _running = true; |
147 |
|
148 |
//---STATIC ATTRIBUTES--- |
149 |
|
150 |
} |