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.6
Committed: Fri Mar 23 02:54:33 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.5: +2 -8 lines
Log Message:
Opps... we were going round and round in circles. AlerterManager and an Alerter
both needed to reference each other whilst being constructed... now that's not
going to work :)

File Contents

# User Rev Content
1 ajm 1.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 ajm 1.3 * 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 ajm 1.1 *
20 ajm 1.4 * @author $Author: ajm4 $
21 tdb 1.6 * @version $Id: AlerterSkeleton.java,v 1.5 2001/03/23 02:30:44 ajm4 Exp $
22 ajm 1.1 */
23     public abstract class AlerterSkeleton extends Thread implements PluginAlerter {
24    
25     //---FINAL ATTRIBUTES---
26    
27 ajm 1.3 /**
28     * The current CVS revision of this class
29     */
30 tdb 1.6 public final String REVISION = "$Revision: 1.5 $";
31 ajm 1.3
32     /**
33     * The default level to send alerts at
34     */
35 ajm 1.1 public final String DEFAULT_LEVEL = Alert.alertLevels[0];
36    
37 ajm 1.3 /**
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 ajm 1.1 //---STATIC METHODS---
44    
45     //---CONSTRUCTORS---
46    
47 ajm 1.3 /**
48     * Constructs and starts the alerter reading alerts
49     */
50 ajm 1.1 public AlerterSkeleton() {
51     _logger.write(toString(), Logger.SYSINIT, "started.");
52     this.start();
53     }
54    
55     //---PUBLIC METHODS---
56    
57 ajm 1.4 /**
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 ajm 1.1 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 tdb 1.2 /**
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 ajm 1.1 protected abstract void sendAlert(Alert alert);
96 tdb 1.2
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 ajm 1.1 protected String processAlertMessage(String message, Alert alert) {
106 tdb 1.2 // get some values
107 ajm 1.1 String alertType = Alert.alertLevels[alert.getLevel()];
108     String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
109 ajm 1.5 String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime()), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
110 ajm 1.1 String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
111    
112 tdb 1.2 // replace fields in message
113 ajm 1.1 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 ajm 1.3 * Return the String representation of what the alerter does
127     *
128     * @return the description
129 ajm 1.1 */
130     public abstract String getDescription();
131    
132    
133     //---PRIVATE METHODS---
134    
135     //---ACCESSOR/MUTATOR METHODS---
136 tdb 1.2
137     /**
138     * Gets hold of the Alerts queue from the AlerterManager.
139     *
140     * @return The Alerts queue
141     */
142 ajm 1.1 protected Queue getQueue() {
143     return AlerterManager.getInstance().getQueue();
144     }
145    
146 tdb 1.2 /**
147     * Gets an id to an individual queue
148     *
149     * @return the id to the queue
150     */
151 ajm 1.1 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 ajm 1.3 /**
173     * This holds a reference to the
174     * system logger that is being used.
175     */
176 ajm 1.1 protected Logger _logger = ReferenceManager.getInstance().getLogger();
177    
178 ajm 1.3 /**
179     * A reference to the singleton configuration proxy
180     * in use, through which alerter configuration can be obtained
181     */
182 ajm 1.1 protected ConfigurationProxy _cp = ConfigurationProxy.getInstance();
183    
184 ajm 1.3 /**
185     * The ID of the queue the alerter will use.
186     * Initially -1, but initialised on first use.
187     */
188 ajm 1.1 protected int _qID = -1;
189    
190 ajm 1.3 /**
191     * The state of the alerter thread
192     */
193 ajm 1.1 protected boolean _running = true;
194    
195     //---STATIC ATTRIBUTES---
196    
197     }