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.15 by ajm, Tue Mar 6 02:33:55 2001 UTC vs.
Revision 1.20 by ajm, Thu Mar 22 23:35:22 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.*;
9 <
10 < import java.util.*;
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.io.*;
10 < import java.text.*;
10 > import java.util.StringTokenizer;
11  
12 +
13   /**
14   * This alerter delivers alerts using e-mail.
15   *
16   * @author  $Author$
17   * @version $Id$
18   */
19 < public class EMail__Alerter implements PluginAlerter {
19 > public class EMail__Alerter extends AlerterSkeleton {
20  
21   //---FINAL ATTRIBUTES---
22  
# Line 28 | Line 27 | public class EMail__Alerter implements PluginAlerter {
27      
28      public final String DESC = "Sends alerts over e-mail.";
29      
31    public final String DEFAULT_LEVEL = Alert.alertLevels[0];
32    
30      public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
31      
32   //---STATIC METHODS---
33  
34   //---CONSTRUCTORS---
35  
39    public EMail__Alerter() {      
40        _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
41    }
42
36   //---PUBLIC METHODS---
37  
38      public void sendAlert(Alert alert) {
39 <        ConfigurationProxy cp = ConfigurationProxy.getInstance();
40 <        String levelName;
39 >        String alertType = Alert.alertLevels[alert.getLevel()];
40 >        // sort out the subject
41 >        String subject;
42          try {
43 <            levelName = cp.getProperty(_name, "Alerter.EMail.level");
43 >            subject = _cp.getProperty(_name, "Alerter.EMail.subject");
44          } catch (PropertyNotFoundException e) {
45 <            levelName = DEFAULT_LEVEL;
46 <            _logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName);
45 >            subject = NOT_CONFIGURED;
46 >            _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
47          }
48 <        int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
49 <        // only send if it's equal (or above) our level
50 <        if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
51 <            String alertType = Alert.alertLevels[alert.getLevel()];
52 <            String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
53 <            String timeFirstSince = DateUtils.formatTime(System.currentTimeMillis() - alert.getInitialAlertTime(), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
54 <            String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
55 <
56 <            // sort out the subject
57 <            String subject;
58 <            try {
59 <                subject = cp.getProperty(_name, "Alerter.EMail.subject");
60 <            } catch (PropertyNotFoundException e) {
61 <                subject = NOT_CONFIGURED;
62 <                _logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject);
48 >        subject = processAlertMessage(subject, alert);
49 >                    
50 >        // sort out the message body
51 >        String message;
52 >        try {
53 >            message = _cp.getProperty(_name, "Alerter.EMail.message");
54 >        } catch (PropertyNotFoundException e) {
55 >            message = NOT_CONFIGURED;
56 >            _logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message);
57 >        }
58 >        message = processAlertMessage(message, alert);
59 >                    
60 >        try {
61 >            // create SMTP message
62 >            Smtp smtp = new Smtp(_cp.getProperty(_name, "Alerter.EMail.smtpServer"));
63 >            // set our sender
64 >            smtp.setSender(_cp.getProperty(_name, "Alerter.EMail.sender"));
65 >            
66 >            String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
67 >            
68 >            // set the to: list
69 >            StringTokenizer st = new StringTokenizer(destList, ";");
70 >            while (st.hasMoreTokens()) {
71 >                smtp.setTo(st.nextToken());
72              }
73 <            subject = StringUtils.replaceText(subject, "%level%", alertType);
74 <            subject = StringUtils.replaceText(subject, "%threshold%", thresholdType);
75 <            subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
76 <            subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
77 <            subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
78 <            subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
79 <            subject = StringUtils.replaceText(subject, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
80 <            subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince);
81 <            subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured);
82 <                        
83 <            // sort out the message body
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())));
95 <            message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
96 <            message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
97 <                        
98 <            try {
99 <                // create SMTP message
100 <                Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer"));
101 <                // set our sender
102 <                smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender"));
103 <                
104 <                // get the default destination list
105 <                String destList = cp.getProperty(_name, "Alerter.EMail.defaultDestList");
106 <                // check if the source we're alerting about has a specific destination
107 <                String sourceDestList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList");
108 <                if(sourceDestList != null) {
109 <                    // if there is a source destination list, use it
110 <                    destList = sourceDestList;
111 <                }
112 <                
113 <                // set the to: list
114 <                StringTokenizer st = new StringTokenizer(destList, ";");
115 <                while (st.hasMoreTokens()) {
116 <                    smtp.setTo(st.nextToken());
117 <                }
118 <    
119 <                // prepare to print the message            
120 <                PrintWriter out = smtp.getOutputStream();
121 <                out.println("Subject: "+subject);
122 <                out.println();
123 <                
124 <                // send the message
125 <                out.println(message);
126 <                smtp.sendMessage();
127 <                smtp.close();
128 <                _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
129 <            }
130 <            catch(IOException e) {
131 <                _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
132 <            }
133 <            catch(PropertyNotFoundException e) {
134 <                _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
135 <            }
73 >
74 >            // prepare to print the message            
75 >            PrintWriter out = smtp.getOutputStream();
76 >            out.println("Subject: "+subject);
77 >            out.println();
78 >            
79 >            // send the message
80 >            out.println(message);
81 >            smtp.sendMessage();
82 >            smtp.close();
83 >            _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
84          }
85 +        catch(IOException e) {
86 +            _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
87 +        }
88 +        catch(PropertyNotFoundException e) {
89 +            _logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e);
90 +        }
91      }
92  
93      /**
94       * Overrides the {@link java.lang.Object#toString() Object.toString()}
95       * method to provide clean logging (every class should have this).
96       *
97 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
97 >     * This uses the uk.org.iscream.util.NameFormat class
98       * to format the toString()
99       *
100       * @return the name of this class and its CVS revision
# Line 153 | Line 107 | public class EMail__Alerter implements PluginAlerter {
107      }
108  
109      /**
110 <     * return the String representation of what the filter does
110 >     * return the String representation of what the alerter does
111       */
112      public String getDescription(){
113          return DESC;
114      }
115  
116   //---PRIVATE METHODS---
163
164    private String getTimeString(long time) {
165        String timeString = null;
166        if (time >= 60) {
167            timeString = (time / 60) + " minutes";
168        } else if (time >= 3600) {
169            timeString = ((time/60) / 60) + " hours";
170        } else {
171            timeString = time + " seconds";
172        }
173        return timeString;
174    }
117      
118   //---ACCESSOR/MUTATOR METHODS---
119  
# Line 186 | Line 128 | public class EMail__Alerter implements PluginAlerter {
128       * can be placed here.  This name could also
129       * be changed to null for utility classes.
130       */
131 <    private String _name = "EMail Alert";
131 >    protected String _name = "EMail";
132  
133      /**
134       * This holds a reference to the
135       * system logger that is being used.
136       */
137      private Logger _logger = ReferenceManager.getInstance().getLogger();
138 <
138 >    
139   //---STATIC ATTRIBUTES---
140  
141   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines