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.util.StringTokenizer; |
11 |
|
|
12 |
+ |
|
13 |
|
/** |
14 |
< |
* This Alert sends by e-mail. |
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 |
|
|
25 |
|
*/ |
26 |
|
public final String REVISION = "$Revision$"; |
27 |
|
|
28 |
< |
public final String DESC = "Sends alerts by e-mail"; |
28 |
> |
public final String DESC = "Sends alerts over e-mail."; |
29 |
|
|
30 |
+ |
public final String NOT_CONFIGURED = "<NOT CONFIGURED>"; |
31 |
+ |
|
32 |
|
//---STATIC METHODS--- |
33 |
|
|
34 |
|
//---CONSTRUCTORS--- |
35 |
|
|
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 |
– |
|
36 |
|
//---PUBLIC METHODS--- |
37 |
|
|
38 |
|
public void sendAlert(Alert alert) { |
39 |
< |
// only send if it's equal (or above) our level |
40 |
< |
if(alert.getLevel() >= _level) { |
41 |
< |
// sort out the subject |
42 |
< |
String subject = _subject; |
43 |
< |
subject = StringUtils.replaceText(subject, "%level%", String.valueOf(alert.getLevel())); |
44 |
< |
subject = StringUtils.replaceText(subject, "%message%", alert.getMessage()); |
39 |
> |
// get the subject and replace fields |
40 |
> |
String subject; |
41 |
> |
try { |
42 |
> |
subject = _cp.getProperty(_name, "Alerter.EMail.subject"); |
43 |
> |
} catch (PropertyNotFoundException e) { |
44 |
> |
subject = NOT_CONFIGURED; |
45 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
46 |
> |
} |
47 |
> |
subject = processAlertMessage(subject, alert); |
48 |
> |
|
49 |
> |
// get the message body and replace fields |
50 |
> |
String message; |
51 |
> |
try { |
52 |
> |
message = _cp.getProperty(_name, "Alerter.EMail.message"); |
53 |
> |
} catch (PropertyNotFoundException e) { |
54 |
> |
message = NOT_CONFIGURED; |
55 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
56 |
> |
} |
57 |
> |
message = processAlertMessage(message, alert); |
58 |
> |
|
59 |
> |
// attempt to send the actual message |
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 |
< |
// sort out the message body |
67 |
< |
String message = _message; |
65 |
< |
message = StringUtils.replaceText(message, "%level%", String.valueOf(alert.getLevel())); |
66 |
< |
message = StringUtils.replaceText(message, "%message%", alert.getMessage()); |
66 |
> |
// list of destination addresses |
67 |
> |
String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
68 |
|
|
69 |
< |
try { |
70 |
< |
// create SMTP message |
71 |
< |
Smtp smtp = new Smtp(_smtpServer); |
72 |
< |
// 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(); |
69 |
> |
// set the to: list |
70 |
> |
StringTokenizer st = new StringTokenizer(destList, ";"); |
71 |
> |
while (st.hasMoreTokens()) { |
72 |
> |
smtp.setTo(st.nextToken()); |
73 |
|
} |
74 |
< |
catch(IOException e) { |
75 |
< |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
76 |
< |
} |
74 |
> |
|
75 |
> |
// prepare to print the message |
76 |
> |
PrintWriter out = smtp.getOutputStream(); |
77 |
> |
out.println("Subject: "+subject+"\n"); |
78 |
> |
|
79 |
> |
// send the message |
80 |
> |
out.println(message); |
81 |
> |
smtp.sendMessage(); |
82 |
> |
smtp.close(); |
83 |
> |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ Alert.alertLevels[alert.getLevel()] + " 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 |
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--- |
117 |
< |
|
117 |
> |
|
118 |
|
//---ACCESSOR/MUTATOR METHODS--- |
119 |
|
|
120 |
|
//---ATTRIBUTES--- |
124 |
– |
|
125 |
– |
// an integer value |
126 |
– |
private int _level; |
121 |
|
|
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 |
– |
|
122 |
|
/** |
123 |
|
* This is the friendly identifier of the |
124 |
|
* component this class is running in. |
128 |
|
* can be placed here. This name could also |
129 |
|
* be changed to null for utility classes. |
130 |
|
*/ |
131 |
< |
private String _name = ClientMain.NAME; |
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 |
|
} |