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.1 by tdb, Thu Mar 1 22:08:35 2001 UTC vs.
Revision 1.6 by tdb, Fri Mar 2 01:32:09 2001 UTC

# Line 7 | Line 7 | 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.*;
11 + import java.io.*;
12 +
13   /**
14   * This Alert sends by e-mail.
15   *
16   * @author  $Author$
17   * @version $Id$
18   */
19 < public class EMail__PluginAlerter implements PluginAlerter {
19 > public class EMail__Alerter implements PluginAlerter {
20  
21   //---FINAL ATTRIBUTES---
22  
# Line 28 | Line 31 | public class EMail__PluginAlerter implements PluginAle
31  
32   //---CONSTRUCTORS---
33  
34 +    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 +        
52 +        _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
53 +    }
54 +
55   //---PUBLIC METHODS---
56  
57      public void sendAlert(Alert alert) {
58 <        // yeah, now what ?
58 >        // only send if it's equal (or above) our level
59 >        if(alert.getLevel() >= _level) {
60 >            // sort out the subject
61 >            String subject = _subject;
62 >            subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel()));
63 >            subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
64 >            subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
65 >            subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
66 >            subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
67 >            
68 >            // sort out the message body
69 >            String message = _message;
70 >            message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel()));
71 >            message = StringUtils.replaceText(message, "%source%", alert.getSource());
72 >            message = StringUtils.replaceText(message, "%value%", alert.getValue());
73 >            message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
74 >            message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
75 >            
76 >            try {
77 >                // create SMTP message
78 >                Smtp smtp = new Smtp(_smtpServer);
79 >                // set our sender
80 >                smtp.setSender(_sender);
81 >                
82 >                // set the to list
83 >                StringTokenizer st = new StringTokenizer(_destList, ";");
84 >                while (st.hasMoreTokens()) {
85 >                    smtp.setTo(st.nextToken());
86 >                }
87 >    
88 >                // prepare to print the message            
89 >                PrintWriter out = smtp.getOutputStream();
90 >                out.println("Subject: "+subject);
91 >                out.println();
92 >                
93 >                // send the message
94 >                out.println(message);
95 >                smtp.sendMessage();
96 >                smtp.close();
97 >                _logger.write(toString(), Logger.DEBUG, "Sending IRC Alert at level"+String.valueOf(alert.getLevel()));
98 >            }
99 >            catch(IOException e) {
100 >                _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
101 >            }
102 >        }
103      }
104  
105      /**
# Line 62 | Line 130 | public class EMail__PluginAlerter implements PluginAle
130   //---ACCESSOR/MUTATOR METHODS---
131  
132   //---ATTRIBUTES---
133 +
134 +    // an integer value
135 +    private int _level;
136 +    
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  
153      /**
154       * This is the friendly identifier of the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines