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.14
Committed: Sun Sep 25 09:57:40 2005 UTC (18 years, 7 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +4 -3 lines
Log Message:
Fix compile problems on j2se 1.5 - our Queue class conflicted with one in
java.util. Also fix an API issue when running the server on Windows - the
println method sends '\r\n' on Windows instead of '\n' on Unix, which
confuses applications such as ihost.

Patch provided by: skel

File Contents

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