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.2
Committed: Thu Mar 1 23:45:21 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.1: +82 -4 lines
Log Message:
Now has basic functionality that one would expect.

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.1 2001/03/01 22:08:35 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.1 $";
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, "%message%", alert.getMessage());
62
63 // sort out the message body
64 String message = _message;
65 message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel()));
66 message = StringUtils.replaceText(message, "%message%", alert.getMessage());
67
68 try {
69 // create SMTP message
70 Smtp smtp = new Smtp(_smtpServer);
71 // set our sender
72 smtp.setSender(_sender);
73
74 // set the to list
75 StringTokenizer st = new StringTokenizer(_destList, ";");
76 while (st.hasMoreTokens()) {
77 smtp.setTo(st.nextToken());
78 }
79
80 // prepare to print the message
81 PrintWriter out = smtp.getOutputStream();
82 out.println("Subject: "+subject);
83 out.println();
84
85 // send the message
86 out.println(message);
87 smtp.sendMessage();
88 smtp.close();
89 }
90 catch(IOException e) {
91 _logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e);
92 }
93 }
94 }
95
96 /**
97 * Overrides the {@link java.lang.Object#toString() Object.toString()}
98 * method to provide clean logging (every class should have this).
99 *
100 * This uses the uk.ac.ukc.iscream.util.NameFormat class
101 * to format the toString()
102 *
103 * @return the name of this class and its CVS revision
104 */
105 public String toString() {
106 return FormatName.getName(
107 _name,
108 getClass().getName(),
109 REVISION);
110 }
111
112 /**
113 * return the String representation of what the filter does
114 */
115 public String getDescription(){
116 return DESC;
117 }
118
119 //---PRIVATE METHODS---
120
121 //---ACCESSOR/MUTATOR METHODS---
122
123 //---ATTRIBUTES---
124
125 // an integer value
126 private int _level;
127
128 // a semi-colon seperated list of destination addresses
129 private String _destList;
130
131 // a single sender address
132 private String _sender;
133
134 // the fqdn of an smtp server
135 private String _smtpServer;
136
137 // a subject with the following: %level% and %message%
138 // eg, "i-scream e-mail alert: level %level%, %message%"
139 private String _subject;
140
141 // a message body with the following: %level% and %message%
142 private String _message;
143
144 /**
145 * This is the friendly identifier of the
146 * component this class is running in.
147 * eg, a Filter may be called "filter1",
148 * If this class does not have an owning
149 * component, a name from the configuration
150 * can be placed here. This name could also
151 * be changed to null for utility classes.
152 */
153 private String _name = ClientMain.NAME;
154
155 /**
156 * This holds a reference to the
157 * system logger that is being used.
158 */
159 private Logger _logger = ReferenceManager.getInstance().getLogger();
160
161 //---STATIC ATTRIBUTES---
162
163 }