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.6 by tdb, Fri Mar 2 01:32:09 2001 UTC vs.
Revision 1.14 by tdb, Mon Mar 5 23:13:22 2001 UTC

# Line 11 | Line 11 | import java.util.*;
11   import java.io.*;
12  
13   /**
14 < * This Alert sends by e-mail.
14 > * This alerter delivers alerts using e-mail.
15   *
16   * @author  $Author$
17   * @version $Id$
# Line 25 | Line 25 | public class EMail__Alerter implements PluginAlerter {
25       */
26      public final String REVISION = "$Revision$";
27      
28 <    public final String DESC = "Sends alerts by e-mail";
28 >    public final String DESC = "Sends alerts over e-mail.";
29      
30 +    public final String DEFAULT_LEVEL = Alert.alertLevels[0];
31 +    
32 +    public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
33 +    
34   //---STATIC METHODS---
35  
36   //---CONSTRUCTORS---
37  
38 <    public EMail__Alerter() {
35 <        // get the configuration for this alerter
36 <        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");
51 <        
38 >    public EMail__Alerter() {      
39          _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
40      }
41  
42   //---PUBLIC METHODS---
43  
44      public void sendAlert(Alert alert) {
45 +        ConfigurationProxy cp = ConfigurationProxy.getInstance();
46 +        String levelName;
47 +        try {
48 +            levelName = cp.getProperty(_name, "Alerter.EMail.level");
49 +        } catch (PropertyNotFoundException e) {
50 +            levelName = DEFAULT_LEVEL;
51 +            _logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName);
52 +        }
53 +        int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
54          // only send if it's equal (or above) our level
55 <        if(alert.getLevel() >= _level) {
55 >        if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
56 >            String alertType = Alert.alertLevels[alert.getLevel()];
57 >            String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
58              // sort out the subject
59 <            String subject = _subject;
60 <            subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel()));
59 >            String subject;
60 >            try {
61 >                subject = cp.getProperty(_name, "Alerter.EMail.subject");
62 >            } catch (PropertyNotFoundException e) {
63 >                subject = NOT_CONFIGURED;
64 >                _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
65 >            }
66 >            subject = StringUtils.replaceText(subject, "%level%", alertType);
67 >            subject = StringUtils.replaceText(subject, "%threshold%", thresholdType);
68              subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
69              subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
70              subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
71              subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
72 <            
72 >            subject = StringUtils.replaceText(subject, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
73 >                        
74              // sort out the message body
75 <            String message = _message;
76 <            message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel()));
75 >            String message;
76 >            try {
77 >                message = cp.getProperty(_name, "Alerter.EMail.message");
78 >            } catch (PropertyNotFoundException e) {
79 >                message = NOT_CONFIGURED;
80 >                _logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message);
81 >            }
82 >            message = StringUtils.replaceText(message, "%level%", alertType);
83 >            message = StringUtils.replaceText(message, "%threshold%", thresholdType);
84              message = StringUtils.replaceText(message, "%source%", alert.getSource());
85              message = StringUtils.replaceText(message, "%value%", alert.getValue());
86              message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
87              message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
88 <            
88 >            message = StringUtils.replaceText(message, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
89 >                        
90              try {
91                  // create SMTP message
92 <                Smtp smtp = new Smtp(_smtpServer);
92 >                Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer"));
93                  // set our sender
94 <                smtp.setSender(_sender);
94 >                smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
95                  
96 <                // set the to list
97 <                StringTokenizer st = new StringTokenizer(_destList, ";");
96 >                // get the default destination list
97 >                String destList = cp.getProperty(_name, "Alerter.EMail.defaultDestList");
98 >                // check if the source we're alerting about has a specific destination
99 >                String sourceDestList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
100 >                if(sourceDestList != null) {
101 >                    // if there is a source destination list, use it
102 >                    destList = sourceDestList;
103 >                }
104 >                
105 >                // set the to: list
106 >                StringTokenizer st = new StringTokenizer(destList, ";");
107                  while (st.hasMoreTokens()) {
108                      smtp.setTo(st.nextToken());
109                  }
# Line 94 | Line 117 | public class EMail__Alerter implements PluginAlerter {
117                  out.println(message);
118                  smtp.sendMessage();
119                  smtp.close();
120 <                _logger.write(toString(), Logger.DEBUG, "Sending IRC Alert at level"+String.valueOf(alert.getLevel()));
120 >                _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
121              }
122              catch(IOException e) {
123                  _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
124              }
125 +            catch(PropertyNotFoundException e) {
126 +                _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
127 +            }
128          }
129      }
130  
# Line 127 | Line 153 | public class EMail__Alerter implements PluginAlerter {
153  
154   //---PRIVATE METHODS---
155  
156 +    private String getTimeString(long time) {
157 +        String timeString = null;
158 +        if (time >= 60) {
159 +            timeString = (time / 60) + " minutes";
160 +        } else if (time >= 3600) {
161 +            timeString = ((time/60) / 60) + " hours";
162 +        } else {
163 +            timeString = time + " seconds";
164 +        }
165 +        return timeString;
166 +    }
167 +    
168   //---ACCESSOR/MUTATOR METHODS---
169  
170   //---ATTRIBUTES---
133
134    // an integer value
135    private int _level;
171      
137    // a semi-colon seperated list of destination addresses
138    private String _destList;
139    
140    // a single sender address
141    private String _sender;
142    
143    // the fqdn of an smtp server
144    private String _smtpServer;
145    
146    // a subject with the following: %level% and %message%
147    // eg, "i-scream e-mail alert: level %level%, %message%"
148    private String _subject;
149
150    // a message body with the following: %level% and %message%
151    private String _message;
152
172      /**
173       * This is the friendly identifier of the
174       * component this class is running in.
# Line 159 | Line 178 | public class EMail__Alerter implements PluginAlerter {
178       * can be placed here.  This name could also
179       * be changed to null for utility classes.
180       */
181 <    private String _name = ClientMain.NAME;
181 >    private String _name = "EMail Alert";
182  
183      /**
184       * This holds a reference to the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines