1 |
/* |
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 |
//---PACKAGE DECLARATION--- |
21 |
package uk.org.iscream.cms.server.client.alerters; |
22 |
|
23 |
//---IMPORTS--- |
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 |
import java.io.*; |
29 |
import java.util.StringTokenizer; |
30 |
|
31 |
|
32 |
/** |
33 |
* This alerter delivers alerts using e-mail. |
34 |
* |
35 |
* @author $Author: tdb $ |
36 |
* @version $Id: EMail__Alerter.java,v 1.24 2001/05/29 17:02:34 tdb Exp $ |
37 |
*/ |
38 |
public class EMail__Alerter extends AlerterSkeleton { |
39 |
|
40 |
//---FINAL ATTRIBUTES--- |
41 |
|
42 |
/** |
43 |
* The current CVS revision of this class |
44 |
*/ |
45 |
public final String REVISION = "$Revision: 1.24 $"; |
46 |
|
47 |
/** |
48 |
* A description of this alerter |
49 |
*/ |
50 |
public final String DESC = "Sends alerts over e-mail."; |
51 |
|
52 |
//---STATIC METHODS--- |
53 |
|
54 |
//---CONSTRUCTORS--- |
55 |
|
56 |
//---PUBLIC METHODS--- |
57 |
|
58 |
/** |
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 |
public void sendAlert(Alert alert) { |
66 |
// get the subject and replace fields |
67 |
String subject; |
68 |
try { |
69 |
subject = _cp.getProperty(_name, "Alerter.EMail.subject"); |
70 |
} catch (PropertyNotFoundException e) { |
71 |
subject = NOT_CONFIGURED; |
72 |
_logger.write(toString(), Logger.WARNING, "Alerter.EMail.subject value unavailable using default of " + subject); |
73 |
} |
74 |
subject = processAlertMessage(subject, alert); |
75 |
|
76 |
// get the message body and replace fields |
77 |
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 |
|
86 |
// attempt to send the actual message |
87 |
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 |
// list of destination addresses |
94 |
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 |
} |
101 |
|
102 |
// prepare to print the message |
103 |
PrintWriter out = smtp.getOutputStream(); |
104 |
out.println("Subject: "+subject+"\n"); |
105 |
|
106 |
// send the message |
107 |
out.println(message); |
108 |
smtp.sendMessage(); |
109 |
smtp.close(); |
110 |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ Alert.alertLevels[alert.getLevel()] + " level"); |
111 |
} |
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 |
} |
118 |
} |
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 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
125 |
* 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 |
* Return the String representation of what the alerter does |
138 |
* |
139 |
* @return the description |
140 |
*/ |
141 |
public String getDescription(){ |
142 |
return DESC; |
143 |
} |
144 |
|
145 |
//---PRIVATE METHODS--- |
146 |
|
147 |
//---ACCESSOR/MUTATOR METHODS--- |
148 |
|
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 |
|
160 |
//---ATTRIBUTES--- |
161 |
|
162 |
/** |
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 |
protected String _name = "EMail"; |
172 |
|
173 |
//---STATIC ATTRIBUTES--- |
174 |
|
175 |
} |