FileDocCategorySizeDatePackage
MailLogger.javaAPI DocApache Ant 1.7011365Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.listener

MailLogger

public class MailLogger extends org.apache.tools.ant.DefaultLogger
Buffers log messages from DefaultLogger, and sends an e-mail with the results. The following Project properties are used to send the mail.
  • MailLogger.mailhost [default: localhost] - Mail server to use
  • MailLogger.port [default: 25] - Default port for SMTP
  • MailLogger.from [required] - Mail "from" address
  • MailLogger.failure.notify [default: true] - Send build failure e-mails?
  • MailLogger.success.notify [default: true] - Send build success e-mails?
  • MailLogger.failure.to [required if failure mail to be sent] - Address to send failure messages to
  • MailLogger.success.to [required if success mail to be sent] - Address to send success messages to
  • MailLogger.failure.subject [default: "Build Failure"] - Subject of failed build
  • MailLogger.success.subject [default: "Build Success"] - Subject of successful build
These properties are set using standard Ant property setting mechanisms (<property>, command-line -D, etc). Ant properties can be overridden by specifying the filename of a properties file in the MailLogger.properties.file property . Any properties defined in that file will override Ant properties.

Fields Summary
private StringBuffer
buffer
Buffer in which the message is constructed prior to sending
Constructors Summary
Methods Summary
public voidbuildFinished(org.apache.tools.ant.BuildEvent event)
Sends an e-mail with the log results.

param
event the build finished event


                       
        
        super.buildFinished(event);

        Project project = event.getProject();
        Hashtable 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 {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        }

        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;
            }

            String mailhost = getValue(properties, "mailhost", "localhost");
            int port = Integer.parseInt(getValue(properties, "port",
                                        String.valueOf(MailMessage.DEFAULT_PORT)));
            String user = getValue(properties, "user", "");
            String password = getValue(properties, "password", "");
            boolean ssl = Project.toBoolean(getValue(properties,
                     "ssl", "off"));
            String from = getValue(properties, "from", null);
            String replytoList = getValue(properties, "replyto", "");
            String toList = getValue(properties, prefix + ".to", null);
            String subject = getValue(properties, prefix + ".subject",
                    (success) ? "Build Success" : "Build Failure");
            if (user.equals("") && password.equals("") && !ssl) {
                sendMail(mailhost, port,  from, replytoList, toList,
                         subject, buffer.substring(0));
            } else {
                sendMimeMail(event.getProject(), mailhost, port, user,
                             password, ssl, from, replytoList, toList,
                             subject, buffer.substring(0));
            }
        } catch (Exception e) {
            System.out.println("MailLogger failed to send e-mail!");
            e.printStackTrace(System.err);
        }
    
private java.lang.StringgetValue(java.util.Hashtable properties, java.lang.String name, java.lang.String defaultValue)
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.

        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;
    
protected voidlog(java.lang.String message)
Receives and buffers log messages.

param
message the message being logger

        buffer.append(message).append(StringUtils.LINE_SEP);
    
private voidsendMail(java.lang.String mailhost, int port, java.lang.String from, java.lang.String replyToList, java.lang.String toList, java.lang.String subject, java.lang.String message)
Send the mail

param
mailhost mail server
param
port mail server port number
param
from from address
param
replyToList comma-separated replyto list
param
toList comma-separated recipient list
param
subject mail subject
param
message mail body
exception
IOException thrown if sending message fails

        MailMessage mailMessage = new MailMessage(mailhost, port);
        mailMessage.setHeader("Date", DateUtils.getDateForHeader());

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

        mailMessage.setSubject(subject);

        PrintStream ps = mailMessage.getPrintStream();
        ps.println(message);

        mailMessage.sendAndClose();
    
private voidsendMimeMail(org.apache.tools.ant.Project project, java.lang.String host, int port, java.lang.String user, java.lang.String password, boolean ssl, java.lang.String from, java.lang.String replyToString, java.lang.String toString, java.lang.String subject, java.lang.String message)
Send the mail (MimeMail)

param
project current ant project
param
host mail server
param
port mail server port number
param
user user name for SMTP auth
param
password password for SMTP auth
param
ssl if true send message over SSL
param
from from address
param
replyToString comma-separated replyto list
param
toString comma-separated recipient list
param
subject mail subject
param
message mail body

        // convert the replyTo string into a vector of emailaddresses
        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;
        }
        Vector replyToList = vectorizeEmailAddresses(replyToString);
        mailer.setHost(host);
        mailer.setPort(port);
        mailer.setUser(user);
        mailer.setPassword(password);
        mailer.setSSL(ssl);
        Message mymessage = new Message(message);
        mymessage.setProject(project);
        mailer.setMessage(mymessage);
        mailer.setFrom(new EmailAddress(from));
        mailer.setReplyToList(replyToList);
        Vector toList = vectorizeEmailAddresses(toString);
        mailer.setToList(toList);
        mailer.setCcList(new Vector());
        mailer.setBccList(new Vector());
        mailer.setFiles(new Vector());
        mailer.setSubject(subject);
        mailer.send();
    
private java.util.VectorvectorizeEmailAddresses(java.lang.String listString)

        Vector emailList = new Vector();
        StringTokenizer tokens = new StringTokenizer(listString, ",");
        while (tokens.hasMoreTokens()) {
            emailList.addElement(new EmailAddress(tokens.nextToken()));
        }
        return emailList;