1 |
tdb |
1.25 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
|
|
* Copyright (C) 2000-2002 i-scream |
4 |
|
|
* |
5 |
|
|
* This program is free software; you can redistribute it and/or |
6 |
|
|
* modify it under the terms of the GNU General Public License |
7 |
|
|
* as published by the Free Software Foundation; either version 2 |
8 |
|
|
* of the License, or (at your option) any later version. |
9 |
|
|
* |
10 |
|
|
* This program is distributed in the hope that it will be useful, |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
* GNU General Public License for more details. |
14 |
|
|
* |
15 |
|
|
* You should have received a copy of the GNU General Public License |
16 |
|
|
* along with this program; if not, write to the Free Software |
17 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
21 |
tdb |
1.24 |
package uk.org.iscream.cms.server.client.alerters; |
22 |
tdb |
1.1 |
|
23 |
|
|
//---IMPORTS--- |
24 |
tdb |
1.24 |
import uk.org.iscream.cms.server.client.*; |
25 |
|
|
import uk.org.iscream.cms.server.core.*; |
26 |
|
|
import uk.org.iscream.cms.server.util.*; |
27 |
|
|
import uk.org.iscream.cms.server.componentmanager.*; |
28 |
ajm |
1.20 |
import java.io.*; |
29 |
|
|
import java.util.StringTokenizer; |
30 |
tdb |
1.1 |
|
31 |
tdb |
1.2 |
|
32 |
tdb |
1.1 |
/** |
33 |
tdb |
1.8 |
* This alerter delivers alerts using e-mail. |
34 |
tdb |
1.1 |
* |
35 |
tdb |
1.25 |
* @author $Author: tdb $ |
36 |
|
|
* @version $Id: EMail__Alerter.java,v 1.24 2001/05/29 17:02:34 tdb Exp $ |
37 |
tdb |
1.1 |
*/ |
38 |
ajm |
1.20 |
public class EMail__Alerter extends AlerterSkeleton { |
39 |
tdb |
1.1 |
|
40 |
|
|
//---FINAL ATTRIBUTES--- |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* The current CVS revision of this class |
44 |
|
|
*/ |
45 |
tdb |
1.25 |
public final String REVISION = "$Revision: 1.24 $"; |
46 |
tdb |
1.1 |
|
47 |
ajm |
1.22 |
/** |
48 |
|
|
* A description of this alerter |
49 |
|
|
*/ |
50 |
tdb |
1.8 |
public final String DESC = "Sends alerts over e-mail."; |
51 |
tdb |
1.1 |
|
52 |
|
|
//---STATIC METHODS--- |
53 |
|
|
|
54 |
|
|
//---CONSTRUCTORS--- |
55 |
|
|
|
56 |
|
|
//---PUBLIC METHODS--- |
57 |
|
|
|
58 |
ajm |
1.22 |
/** |
59 |
|
|
* Implements the abstract method from the skeleton class. |
60 |
|
|
* This method will attempt to send an alert |
61 |
|
|
* message using the configured email configuration. |
62 |
|
|
* |
63 |
|
|
* @param alert the alert to send |
64 |
|
|
*/ |
65 |
tdb |
1.1 |
public void sendAlert(Alert alert) { |
66 |
tdb |
1.21 |
// get the subject and replace fields |
67 |
ajm |
1.20 |
String subject; |
68 |
tdb |
1.14 |
try { |
69 |
ajm |
1.20 |
subject = _cp.getProperty(_name, "Alerter.EMail.subject"); |
70 |
tdb |
1.14 |
} catch (PropertyNotFoundException e) { |
71 |
ajm |
1.20 |
subject = NOT_CONFIGURED; |
72 |
|
|
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
73 |
tdb |
1.14 |
} |
74 |
ajm |
1.20 |
subject = processAlertMessage(subject, alert); |
75 |
|
|
|
76 |
tdb |
1.21 |
// get the message body and replace fields |
77 |
ajm |
1.20 |
String message; |
78 |
|
|
try { |
79 |
|
|
message = _cp.getProperty(_name, "Alerter.EMail.message"); |
80 |
|
|
} catch (PropertyNotFoundException e) { |
81 |
|
|
message = NOT_CONFIGURED; |
82 |
|
|
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.message value unavailable using default of " + message); |
83 |
|
|
} |
84 |
|
|
message = processAlertMessage(message, alert); |
85 |
tdb |
1.21 |
|
86 |
|
|
// attempt to send the actual message |
87 |
ajm |
1.20 |
try { |
88 |
|
|
// create SMTP message |
89 |
|
|
Smtp smtp = new Smtp(_cp.getProperty(_name, "Alerter.EMail.smtpServer")); |
90 |
|
|
// set our sender |
91 |
|
|
smtp.setSender(_cp.getProperty(_name, "Alerter.EMail.sender")); |
92 |
|
|
|
93 |
tdb |
1.21 |
// list of destination addresses |
94 |
ajm |
1.20 |
String destList = _cp.getProperty("Host."+alert.getSource(), "Alerter.EMail.destList"); |
95 |
|
|
|
96 |
|
|
// set the to: list |
97 |
|
|
StringTokenizer st = new StringTokenizer(destList, ";"); |
98 |
|
|
while (st.hasMoreTokens()) { |
99 |
|
|
smtp.setTo(st.nextToken()); |
100 |
tdb |
1.2 |
} |
101 |
ajm |
1.20 |
|
102 |
|
|
// prepare to print the message |
103 |
|
|
PrintWriter out = smtp.getOutputStream(); |
104 |
tdb |
1.21 |
out.println("Subject: "+subject+"\n"); |
105 |
ajm |
1.20 |
|
106 |
|
|
// send the message |
107 |
|
|
out.println(message); |
108 |
|
|
smtp.sendMessage(); |
109 |
|
|
smtp.close(); |
110 |
tdb |
1.21 |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ Alert.alertLevels[alert.getLevel()] + " level"); |
111 |
ajm |
1.20 |
} |
112 |
|
|
catch(IOException e) { |
113 |
|
|
_logger.write(toString(), Logger.ERROR, "Error whilst sending message: "+e); |
114 |
|
|
} |
115 |
|
|
catch(PropertyNotFoundException e) { |
116 |
|
|
_logger.write(toString(), Logger.ERROR, "Error obtaining essential configuration: "+e); |
117 |
tdb |
1.2 |
} |
118 |
tdb |
1.1 |
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
122 |
|
|
* method to provide clean logging (every class should have this). |
123 |
|
|
* |
124 |
tdb |
1.24 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
125 |
tdb |
1.1 |
* to format the toString() |
126 |
|
|
* |
127 |
|
|
* @return the name of this class and its CVS revision |
128 |
|
|
*/ |
129 |
|
|
public String toString() { |
130 |
|
|
return FormatName.getName( |
131 |
|
|
_name, |
132 |
|
|
getClass().getName(), |
133 |
|
|
REVISION); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
ajm |
1.22 |
* Return the String representation of what the alerter does |
138 |
|
|
* |
139 |
|
|
* @return the description |
140 |
tdb |
1.1 |
*/ |
141 |
|
|
public String getDescription(){ |
142 |
|
|
return DESC; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
//---PRIVATE METHODS--- |
146 |
ajm |
1.9 |
|
147 |
tdb |
1.1 |
//---ACCESSOR/MUTATOR METHODS--- |
148 |
tdb |
1.23 |
|
149 |
|
|
/** |
150 |
|
|
* Returns the "friendly" name of this class. This |
151 |
|
|
* is simply an accessor for _name, required due to |
152 |
|
|
* inheritance issues with extending AlerterSkeleton. |
153 |
|
|
* |
154 |
|
|
* @return the friendly name |
155 |
|
|
*/ |
156 |
|
|
protected String getFName() { |
157 |
|
|
return _name; |
158 |
|
|
} |
159 |
tdb |
1.1 |
|
160 |
|
|
//---ATTRIBUTES--- |
161 |
tdb |
1.2 |
|
162 |
tdb |
1.1 |
/** |
163 |
|
|
* This is the friendly identifier of the |
164 |
|
|
* component this class is running in. |
165 |
|
|
* eg, a Filter may be called "filter1", |
166 |
|
|
* If this class does not have an owning |
167 |
|
|
* component, a name from the configuration |
168 |
|
|
* can be placed here. This name could also |
169 |
|
|
* be changed to null for utility classes. |
170 |
|
|
*/ |
171 |
ajm |
1.20 |
protected String _name = "EMail"; |
172 |
ajm |
1.19 |
|
173 |
tdb |
1.1 |
//---STATIC ATTRIBUTES--- |
174 |
|
|
|
175 |
|
|
} |