aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/listener/MailLogger.java
blob: 4b50547f349e7d1a3b539f0d8c77150440d62b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.listener;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.email.EmailAddress;
import org.apache.tools.ant.taskdefs.email.Header;
import org.apache.tools.ant.taskdefs.email.Mailer;
import org.apache.tools.ant.taskdefs.email.Message;
import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.DateUtils;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.StringUtils;
import org.apache.tools.mail.MailMessage;

/**
 *  Buffers log messages from DefaultLogger, and sends an e-mail with the
 *  results. The following Project properties are used to send the mail.
 *  <ul>
 *    <li> MailLogger.mailhost [default: localhost] - Mail server to use</li>
 *    <li> MailLogger.port [default: 25] - Default port for SMTP </li>
 *    <li> Maillogger.user [no default] - user name for SMTP auth
 *    (requires JavaMail)</li>
 *    <li> Maillogger.password [no default] - password for SMTP auth
 *    (requires JavaMail)</li>
 *    <li> Maillogger.ssl [default: false] - on or true if ssl is
 *    needed (requires JavaMail)</li>
 *    <li> MailLogger.from [required] - Mail "from" address</li>
 *    <li> MailLogger.from [no default] - Mail "replyto" address(es),
 *    comma-separated</li>
 *    <li> MailLogger.failure.notify [default: true] - Send build failure
 *    e-mails?</li>
 *    <li> MailLogger.success.notify [default: true] - Send build success
 *    e-mails?</li>
 *    <li> MailLogger.failure.to [required if failure mail to be sent] - Address
 *    to send failure messages to</li>
 *    <li> MailLogger.success.to [required if success mail to be sent] - Address
 *    to send success messages to</li>
 *    <li> MailLogger.failure.cc [no default] - Address
 *    to send failure messages to carbon copy (cc)</li>
 *    <li> MailLogger.success.to [no default] - Address
 *    to send success messages to carbon copy (cc)</li>
 *    <li> MailLogger.failure.bcc [no default] - Address
 *    to send failure messages to blind carbon copy (bcc)</li>
 *    <li> MailLogger.success.bcc [no default] - Address
 *    to send success messages to blind carbon copy (bcc)</li>
 *    <li> MailLogger.failure.subject [default: "Build Failure"] - Subject of
 *    failed build</li>
 *    <li> MailLogger.success.subject [default: "Build Success"] - Subject of
 *    successful build</li>
 *    <li> MailLogger.failure.body [default: none] - fixed text of
 *    mail body for a failed build, default is to send the logfile</li>
 *    <li> MailLogger.success.body [default: none] - fixed text of
 *    mail body for a successful build, default is to send the logfile</li>
 *    <li> MailLogger.mimeType [default: text/plain] - MIME-Type of email</li>
 *    <li> MailLogger.charset [no default] - character set of email</li>
 *    <li> Maillogger.starttls.enable [default: false] - on or true if
 *    STARTTLS should be supported (requires JavaMail)</li>
 *    <li> MailLogger.properties.file [no default] - Filename of
 *    properties file that will override other values.</li>
 *  </ul>
 *  These properties are set using standard Ant property setting mechanisms
 *  (&lt;property&gt;, command-line -D, etc). Ant properties can be overridden
 *  by specifying the filename of a properties file in the <i>
 *  MailLogger.properties.file property</i> . Any properties defined in that
 *  file will override Ant properties.
 *
 */
public class MailLogger extends DefaultLogger {
    /** Buffer in which the message is constructed prior to sending */
    private StringBuffer buffer = new StringBuffer();

    private static final String DEFAULT_MIME_TYPE = "text/plain";

    /**
     *  Sends an e-mail with the log results.
     *
     * @param event the build finished event
     */
    public void buildFinished(BuildEvent event) {
        super.buildFinished(event);

        Project project = event.getProject();
        Hashtable<String, Object> properties = project.getProperties();

        // overlay specified properties file (if any), which overrides project
        // settings
        Properties fileProperties = new Properties();
        String filename = (String) properties.get("MailLogger.properties.file");
        if (filename != null) {
            InputStream is = null;
            try {
                is = new FileInputStream(filename);
                fileProperties.load(is);
            } catch (IOException ioe) {
                // ignore because properties file is not required
            } finally {
                FileUtils.close(is);
            }
        }

        for (Enumeration<?> e = fileProperties.keys(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            String value = fileProperties.getProperty(key);
            properties.put(key, project.replaceProperties(value));
        }

        boolean success = (event.getException() == null);
        String prefix = success ? "success" : "failure";

        try {
            boolean notify = Project.toBoolean(getValue(properties,
                    prefix + ".notify", "on"));

            if (!notify) {
                return;
            }
            Values values = new Values()
                .mailhost(getValue(properties, "mailhost", "localhost"))
                .port(Integer.parseInt(
                          getValue(
                              properties, "port",
                              String.valueOf(MailMessage.DEFAULT_PORT))))
                .user(getValue(properties, "user", ""))
                .password(getValue(properties, "password", ""))
                .ssl(Project.toBoolean(getValue(properties,
                                                "ssl", "off")))
                .starttls(Project.toBoolean(getValue(properties,
                                                     "starttls.enable", "off")))
                .from(getValue(properties, "from", null))
                .replytoList(getValue(properties, "replyto", ""))
                .toList(getValue(properties, prefix + ".to", null))
                .toCcList(getValue(properties, prefix + ".cc", ""))
                .toBccList(getValue(properties, prefix + ".bcc", ""))
                .mimeType(getValue(properties, "mimeType", DEFAULT_MIME_TYPE))
                .charset(getValue(properties, "charset", ""))
                .body(getValue(properties, prefix + ".body", ""))
                .subject(getValue(
                             properties, prefix + ".subject",
                             (success) ? "Build Success" : "Build Failure"));
            if (values.user().equals("")
                && values.password().equals("")
                && !values.ssl() && !values.starttls()) {
                sendMail(values, buffer.substring(0));
            } else {
                sendMimeMail(
                    event.getProject(), values, buffer.substring(0));
            }
        } catch (Exception e) {
            System.out.println("MailLogger failed to send e-mail!");
            e.printStackTrace(System.err);
        }
    }

    private static class Values {
        private String mailhost;
        public String mailhost() {
            return mailhost;
        }
        public Values mailhost(String mailhost) {
            this.mailhost = mailhost;
            return this;
        }
        private int port;
        public int port() {
            return port;
        }
        public Values port(int port) {
            this.port = port;
            return this;
        }
        private String user;
        public String user() {
            return user;
        }
        public Values user(String user) {
            this.user = user;
            return this;
        }
        private String password;
        public String password() {
            return password;
        }
        public Values password(String password) {
            this.password = password;
            return this;
        }
        private boolean ssl;
        public boolean ssl() {
            return ssl;
        }
        public Values ssl(boolean ssl) {
            this.ssl = ssl;
            return this;
        }
        private String from;
        public String from() {
            return from;
        }
        public Values from(String from) {
            this.from = from;
            return this;
        }
        private String replytoList;
        public String replytoList() {
            return replytoList;
        }
        public Values replytoList(String replytoList) {
            this.replytoList = replytoList;
            return this;
        }
        private String toList;
        public String toList() {
            return toList;
        }
        public Values toList(String toList) {
            this.toList = toList;
            return this;
        }
        private String toCcList;
        public String toCcList() {
            return toCcList;
        }
        public Values toCcList(String toCcList) {
            this.toCcList = toCcList;
            return this;
        }
        private String toBccList;
        public String toBccList() {
            return toBccList;
        }
        public Values toBccList(String toBccList) {
            this.toBccList = toBccList;
            return this;
        }
        private String subject;
        public String subject() {
            return subject;
        }
        public Values subject(String subject) {
            this.subject = subject;
            return this;
        }
        private String charset;
        public String charset() {
            return charset;
        }
        public Values charset(String charset) {
            this.charset = charset;
            return this;
        }
        private String mimeType;
        public String mimeType() {
            return mimeType;
        }
        public Values mimeType(String mimeType) {
            this.mimeType = mimeType;
            return this;
        }
        private String body;
        public String body() {
            return body;
        }
        public Values body(String body) {
            this.body = body;
            return this;
        }
        private boolean starttls;
        public boolean starttls() {
            return starttls;
        }
        public Values starttls(boolean starttls) {
            this.starttls = starttls;
            return this;
        }
    }

    /**
     *  Receives and buffers log messages.
     *
     * @param message the message being logger
     */
    protected void log(String message) {
        buffer.append(message).append(StringUtils.LINE_SEP);
    }


    /**
     *  Gets the value of a property.
     *
     * @param  properties     Properties to obtain value from
     * @param  name           suffix of property name. "MailLogger." will be
     *      prepended internally.
     * @param  defaultValue   value returned if not present in the properties.
     *      Set to null to make required.
     * @return                The value of the property, or default value.
     * @exception  Exception  thrown if no default value is specified and the
     *      property is not present in properties.
     */
    private String getValue(Hashtable<String, Object> properties, String name,
                            String defaultValue) throws Exception {
        String propertyName = "MailLogger." + name;
        String value = (String) properties.get(propertyName);

        if (value == null) {
            value = defaultValue;
        }

        if (value == null) {
            throw new Exception("Missing required parameter: " + propertyName);
        }

        return value;
    }


    /**
     *  Send the mail
     * @param  values           the various values.
     * @param  message          mail body
     * @exception  IOException  thrown if sending message fails
     */
    private void sendMail(Values values, String message) throws IOException {
        MailMessage mailMessage = new MailMessage(
            values.mailhost(), values.port());
        mailMessage.setHeader("Date", DateUtils.getDateForHeader());

        mailMessage.from(values.from());
        if (!values.replytoList().equals("")) {
            StringTokenizer t = new StringTokenizer(
                values.replytoList(), ", ", false);
            while (t.hasMoreTokens()) {
                mailMessage.replyto(t.nextToken());
            }
        }
        StringTokenizer t = new StringTokenizer(values.toList(), ", ", false);
        while (t.hasMoreTokens()) {
            mailMessage.to(t.nextToken());
        }

        mailMessage.setSubject(values.subject());

        if (values.charset().length() > 0) {
            mailMessage.setHeader("Content-Type", values.mimeType()
                                  + "; charset=\"" + values.charset() + "\"");
        } else {
            mailMessage.setHeader("Content-Type", values.mimeType());
        }

        PrintStream ps = mailMessage.getPrintStream();
        ps.println(values.body().length() > 0 ? values.body() : message);

        mailMessage.sendAndClose();
    }
    /**
     *  Send the mail  (MimeMail)
     * @param  project          current ant project
     * @param  values           various values
     * @param  message          mail body
     */
    private void sendMimeMail(Project project, Values values, String message) {
        Mailer mailer = null;
        try {
            mailer = (Mailer) ClasspathUtils.newInstance(
                    "org.apache.tools.ant.taskdefs.email.MimeMailer",
                    MailLogger.class.getClassLoader(), Mailer.class);
        } catch (BuildException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            log("Failed to initialise MIME mail: " + t.getMessage());
            return;
        }
        // convert the replyTo string into a vector of emailaddresses
        Vector<EmailAddress> replyToList = vectorizeEmailAddresses(values.replytoList());
        mailer.setHost(values.mailhost());
        mailer.setPort(values.port());
        mailer.setUser(values.user());
        mailer.setPassword(values.password());
        mailer.setSSL(values.ssl());
        mailer.setEnableStartTLS(values.starttls());
        Message mymessage =
            new Message(values.body().length() > 0 ? values.body() : message);
        mymessage.setProject(project);
        mymessage.setMimeType(values.mimeType());
        if (values.charset().length() > 0) {
            mymessage.setCharset(values.charset());
        }
        mailer.setMessage(mymessage);
        mailer.setFrom(new EmailAddress(values.from()));
        mailer.setReplyToList(replyToList);
        Vector<EmailAddress> toList = vectorizeEmailAddresses(values.toList());
        mailer.setToList(toList);
        Vector<EmailAddress> toCcList = vectorizeEmailAddresses(values.toCcList());
        mailer.setCcList(toCcList);
        Vector<EmailAddress> toBccList = vectorizeEmailAddresses(values.toBccList());
        mailer.setBccList(toBccList);
        mailer.setFiles(new Vector<File>());
        mailer.setSubject(values.subject());
        mailer.setHeaders(new Vector<Header>());
        mailer.send();
    }
    private Vector<EmailAddress> vectorizeEmailAddresses(String listString) {
        Vector<EmailAddress> emailList = new Vector<EmailAddress>();
        StringTokenizer tokens = new StringTokenizer(listString, ",");
        while (tokens.hasMoreTokens()) {
            emailList.addElement(new EmailAddress(tokens.nextToken()));
        }
        return emailList;
    }
}