ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/AlerterSkeleton.java
Revision: 1.3
Committed: Fri Mar 23 01:09:16 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
Changes since 1.2: +46 -3 lines
Log Message:
Added a couple more vars that are global to alerters.
Finished javadoc.

File Contents

# Content
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.2 2001/03/23 00:05:23 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.21 $";
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 public void run() {
58 while(_running) {
59 try {
60 Alert alert = (Alert) getQueue().get(getQueueId());
61 String levelName;
62 try {
63 levelName = _cp.getProperty(_name, "Alerter." + _name + ".level");
64 } catch (PropertyNotFoundException e) {
65 levelName = DEFAULT_LEVEL;
66 _logger.write(toString(), Logger.WARNING, "Alerter." + _name + ".level value unavailable using default of " + levelName);
67 }
68 int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
69 // only send if it's equal (or above) our level
70 if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
71 sendAlert(alert);
72 }
73 } catch (InvalidQueueException e) {
74 _logger.write(this.toString(), Logger.ERROR, "Unable to get queue.");
75 }
76 }
77 }
78
79 /**
80 * Sends an alert object using whatever transport
81 * mechanism the extending class is implementing.
82 *
83 * @param alert the Alert to send
84 */
85 protected abstract void sendAlert(Alert alert);
86
87 /**
88 * Processes a message, replace key fields (eg. %level%) with
89 * representative text from the given Alert object.
90 *
91 * @param message The message to process
92 * @param alert The Alert object to get data from
93 * @return the processed message
94 */
95 protected String processAlertMessage(String message, Alert alert) {
96 // get some values
97 String alertType = Alert.alertLevels[alert.getLevel()];
98 String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
99 String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
100 String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
101
102 // replace fields in message
103 message = StringUtils.replaceText(message, "%level%", alertType);
104 message = StringUtils.replaceText(message, "%threshold%", thresholdType);
105 message = StringUtils.replaceText(message, "%source%", alert.getSource());
106 message = StringUtils.replaceText(message, "%value%", alert.getValue());
107 message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
108 message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
109 message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
110 message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
111 message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
112 return message;
113 }
114
115 /**
116 * Return the String representation of what the alerter does
117 *
118 * @return the description
119 */
120 public abstract String getDescription();
121
122
123 //---PRIVATE METHODS---
124
125 //---ACCESSOR/MUTATOR METHODS---
126
127 /**
128 * Gets hold of the Alerts queue from the AlerterManager.
129 *
130 * @return The Alerts queue
131 */
132 protected Queue getQueue() {
133 return AlerterManager.getInstance().getQueue();
134 }
135
136 /**
137 * Gets an id to an individual queue
138 *
139 * @return the id to the queue
140 */
141 protected int getQueueId() {
142 if (_qID == -1) {
143 _qID = getQueue().getQueue();
144 _logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID);
145 }
146 return _qID;
147 }
148
149 //---ATTRIBUTES---
150
151 /**
152 * This is the friendly identifier of the
153 * component this class is running in.
154 * eg, a Filter may be called "filter1",
155 * If this class does not have an owning
156 * component, a name from the configuration
157 * can be placed here. This name could also
158 * be changed to null for utility classes.
159 */
160 protected String _name = "AlerterSkeleton";
161
162 /**
163 * This holds a reference to the
164 * system logger that is being used.
165 */
166 protected Logger _logger = ReferenceManager.getInstance().getLogger();
167
168 /**
169 * A reference to the singleton configuration proxy
170 * in use, through which alerter configuration can be obtained
171 */
172 protected ConfigurationProxy _cp = ConfigurationProxy.getInstance();
173
174 /**
175 * A reference to the LocalClient alerter queue, into which
176 * all new alerts will be placed.
177 */
178 protected Queue _alerterQueue = ClientMain._alerterQueue;
179
180 /**
181 * The ID of the queue the alerter will use.
182 * Initially -1, but initialised on first use.
183 */
184 protected int _qID = -1;
185
186 /**
187 * The state of the alerter thread
188 */
189 protected boolean _running = true;
190
191 //---STATIC ATTRIBUTES---
192
193 }