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
Revision: 1.5
Committed: Fri Mar 2 01:29:22 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.4: +5 -2 lines
Log Message:
Added some debugging information.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2     package uk.ac.ukc.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 tdb 1.2 import java.util.*;
11     import java.io.*;
12    
13 tdb 1.1 /**
14     * This Alert sends by e-mail.
15     *
16 tdb 1.2 * @author $Author: tdb1 $
17 tdb 1.5 * @version $Id: EMail__Alerter.java,v 1.4 2001/03/02 00:37:03 tdb1 Exp $
18 tdb 1.1 */
19 tdb 1.2 public class EMail__Alerter implements PluginAlerter {
20 tdb 1.1
21     //---FINAL ATTRIBUTES---
22    
23     /**
24     * The current CVS revision of this class
25     */
26 tdb 1.5 public final String REVISION = "$Revision: 1.4 $";
27 tdb 1.1
28     public final String DESC = "Sends alerts by e-mail";
29    
30     //---STATIC METHODS---
31    
32     //---CONSTRUCTORS---
33    
34 tdb 1.2 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 tdb 1.5
52     _logger.write(toString(), Logger.SYSINT, "IRC Alerter started");
53 tdb 1.2 }
54    
55 tdb 1.1 //---PUBLIC METHODS---
56    
57     public void sendAlert(Alert alert) {
58 tdb 1.2 // 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 tdb 1.4 subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
64 tdb 1.3 subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
65     subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
66     subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
67 tdb 1.2
68     // sort out the message body
69     String message = _message;
70     message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel()));
71 tdb 1.4 message = StringUtils.replaceText(message, "%source%", alert.getSource());
72 tdb 1.3 message = StringUtils.replaceText(message, "%value%", alert.getValue());
73     message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
74     message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
75 tdb 1.2
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 tdb 1.5 _logger.write(toString(), Logger.DEBUG, "Sending IRC Alert at level"+String.valueOf(alert.getLevel()));
98 tdb 1.2 }
99     catch(IOException e) {
100     _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
101     }
102     }
103 tdb 1.1 }
104    
105     /**
106     * Overrides the {@link java.lang.Object#toString() Object.toString()}
107     * method to provide clean logging (every class should have this).
108     *
109     * This uses the uk.ac.ukc.iscream.util.NameFormat class
110     * to format the toString()
111     *
112     * @return the name of this class and its CVS revision
113     */
114     public String toString() {
115     return FormatName.getName(
116     _name,
117     getClass().getName(),
118     REVISION);
119     }
120    
121     /**
122     * return the String representation of what the filter does
123     */
124     public String getDescription(){
125     return DESC;
126     }
127    
128     //---PRIVATE METHODS---
129    
130     //---ACCESSOR/MUTATOR METHODS---
131    
132     //---ATTRIBUTES---
133 tdb 1.2
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 tdb 1.1
153     /**
154     * This is the friendly identifier of the
155     * component this class is running in.
156     * eg, a Filter may be called "filter1",
157     * If this class does not have an owning
158     * component, a name from the configuration
159     * can be placed here. This name could also
160     * be changed to null for utility classes.
161     */
162     private String _name = ClientMain.NAME;
163    
164     /**
165     * This holds a reference to the
166     * system logger that is being used.
167     */
168     private Logger _logger = ReferenceManager.getInstance().getLogger();
169    
170     //---STATIC ATTRIBUTES---
171    
172     }