Here is the code. Line 82 - 87 is where the work is needed. Could parameterize 
some stuff also. This is mostly the same as the MailLogger, in fact that was my 
starting point, as you may notice the documentation section below the imports 
has not been updated.

-Rob A

/*
 * Copyright  2002-2004 The Apache Software Foundation
 *
 *  Licensed 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.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Properties;
import java.util.Enumeration;
import java.util.StringTokenizer;

import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.XmlLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.email.EmailAddress;
import org.apache.tools.ant.taskdefs.email.Message;
import org.apache.tools.ant.taskdefs.email.Mailer;
import org.apache.tools.ant.util.DateUtils;
import org.apache.tools.ant.util.StringUtils;
import org.apache.tools.mail.MailMessage;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.XSLTProcess;

/**
 *  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.from [required] - Mail "from" address</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.subject [default: "Build Failure"] - Subject of
 *    failed build</li>
 *    <li> MailLogger.success.subject [default: "Build Success"] - Subject of
 *    successful build</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 HtmlMailLogger extends XmlLogger {
    /** Buffer in which the message is constructed prior to sending */
    private StringBuffer buffer = new StringBuffer();

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

                // This is the part I am not doing right.
                // Should use Project.createTask() and use XSLTProcess setters
                // Then call XSLTProcess.execute.
                XSLTProcess xslt = new XSLTProcess();
                xslt.process(new File("log.xml"),new File("log.html"),new 
File("log.xsl"));
        Project project = event.getProject();
                String logfile = "log.html";
        Hashtable properties = project.getProperties();
                try {
                        BufferedReader reader = new BufferedReader(new 
FileReader(logfile));
                        String line = reader.readLine();
                        while (line != null) {
                                log(line);
                                line = reader.readLine();
                        }
                } catch (FileNotFoundException e) {
                        throw new BuildException("Unable to open log file",e);
                } catch (IOException e) {
                        throw new BuildException("Error reading log file",e);
                }

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


    /**
     *  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 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  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
     */
    private void sendMail(String mailhost, int port, String from, String 
replyToList, String toList,
                          String subject, String message) throws IOException {
        MailMessage mailMessage = new MailMessage(mailhost, port);
        mailMessage.setHeader("Date", DateUtils.getDateForHeader());
                mailMessage.setHeader("Content-Type","text/html");

        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();
    }
    /**
     *  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
     */
    private void sendMimeMail(Project project, String host, int port,
                              String user, String password, boolean ssl,
                              String from, String replyToString,
                              String toString, String subject,
                              String message)  {
        // convert the replyTo string into a vector of emailaddresses
        Mailer mailer = null;
            try {
                mailer =
                    (Mailer) 
Class.forName("org.apache.tools.ant.taskdefs.email.MimeMailer")
                    .newInstance();
            } catch (Throwable e) {
                log("Failed to initialise MIME mail: " + e.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 Vector vectorizeEmailAddresses(String listString) {
        Vector emailList = new Vector();
        StringTokenizer tokens = new StringTokenizer(listString, ",");
        while (tokens.hasMoreTokens()) {
            emailList.addElement(new EmailAddress(tokens.nextToken()));
        }
        return emailList;
    }
}



> -----Original Message-----
> From: Anderson, Rob (Global Trade) [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, June 14, 2005 5:07 PM
> To: Ant Users List
> Cc: dev@ant.apache.org
> Subject: RE: Mail a nice pretty html build log...
> 
> 
> Ivan, Thanks. What I'm really looking for is an HTMLMailLogger.
> 
> All, I have almost got one working but I am having some 
> trouble running the xslt. I'm not really committed to making 
> it work, since I don't really need it, but if anyone is 
> interested in it, I'll send out what I have and you can finnish it up.
> 
> Let me know.
> 
> -Rob A
> 
> > -----Original Message-----
> > From: Ivan Ivanov [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, June 14, 2005 12:05 PM
> > To: Ant Users List
> > Subject: Re: Mail a nice pretty html build log...
> > 
> > 
> > Rob,
> > 
> > I have an extension of MailLogger that allows to send
> > emails with some files attached to it. It works pretty
> > closely to the original oata.listener.MailLogger. You
> > only should specify the location of the attachments by
> > specifying MailLogger.success.attachments and
> > MailLogger.failure.attachments and at the end of the
> > build proccess it will send a message with the
> > attached files.
> > 
> > If you find this useful, I will post the code as well.
> > 
> > Regards
> > Ivan
> > 
> > --- "Anderson, Rob (Global Trade)"
> > <[EMAIL PROTECTED]> wrote:
> > 
> > > Hi all, I would like to be able to mail a nice
> > > pretty html build log. There is a slight dilema
> > > though. If I run:
> > > 
> > > ant -listener org.apache.tools.ant.XmlLogger
> > > 
> > > The log is not produced until the build is complete.
> > > I would like to do this in one invocation of ant. I
> > > think it might be possible if I write a wrapper
> > > target like so:
> > > 
> > >   <target name="build" depends="wrapper">
> > >   </target>
> > > 
> > >   <target name="test">
> > >     <tstamp>
> > >       <format property="start.time"
> > > pattern="MM/dd/yyyy hh:mm:ss aa"/>
> > >     </tstamp>
> > >     <echo>${start.time}</echo>
> > >   </target>
> > > 
> > >   <target name="wrapper">
> > >     <ant target="test">
> > >       <property name="listener"
> > > value="org.apache.tools.ant.XmlLogger"/>
> > >     </ant>
> > >     <style ... /style>
> > >     <mail ... /mail>
> > >   </target>
> > > 
> > > The problem I am having is with making the XmlLogger
> > > work via <ant> task. Any help would be appreciated.
> > > 
> > > Thanks,
> > > 
> > > ______________________
> > > Robert Anderson
> > > Sr. System Engineer
> > > Nike - Global Trade IT
> > > 503-532-6803 
> > > 
> > > 
> > >
> > 
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > > [EMAIL PROTECTED]
> > > For additional commands, e-mail:
> > > [EMAIL PROTECTED]
> > > 
> > > 
> > 
> > 
> > 
> >             
> > __________________________________ 
> > Discover Yahoo! 
> > Have fun online with music videos, cool games, IM and more. 
> > Check it out! 
> > http://discover.yahoo.com/online.html
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> > 
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to