1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.org.iscream.client.alerters; |
2 |
> |
package uk.org.iscream.cms.server.client.alerters; |
3 |
|
|
4 |
|
//---IMPORTS--- |
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 |
< |
|
10 |
< |
import java.util.*; |
5 |
> |
import uk.org.iscream.cms.server.client.*; |
6 |
> |
import uk.org.iscream.cms.server.core.*; |
7 |
> |
import uk.org.iscream.cms.server.util.*; |
8 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
9 |
|
import java.io.*; |
10 |
< |
import java.text.*; |
10 |
> |
import java.util.StringTokenizer; |
11 |
|
|
12 |
+ |
|
13 |
|
/** |
14 |
|
* This alerter delivers alerts using e-mail. |
15 |
|
* |
16 |
|
* @author $Author$ |
17 |
|
* @version $Id$ |
18 |
|
*/ |
19 |
< |
public class EMail__Alerter extends Thread implements PluginAlerter { |
19 |
> |
public class EMail__Alerter extends AlerterSkeleton { |
20 |
|
|
21 |
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
25 |
|
*/ |
26 |
|
public final String REVISION = "$Revision$"; |
27 |
|
|
28 |
+ |
/** |
29 |
+ |
* A description of this alerter |
30 |
+ |
*/ |
31 |
|
public final String DESC = "Sends alerts over e-mail."; |
32 |
|
|
31 |
– |
public final String DEFAULT_LEVEL = Alert.alertLevels[0]; |
32 |
– |
|
33 |
– |
public final String NOT_CONFIGURED = "<NOT CONFIGURED>"; |
34 |
– |
|
33 |
|
//---STATIC METHODS--- |
34 |
|
|
35 |
|
//---CONSTRUCTORS--- |
36 |
|
|
39 |
– |
public EMail__Alerter() { |
40 |
– |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
41 |
– |
this.start(); |
42 |
– |
} |
43 |
– |
|
37 |
|
//---PUBLIC METHODS--- |
38 |
|
|
39 |
+ |
/** |
40 |
+ |
* Implements the abstract method from the skeleton class. |
41 |
+ |
* This method will attempt to send an alert |
42 |
+ |
* message using the configured email configuration. |
43 |
+ |
* |
44 |
+ |
* @param alert the alert to send |
45 |
+ |
*/ |
46 |
|
public void sendAlert(Alert alert) { |
47 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
48 |
< |
String levelName; |
47 |
> |
// get the subject and replace fields |
48 |
> |
String subject; |
49 |
|
try { |
50 |
< |
levelName = cp.getProperty(_name, "Alerter.EMail.level"); |
50 |
> |
subject = _cp.getProperty(_name, "Alerter.EMail.subject"); |
51 |
|
} catch (PropertyNotFoundException e) { |
52 |
< |
levelName = DEFAULT_LEVEL; |
53 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.level value unavailable using default of " + levelName); |
52 |
> |
subject = NOT_CONFIGURED; |
53 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
54 |
|
} |
55 |
< |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
56 |
< |
// only send if it's equal (or above) our level |
57 |
< |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
58 |
< |
String alertType = Alert.alertLevels[alert.getLevel()]; |
59 |
< |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
60 |
< |
String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
61 |
< |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
62 |
< |
|
63 |
< |
// sort out the subject |
64 |
< |
String subject; |
65 |
< |
try { |
66 |
< |
subject = cp.getProperty(_name, "Alerter.EMail.subject"); |
67 |
< |
} catch (PropertyNotFoundException e) { |
68 |
< |
subject = NOT_CONFIGURED; |
69 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
55 |
> |
subject = processAlertMessage(subject, alert); |
56 |
> |
|
57 |
> |
// get the message body and replace fields |
58 |
> |
String message; |
59 |
> |
try { |
60 |
> |
message = _cp.getProperty(_name, "Alerter.EMail.message"); |
61 |
> |
} catch (PropertyNotFoundException e) { |
62 |
> |
message = NOT_CONFIGURED; |
63 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
64 |
> |
} |
65 |
> |
message = processAlertMessage(message, alert); |
66 |
> |
|
67 |
> |
// attempt to send the actual message |
68 |
> |
try { |
69 |
> |
// create SMTP message |
70 |
> |
Smtp smtp = new Smtp(_cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
71 |
> |
// set our sender |
72 |
> |
smtp.setSender(_cp.getProperty(_name, "Alerter.EMail.sender")); |
73 |
> |
|
74 |
> |
// list of destination addresses |
75 |
> |
String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
76 |
> |
|
77 |
> |
// set the to: list |
78 |
> |
StringTokenizer st = new StringTokenizer(destList, ";"); |
79 |
> |
while (st.hasMoreTokens()) { |
80 |
> |
smtp.setTo(st.nextToken()); |
81 |
|
} |
82 |
< |
subject = StringUtils.replaceText(subject, "%level%", alertType); |
83 |
< |
subject = StringUtils.replaceText(subject, "%threshold%", thresholdType); |
84 |
< |
subject = StringUtils.replaceText(subject, "%source%", alert.getSource()); |
85 |
< |
subject = StringUtils.replaceText(subject, "%value%", alert.getValue()); |
86 |
< |
subject = StringUtils.replaceText(subject, "%thresholdValue%", alert.getThresholdValue()); |
87 |
< |
subject = StringUtils.replaceText(subject, "%attributeName%", alert.getAttributeName()); |
88 |
< |
subject = StringUtils.replaceText(subject, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
89 |
< |
subject = StringUtils.replaceText(subject, "%timeSinceFirstAlert%", timeFirstSince); |
90 |
< |
subject = StringUtils.replaceText(subject, "%timeOfFirstAlert%", timeFirstOccured); |
91 |
< |
|
81 |
< |
// sort out the message body |
82 |
< |
String message; |
83 |
< |
try { |
84 |
< |
message = cp.getProperty(_name, "Alerter.EMail.message"); |
85 |
< |
} catch (PropertyNotFoundException e) { |
86 |
< |
message = NOT_CONFIGURED; |
87 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
88 |
< |
} |
89 |
< |
message = StringUtils.replaceText(message, "%level%", alertType); |
90 |
< |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
91 |
< |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
92 |
< |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
93 |
< |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
94 |
< |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
95 |
< |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
96 |
< |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
97 |
< |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
98 |
< |
|
99 |
< |
try { |
100 |
< |
// create SMTP message |
101 |
< |
Smtp smtp = new Smtp(cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
102 |
< |
// set our sender |
103 |
< |
smtp.setSender(cp.getProperty(_name, "Alerter.EMail.sender")); |
104 |
< |
|
105 |
< |
String destList = cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
106 |
< |
|
107 |
< |
// set the to: list |
108 |
< |
StringTokenizer st = new StringTokenizer(destList, ";"); |
109 |
< |
while (st.hasMoreTokens()) { |
110 |
< |
smtp.setTo(st.nextToken()); |
111 |
< |
} |
112 |
< |
|
113 |
< |
// prepare to print the message |
114 |
< |
PrintWriter out = smtp.getOutputStream(); |
115 |
< |
out.println("Subject: "+subject); |
116 |
< |
out.println(); |
117 |
< |
|
118 |
< |
// send the message |
119 |
< |
out.println(message); |
120 |
< |
smtp.sendMessage(); |
121 |
< |
smtp.close(); |
122 |
< |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
123 |
< |
} |
124 |
< |
catch(IOException e) { |
125 |
< |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
126 |
< |
} |
127 |
< |
catch(PropertyNotFoundException e) { |
128 |
< |
_logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e); |
129 |
< |
} |
82 |
> |
|
83 |
> |
// prepare to print the message |
84 |
> |
PrintWriter out = smtp.getOutputStream(); |
85 |
> |
out.println("Subject: "+subject+"\n"); |
86 |
> |
|
87 |
> |
// send the message |
88 |
> |
out.println(message); |
89 |
> |
smtp.sendMessage(); |
90 |
> |
smtp.close(); |
91 |
> |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ Alert.alertLevels[alert.getLevel()] + " level"); |
92 |
|
} |
93 |
+ |
catch(IOException e) { |
94 |
+ |
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
95 |
+ |
} |
96 |
+ |
catch(PropertyNotFoundException e) { |
97 |
+ |
_logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e); |
98 |
+ |
} |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
103 |
|
* method to provide clean logging (every class should have this). |
104 |
|
* |
105 |
< |
* This uses the uk.org.iscream.util.NameFormat class |
105 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
106 |
|
* to format the toString() |
107 |
|
* |
108 |
|
* @return the name of this class and its CVS revision |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* return the String representation of what the filter does |
118 |
> |
* Return the String representation of what the alerter does |
119 |
> |
* |
120 |
> |
* @return the description |
121 |
|
*/ |
122 |
|
public String getDescription(){ |
123 |
|
return DESC; |
126 |
|
//---PRIVATE METHODS--- |
127 |
|
|
128 |
|
//---ACCESSOR/MUTATOR METHODS--- |
159 |
– |
|
160 |
– |
protected Queue getQueue() { |
161 |
– |
return AlerterManager.getInstance().getQueue(); |
162 |
– |
} |
129 |
|
|
130 |
< |
protected int getQueueId() { |
131 |
< |
if (_qID == -1) { |
132 |
< |
_qID = getQueue().getQueue(); |
133 |
< |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
134 |
< |
} |
135 |
< |
return _qID; |
130 |
> |
/** |
131 |
> |
* Returns the "friendly" name of this class. This |
132 |
> |
* is simply an accessor for _name, required due to |
133 |
> |
* inheritance issues with extending AlerterSkeleton. |
134 |
> |
* |
135 |
> |
* @return the friendly name |
136 |
> |
*/ |
137 |
> |
protected String getFName() { |
138 |
> |
return _name; |
139 |
|
} |
140 |
|
|
141 |
|
//---ATTRIBUTES--- |
149 |
|
* can be placed here. This name could also |
150 |
|
* be changed to null for utility classes. |
151 |
|
*/ |
152 |
< |
private String _name = "EMail Alert"; |
184 |
< |
|
185 |
< |
/** |
186 |
< |
* This holds a reference to the |
187 |
< |
* system logger that is being used. |
188 |
< |
*/ |
189 |
< |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
152 |
> |
protected String _name = "EMail"; |
153 |
|
|
191 |
– |
/** |
192 |
– |
* The queue id for this alerters queue in the alert queue |
193 |
– |
*/ |
194 |
– |
private int _qID = -1; |
195 |
– |
|
196 |
– |
/** |
197 |
– |
* The running status of the alerter |
198 |
– |
*/ |
199 |
– |
private boolean _running = true; |
200 |
– |
|
154 |
|
//---STATIC ATTRIBUTES--- |
155 |
|
|
156 |
|
} |