1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.listener;
|
---|
18 |
|
---|
19 | import java.io.FileInputStream;
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.InputStream;
|
---|
22 | import java.io.PrintStream;
|
---|
23 | import java.util.Hashtable;
|
---|
24 | import java.util.Vector;
|
---|
25 | import java.util.Properties;
|
---|
26 | import java.util.Enumeration;
|
---|
27 | import java.util.StringTokenizer;
|
---|
28 |
|
---|
29 | import org.apache.tools.ant.BuildEvent;
|
---|
30 | import org.apache.tools.ant.DefaultLogger;
|
---|
31 | import org.apache.tools.ant.Project;
|
---|
32 | import org.apache.tools.ant.taskdefs.email.EmailAddress;
|
---|
33 | import org.apache.tools.ant.taskdefs.email.Message;
|
---|
34 | import org.apache.tools.ant.taskdefs.email.Mailer;
|
---|
35 | import org.apache.tools.ant.util.DateUtils;
|
---|
36 | import org.apache.tools.ant.util.StringUtils;
|
---|
37 | import org.apache.tools.mail.MailMessage;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Buffers log messages from DefaultLogger, and sends an e-mail with the
|
---|
41 | * results. The following Project properties are used to send the mail.
|
---|
42 | * <ul>
|
---|
43 | * <li> MailLogger.mailhost [default: localhost] - Mail server to use</li>
|
---|
44 | * <li> MailLogger.port [default: 25] - Default port for SMTP </li>
|
---|
45 | * <li> MailLogger.from [required] - Mail "from" address</li>
|
---|
46 | * <li> MailLogger.failure.notify [default: true] - Send build failure
|
---|
47 | * e-mails?</li>
|
---|
48 | * <li> MailLogger.success.notify [default: true] - Send build success
|
---|
49 | * e-mails?</li>
|
---|
50 | * <li> MailLogger.failure.to [required if failure mail to be sent] - Address
|
---|
51 | * to send failure messages to</li>
|
---|
52 | * <li> MailLogger.success.to [required if success mail to be sent] - Address
|
---|
53 | * to send success messages to</li>
|
---|
54 | * <li> MailLogger.failure.subject [default: "Build Failure"] - Subject of
|
---|
55 | * failed build</li>
|
---|
56 | * <li> MailLogger.success.subject [default: "Build Success"] - Subject of
|
---|
57 | * successful build</li>
|
---|
58 | * </ul>
|
---|
59 | * These properties are set using standard Ant property setting mechanisms
|
---|
60 | * (<property>, command-line -D, etc). Ant properties can be overridden
|
---|
61 | * by specifying the filename of a properties file in the <i>
|
---|
62 | * MailLogger.properties.file property</i> . Any properties defined in that
|
---|
63 | * file will override Ant properties.
|
---|
64 | *
|
---|
65 | *
|
---|
66 | */
|
---|
67 | public class MailLogger extends DefaultLogger {
|
---|
68 | /** Buffer in which the message is constructed prior to sending */
|
---|
69 | private StringBuffer buffer = new StringBuffer();
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Sends an e-mail with the log results.
|
---|
73 | *
|
---|
74 | * @param event the build finished event
|
---|
75 | */
|
---|
76 | public void buildFinished(BuildEvent event) {
|
---|
77 | super.buildFinished(event);
|
---|
78 |
|
---|
79 | Project project = event.getProject();
|
---|
80 | Hashtable properties = project.getProperties();
|
---|
81 |
|
---|
82 | // overlay specified properties file (if any), which overrides project
|
---|
83 | // settings
|
---|
84 | Properties fileProperties = new Properties();
|
---|
85 | String filename = (String) properties.get("MailLogger.properties.file");
|
---|
86 | if (filename != null) {
|
---|
87 | InputStream is = null;
|
---|
88 | try {
|
---|
89 | is = new FileInputStream(filename);
|
---|
90 | fileProperties.load(is);
|
---|
91 | } catch (IOException ioe) {
|
---|
92 | // ignore because properties file is not required
|
---|
93 | } finally {
|
---|
94 | if (is != null) {
|
---|
95 | try {
|
---|
96 | is.close();
|
---|
97 | } catch (IOException e) {
|
---|
98 | // ignore
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | for (Enumeration e = fileProperties.keys(); e.hasMoreElements();) {
|
---|
105 | String key = (String) e.nextElement();
|
---|
106 | String value = fileProperties.getProperty(key);
|
---|
107 | properties.put(key, project.replaceProperties(value));
|
---|
108 | }
|
---|
109 |
|
---|
110 | boolean success = (event.getException() == null);
|
---|
111 | String prefix = success ? "success" : "failure";
|
---|
112 |
|
---|
113 | try {
|
---|
114 | boolean notify = Project.toBoolean(getValue(properties,
|
---|
115 | prefix + ".notify", "on"));
|
---|
116 |
|
---|
117 | if (!notify) {
|
---|
118 | return;
|
---|
119 | }
|
---|
120 |
|
---|
121 | String mailhost = getValue(properties, "mailhost", "localhost");
|
---|
122 | int port = Integer.parseInt(getValue(properties, "port",
|
---|
123 | String.valueOf(MailMessage.DEFAULT_PORT)));
|
---|
124 | String user = getValue(properties, "user", "");
|
---|
125 | String password = getValue(properties, "password", "");
|
---|
126 | boolean ssl = Project.toBoolean(getValue(properties,
|
---|
127 | "ssl", "off"));
|
---|
128 | String from = getValue(properties, "from", null);
|
---|
129 | String replytoList = getValue(properties, "replyto", "");
|
---|
130 | String toList = getValue(properties, prefix + ".to", null);
|
---|
131 | String subject = getValue(properties, prefix + ".subject",
|
---|
132 | (success) ? "Build Success" : "Build Failure");
|
---|
133 | if (user.equals("") && password.equals("") && !ssl) {
|
---|
134 | sendMail(mailhost, port, from, replytoList, toList,
|
---|
135 | subject, buffer.substring(0));
|
---|
136 | } else {
|
---|
137 | sendMimeMail(event.getProject(), mailhost, port, user,
|
---|
138 | password, ssl, from, replytoList, toList,
|
---|
139 | subject, buffer.substring(0));
|
---|
140 | }
|
---|
141 | } catch (Exception e) {
|
---|
142 | System.out.println("MailLogger failed to send e-mail!");
|
---|
143 | e.printStackTrace(System.err);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Receives and buffers log messages.
|
---|
150 | *
|
---|
151 | * @param message the message being logger
|
---|
152 | */
|
---|
153 | protected void log(String message) {
|
---|
154 | buffer.append(message).append(StringUtils.LINE_SEP);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Gets the value of a property.
|
---|
160 | *
|
---|
161 | * @param properties Properties to obtain value from
|
---|
162 | * @param name suffix of property name. "MailLogger." will be
|
---|
163 | * prepended internally.
|
---|
164 | * @param defaultValue value returned if not present in the properties.
|
---|
165 | * Set to null to make required.
|
---|
166 | * @return The value of the property, or default value.
|
---|
167 | * @exception Exception thrown if no default value is specified and the
|
---|
168 | * property is not present in properties.
|
---|
169 | */
|
---|
170 | private String getValue(Hashtable properties, String name,
|
---|
171 | String defaultValue) throws Exception {
|
---|
172 | String propertyName = "MailLogger." + name;
|
---|
173 | String value = (String) properties.get(propertyName);
|
---|
174 |
|
---|
175 | if (value == null) {
|
---|
176 | value = defaultValue;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (value == null) {
|
---|
180 | throw new Exception("Missing required parameter: " + propertyName);
|
---|
181 | }
|
---|
182 |
|
---|
183 | return value;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Send the mail
|
---|
189 | * @param mailhost mail server
|
---|
190 | * @param port mail server port number
|
---|
191 | * @param from from address
|
---|
192 | * @param replyToList comma-separated replyto list
|
---|
193 | * @param toList comma-separated recipient list
|
---|
194 | * @param subject mail subject
|
---|
195 | * @param message mail body
|
---|
196 | * @exception IOException thrown if sending message fails
|
---|
197 | */
|
---|
198 | private void sendMail(String mailhost, int port, String from, String replyToList, String toList,
|
---|
199 | String subject, String message) throws IOException {
|
---|
200 | MailMessage mailMessage = new MailMessage(mailhost, port);
|
---|
201 | mailMessage.setHeader("Date", DateUtils.getDateForHeader());
|
---|
202 |
|
---|
203 | mailMessage.from(from);
|
---|
204 | if (!replyToList.equals("")) {
|
---|
205 | StringTokenizer t = new StringTokenizer(replyToList, ", ", false);
|
---|
206 | while (t.hasMoreTokens()) {
|
---|
207 | mailMessage.replyto(t.nextToken());
|
---|
208 | }
|
---|
209 | }
|
---|
210 | StringTokenizer t = new StringTokenizer(toList, ", ", false);
|
---|
211 | while (t.hasMoreTokens()) {
|
---|
212 | mailMessage.to(t.nextToken());
|
---|
213 | }
|
---|
214 |
|
---|
215 | mailMessage.setSubject(subject);
|
---|
216 |
|
---|
217 | PrintStream ps = mailMessage.getPrintStream();
|
---|
218 | ps.println(message);
|
---|
219 |
|
---|
220 | mailMessage.sendAndClose();
|
---|
221 | }
|
---|
222 | /**
|
---|
223 | * Send the mail (MimeMail)
|
---|
224 | * @param project current ant project
|
---|
225 | * @param host mail server
|
---|
226 | * @param port mail server port number
|
---|
227 | * @param user user name for SMTP auth
|
---|
228 | * @param password password for SMTP auth
|
---|
229 | * @param ssl if true send message over SSL
|
---|
230 | * @param from from address
|
---|
231 | * @param replyToString comma-separated replyto list
|
---|
232 | * @param toString comma-separated recipient list
|
---|
233 | * @param subject mail subject
|
---|
234 | * @param message mail body
|
---|
235 | */
|
---|
236 | private void sendMimeMail(Project project, String host, int port,
|
---|
237 | String user, String password, boolean ssl,
|
---|
238 | String from, String replyToString,
|
---|
239 | String toString, String subject,
|
---|
240 | String message) {
|
---|
241 | // convert the replyTo string into a vector of emailaddresses
|
---|
242 | Mailer mailer = null;
|
---|
243 | try {
|
---|
244 | mailer =
|
---|
245 | (Mailer) Class.forName("org.apache.tools.ant.taskdefs.email.MimeMailer")
|
---|
246 | .newInstance();
|
---|
247 | } catch (Throwable e) {
|
---|
248 | log("Failed to initialise MIME mail: " + e.getMessage());
|
---|
249 | return;
|
---|
250 | }
|
---|
251 | Vector replyToList = vectorizeEmailAddresses(replyToString);
|
---|
252 | mailer.setHost(host);
|
---|
253 | mailer.setPort(port);
|
---|
254 | mailer.setUser(user);
|
---|
255 | mailer.setPassword(password);
|
---|
256 | mailer.setSSL(ssl);
|
---|
257 | Message mymessage = new Message(message);
|
---|
258 | mymessage.setProject(project);
|
---|
259 | mailer.setMessage(mymessage);
|
---|
260 | mailer.setFrom(new EmailAddress(from));
|
---|
261 | mailer.setReplyToList(replyToList);
|
---|
262 | Vector toList = vectorizeEmailAddresses(toString);
|
---|
263 | mailer.setToList(toList);
|
---|
264 | mailer.setCcList(new Vector());
|
---|
265 | mailer.setBccList(new Vector());
|
---|
266 | mailer.setFiles(new Vector());
|
---|
267 | mailer.setSubject(subject);
|
---|
268 | mailer.send();
|
---|
269 | }
|
---|
270 | private Vector vectorizeEmailAddresses(String listString) {
|
---|
271 | Vector emailList = new Vector();
|
---|
272 | StringTokenizer tokens = new StringTokenizer(listString, ",");
|
---|
273 | while (tokens.hasMoreTokens()) {
|
---|
274 | emailList.addElement(new EmailAddress(tokens.nextToken()));
|
---|
275 | }
|
---|
276 | return emailList;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|