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.4
Committed: Fri Mar 2 00:37:03 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.3: +4 -2 lines
Log Message:
Added the source to the appropriate places.

File Contents

# Content
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 import java.util.*;
11 import java.io.*;
12
13 /**
14 * This Alert sends by e-mail.
15 *
16 * @author $Author: tdb1 $
17 * @version $Id: EMail__Alerter.java,v 1.3 2001/03/02 00:14:12 tdb1 Exp $
18 */
19 public class EMail__Alerter implements PluginAlerter {
20
21 //---FINAL ATTRIBUTES---
22
23 /**
24 * The current CVS revision of this class
25 */
26 public final String REVISION = "$Revision: 1.3 $";
27
28 public final String DESC = "Sends alerts by e-mail";
29
30 //---STATIC METHODS---
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
53 //---PUBLIC METHODS---
54
55 public void sendAlert(Alert alert) {
56 // only send if it's equal (or above) our level
57 if(alert.getLevel() >= _level) {
58 // sort out the subject
59 String subject = _subject;
60 subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel()));
61 subject = StringUtils.replaceText(subject, "%source%", alert.getSource());
62 subject = StringUtils.replaceText(subject, "%value%", alert.getValue());
63 subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue());
64 subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName());
65
66 // sort out the message body
67 String message = _message;
68 message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel()));
69 message = StringUtils.replaceText(message, "%source%", alert.getSource());
70 message = StringUtils.replaceText(message, "%value%", alert.getValue());
71 message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
72 message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
73
74 try {
75 // create SMTP message
76 Smtp smtp = new Smtp(_smtpServer);
77 // set our sender
78 smtp.setSender(_sender);
79
80 // set the to list
81 StringTokenizer st = new StringTokenizer(_destList, ";");
82 while (st.hasMoreTokens()) {
83 smtp.setTo(st.nextToken());
84 }
85
86 // prepare to print the message
87 PrintWriter out = smtp.getOutputStream();
88 out.println("Subject: "+subject);
89 out.println();
90
91 // send the message
92 out.println(message);
93 smtp.sendMessage();
94 smtp.close();
95 }
96 catch(IOException e) {
97 _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
98 }
99 }
100 }
101
102 /**
103 * Overrides the {@link java.lang.Object#toString() Object.toString()}
104 * method to provide clean logging (every class should have this).
105 *
106 * This uses the uk.ac.ukc.iscream.util.NameFormat class
107 * to format the toString()
108 *
109 * @return the name of this class and its CVS revision
110 */
111 public String toString() {
112 return FormatName.getName(
113 _name,
114 getClass().getName(),
115 REVISION);
116 }
117
118 /**
119 * return the String representation of what the filter does
120 */
121 public String getDescription(){
122 return DESC;
123 }
124
125 //---PRIVATE METHODS---
126
127 //---ACCESSOR/MUTATOR METHODS---
128
129 //---ATTRIBUTES---
130
131 // an integer value
132 private int _level;
133
134 // a semi-colon seperated list of destination addresses
135 private String _destList;
136
137 // a single sender address
138 private String _sender;
139
140 // the fqdn of an smtp server
141 private String _smtpServer;
142
143 // a subject with the following: %level% and %message%
144 // eg, "i-scream e-mail alert: level %level%, %message%"
145 private String _subject;
146
147 // a message body with the following: %level% and %message%
148 private String _message;
149
150 /**
151 * This is the friendly identifier of the
152 * component this class is running in.
153 * eg, a Filter may be called "filter1",
154 * If this class does not have an owning
155 * component, a name from the configuration
156 * can be placed here. This name could also
157 * be changed to null for utility classes.
158 */
159 private String _name = ClientMain.NAME;
160
161 /**
162 * This holds a reference to the
163 * system logger that is being used.
164 */
165 private Logger _logger = ReferenceManager.getInstance().getLogger();
166
167 //---STATIC ATTRIBUTES---
168
169 }