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.12
Committed: Wed Feb 5 16:43:44 2003 UTC (21 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.11: +3 -3 lines
Log Message:
Changed the server to use the external util package. Quite a minor change,
but does affect a lot of files.

File Contents

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