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/alerters/EMail__Alerter.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/EMail__Alerter.java (file contents):
Revision 1.4 by tdb, Fri Mar 2 00:37:03 2001 UTC vs.
Revision 1.19 by ajm, Thu Mar 22 22:07:58 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.client.alerters;
2 > package uk.org.iscream.client.alerters;
3  
4   //---IMPORTS---
5 < import uk.ac.ukc.iscream.client.*;
6 < import uk.ac.ukc.iscream.core.*;
7 < import uk.ac.ukc.iscream.util.*;
8 < import uk.ac.ukc.iscream.componentmanager.*;
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  
10   import java.util.*;
11   import java.io.*;
12 + import java.text.*;
13  
14   /**
15 < * This Alert sends by e-mail.
15 > * This alerter delivers alerts using e-mail.
16   *
17   * @author  $Author$
18   * @version $Id$
19   */
20 < public class EMail__Alerter implements PluginAlerter {
20 > public class EMail__Alerter extends Thread implements PluginAlerter {
21  
22   //---FINAL ATTRIBUTES---
23  
# Line 25 | Line 26 | public class EMail__Alerter implements PluginAlerter {
26       */
27      public final String REVISION = "$Revision$";
28      
29 <    public final String DESC = "Sends alerts by e-mail";
29 >    public final String DESC = "Sends alerts over e-mail.";
30      
31 +    public final String DEFAULT_LEVEL = Alert.alertLevels[0];
32 +    
33 +    public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
34 +    
35   //---STATIC METHODS---
36  
37   //---CONSTRUCTORS---
38  
39 <    public EMail__Alerter() {
40 <        // get the configuration for this alerter
41 <        Configuration config = ReferenceManager.getInstance().getCM().getConfiguration(_name);
37 <        
38 <        // an integer value
39 <        _level = Integer.parseInt(config.getProperty("Alerter.EMail.level"));
40 <        // a semi-colon seperated list of destination addresses
41 <        _destList = config.getProperty("Alerter.EMail.destList");
42 <        // a single from address
43 <        _sender = config.getProperty("Alerter.EMail.sender");
44 <        // the fqdn of an smtp server
45 <        _smtpServer = config.getProperty("Alerter.EMail.smtpServer");
46 <        // a subject with the following: %level% and %message%
47 <        // eg, "i-scream e-mail alert: level %level%, %message%"
48 <        _subject = config.getProperty("Alerter.EMail.subject");
49 <        // a message body with the following: %level% and %message%
50 <        _message = config.getProperty("Alerter.EMail.message");
39 >    public EMail__Alerter() {      
40 >        _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
41 >        this.start();
42      }
43  
44   //---PUBLIC METHODS---
45  
46      public void sendAlert(Alert alert) {
47 +        ConfigurationProxy cp = ConfigurationProxy.getInstance();
48 +        String levelName;
49 +        try {
50 +            levelName = cp.getProperty(_name, "Alerter.EMail.level");
51 +        } catch (PropertyNotFoundException e) {
52 +            levelName = DEFAULT_LEVEL;
53 +            _logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName);
54 +        }
55 +        int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
56          // only send if it's equal (or above) our level
57 <        if(alert.getLevel() >= _level) {
57 >        if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
58 >            String alertType = Alert.alertLevels[alert.getLevel()];
59 >            String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
60 >            String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
61 >            String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
62 >
63              // sort out the subject
64 <            String subject = _subject;
65 <            subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel()));
64 >            String subject;
65 >            try {
66 >                subject = cp.getProperty(_name, "Alerter.EMail.subject");
67 >            } catch (PropertyNotFoundException e) {
68 >                subject = NOT_CONFIGURED;
69 >                _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
70 >            }
71 >            subject = StringUtils.replaceText(subject, "%level%", alertType);
72 >            subject = StringUtils.replaceText(subject, "%threshold%", thresholdType);
73              subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
74              subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
75              subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
76              subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
77 <            
77 >            subject = StringUtils.replaceText(subject, "%timeTillNextAlert%",  DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
78 >            subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince);
79 >            subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured);
80 >                        
81              // sort out the message body
82 <            String message = _message;
83 <            message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel()));
82 >            String message;
83 >            try {
84 >                message = cp.getProperty(_name, "Alerter.EMail.message");
85 >            } catch (PropertyNotFoundException e) {
86 >                message = NOT_CONFIGURED;
87 >                _logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message);
88 >            }
89 >            message = StringUtils.replaceText(message, "%level%", alertType);
90 >            message = StringUtils.replaceText(message, "%threshold%", thresholdType);
91              message = StringUtils.replaceText(message, "%source%", alert.getSource());
92              message = StringUtils.replaceText(message, "%value%", alert.getValue());
93              message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
94              message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
95 <            
95 >            message = StringUtils.replaceText(message, "%timeTillNextAlert%",  DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
96 >            message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
97 >            message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
98 >                        
99              try {
100                  // create SMTP message
101 <                Smtp smtp = new Smtp(_smtpServer);
101 >                Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer"));
102                  // set our sender
103 <                smtp.setSender(_sender);
103 >                smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
104                  
105 <                // set the to list
106 <                StringTokenizer st = new StringTokenizer(_destList, ";");
105 >                String destList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
106 >                
107 >                // set the to: list
108 >                StringTokenizer st = new StringTokenizer(destList, ";");
109                  while (st.hasMoreTokens()) {
110                      smtp.setTo(st.nextToken());
111                  }
# Line 92 | Line 119 | public class EMail__Alerter implements PluginAlerter {
119                  out.println(message);
120                  smtp.sendMessage();
121                  smtp.close();
122 +                _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
123              }
124              catch(IOException e) {
125                  _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
126              }
127 +            catch(PropertyNotFoundException e) {
128 +                _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
129 +            }
130          }
131      }
132  
# Line 103 | Line 134 | public class EMail__Alerter implements PluginAlerter {
134       * Overrides the {@link java.lang.Object#toString() Object.toString()}
135       * method to provide clean logging (every class should have this).
136       *
137 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
137 >     * This uses the uk.org.iscream.util.NameFormat class
138       * to format the toString()
139       *
140       * @return the name of this class and its CVS revision
# Line 123 | Line 154 | public class EMail__Alerter implements PluginAlerter {
154      }
155  
156   //---PRIVATE METHODS---
157 <
157 >    
158   //---ACCESSOR/MUTATOR METHODS---
159  
160 < //---ATTRIBUTES---
161 <
162 <    // an integer value
132 <    private int _level;
160 >    protected Queue getQueue() {
161 >        return AlerterManager.getInstance().getQueue();
162 >    }
163      
164 <    // a semi-colon seperated list of destination addresses
165 <    private String _destList;
166 <    
167 <    // a single sender address
168 <    private String _sender;
169 <    
170 <    // the fqdn of an smtp server
141 <    private String _smtpServer;
142 <    
143 <    // a subject with the following: %level% and %message%
144 <    // eg, "i-scream e-mail alert: level %level%, %message%"
145 <    private String _subject;
164 >    protected int getQueueId() {
165 >        if (_qID == -1) {
166 >            _qID = getQueue().getQueue();
167 >            _logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID);
168 >        }
169 >        return _qID;
170 >    }
171  
172 <    // a message body with the following: %level% and %message%
173 <    private String _message;
149 <
172 > //---ATTRIBUTES---
173 >    
174      /**
175       * This is the friendly identifier of the
176       * component this class is running in.
# Line 156 | Line 180 | public class EMail__Alerter implements PluginAlerter {
180       * can be placed here.  This name could also
181       * be changed to null for utility classes.
182       */
183 <    private String _name = ClientMain.NAME;
183 >    private String _name = "EMail Alert";
184  
185      /**
186       * This holds a reference to the
187       * system logger that is being used.
188       */
189      private Logger _logger = ReferenceManager.getInstance().getLogger();
190 +    
191 +    /**
192 +     * The queue id for this alerters queue in the alert queue
193 +     */
194 +    private int _qID = -1;
195 +    
196 +    /**
197 +     * The running status of the alerter
198 +     */
199 +    private boolean _running = true;
200  
201   //---STATIC ATTRIBUTES---
202  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines