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.11 by tdb, Mon Mar 5 02:09:49 2001 UTC vs.
Revision 1.18 by tdb, Fri Mar 16 17:13:49 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 alerter delivers alerts using e-mail.
# Line 27 | Line 28 | public class EMail__Alerter implements PluginAlerter {
28      
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---
# Line 39 | Line 44 | public class EMail__Alerter implements PluginAlerter {
44  
45      public void sendAlert(Alert alert) {
46          ConfigurationProxy cp = ConfigurationProxy.getInstance();
47 <        String levelName = cp.getProperty(_name, "Alerter.EMail.level");
47 >        String levelName;
48 >        try {
49 >            levelName = cp.getProperty(_name, "Alerter.EMail.level");
50 >        } catch (PropertyNotFoundException e) {
51 >            levelName = DEFAULT_LEVEL;
52 >            _logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName);
53 >        }
54          int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
55          // only send if it's equal (or above) our level
56 <        if(alert.getLevel() >= level) {
56 >        if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
57              String alertType = Alert.alertLevels[alert.getLevel()];
58              String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
59 +            String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
60 +            String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
61 +
62              // sort out the subject
63 <            String subject = cp.getProperty(_name, "Alerter.EMail.subject");
63 >            String subject;
64 >            try {
65 >                subject = cp.getProperty(_name, "Alerter.EMail.subject");
66 >            } catch (PropertyNotFoundException e) {
67 >                subject = NOT_CONFIGURED;
68 >                _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
69 >            }
70              subject = StringUtils.replaceText(subject, "%level%", alertType);
71              subject = StringUtils.replaceText(subject, "%threshold%", thresholdType);
72              subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
73              subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
74              subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
75              subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
76 <            subject = StringUtils.replaceText(subject, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
76 >            subject = StringUtils.replaceText(subject, "%timeTillNextAlert%",  DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
77 >            subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince);
78 >            subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured);
79                          
80              // sort out the message body
81 <            String message = cp.getProperty(_name, "Alerter.EMail.message");
81 >            String message;
82 >            try {
83 >                message = cp.getProperty(_name, "Alerter.EMail.message");
84 >            } catch (PropertyNotFoundException e) {
85 >                message = NOT_CONFIGURED;
86 >                _logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message);
87 >            }
88              message = StringUtils.replaceText(message, "%level%", alertType);
89              message = StringUtils.replaceText(message, "%threshold%", thresholdType);
90              message = StringUtils.replaceText(message, "%source%", alert.getSource());
91              message = StringUtils.replaceText(message, "%value%", alert.getValue());
92              message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
93              message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
94 <            message = StringUtils.replaceText(message, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
94 >            message = StringUtils.replaceText(message, "%timeTillNextAlert%",  DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
95 >            message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
96 >            message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
97                          
98              try {
99                  // create SMTP message
# Line 71 | Line 101 | public class EMail__Alerter implements PluginAlerter {
101                  // set our sender
102                  smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
103                  
104 <                // get the default destination list
75 <                String destList = cp.getProperty(_name, "Alerter.EMail.defaultDestList");
76 <                // check if the source we're alerting about has a specific destination
77 <                String sourceDestList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
78 <                if(sourceDestList != null) {
79 <                    // if there is a source destination list, use it
80 <                    destList = sourceDestList;
81 <                }
104 >                String destList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
105                  
106                  // set the to: list
107                  StringTokenizer st = new StringTokenizer(destList, ";");
# Line 100 | Line 123 | public class EMail__Alerter implements PluginAlerter {
123              catch(IOException e) {
124                  _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
125              }
126 +            catch(PropertyNotFoundException e) {
127 +                _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
128 +            }
129          }
130      }
131  
# Line 107 | Line 133 | public class EMail__Alerter implements PluginAlerter {
133       * Overrides the {@link java.lang.Object#toString() Object.toString()}
134       * method to provide clean logging (every class should have this).
135       *
136 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
136 >     * This uses the uk.org.iscream.util.NameFormat class
137       * to format the toString()
138       *
139       * @return the name of this class and its CVS revision
# Line 127 | Line 153 | public class EMail__Alerter implements PluginAlerter {
153      }
154  
155   //---PRIVATE METHODS---
130
131    private String getTimeString(long time) {
132        String timeString = null;
133        if (time >= 60) {
134            timeString = (time / 60) + " minutes";
135        } else if (time >= 3600) {
136            timeString = ((time/60) / 60) + " hours";
137        } else {
138            timeString = time + " seconds";
139        }
140        return timeString;
141    }
156      
157   //---ACCESSOR/MUTATOR METHODS---
158  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines