ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/listener MailLogger.java
Date Thu, 13 Dec 2001 12:23:17 GMT
bodewig     01/12/13 04:23:17

  Modified:    .        WHATSNEW
  Added:       src/main/org/apache/tools/ant/listener MailLogger.java
  Log:
  Add a BuildLogger that can send emails.
  
  Submitted by:	Erik Hatcher <jakarta-ant@ehatchersolutions.com>
  
  Revision  Changes    Path
  1.192     +3 -0      jakarta-ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
  retrieving revision 1.191
  retrieving revision 1.192
  diff -u -r1.191 -r1.192
  --- WHATSNEW	2001/12/13 11:56:25	1.191
  +++ WHATSNEW	2001/12/13 12:23:17	1.192
  @@ -125,6 +125,9 @@
   * <fail> no supports builds to fail based on conditions via if and
     unless attributes.
   
  +* Ant now comes with a BuildLogger implementation that can send emails
  +  containing a log of the build process.
  +
   Changes from Ant 1.4 to Ant 1.4.1
   ===========================================
   
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/listener/MailLogger.java
  
  Index: MailLogger.java
  ===================================================================
  /*
   *  The Apache Software License, Version 1.1
   *
   *  Copyright (c) 2001 The Apache Software Foundation.  All rights
   *  reserved.
   *
   *  Redistribution and use in source and binary forms, with or without
   *  modification, are permitted provided that the following conditions
   *  are met:
   *
   *  1. Redistributions of source code must retain the above copyright
   *  notice, this list of conditions and the following disclaimer.
   *
   *  2. Redistributions in binary form must reproduce the above copyright
   *  notice, this list of conditions and the following disclaimer in
   *  the documentation and/or other materials provided with the
   *  distribution.
   *
   *  3. The end-user documentation included with the redistribution, if
   *  any, must include the following acknowlegement:
   *  "This product includes software developed by the
   *  Apache Software Foundation (http://www.apache.org/)."
   *  Alternately, this acknowlegement may appear in the software itself,
   *  if and wherever such third-party acknowlegements normally appear.
   *
   *  4. The names "The Jakarta Project", "Ant", and "Apache Software
   *  Foundation" must not be used to endorse or promote products derived
   *  from this software without prior written permission. For written
   *  permission, please contact apache@apache.org.
   *
   *  5. Products derived from this software may not be called "Apache"
   *  nor may "Apache" appear in their names without prior written
   *  permission of the Apache Group.
   *
   *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   *  SUCH DAMAGE.
   *  ====================================================================
   *
   *  This software consists of voluntary contributions made by many
   *  individuals on behalf of the Apache Software Foundation.  For more
   *  information on the Apache Software Foundation, please see
   *  <http://www.apache.org/>.
   */
  package org.apache.tools.ant.listener;
  
  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 org.apache.tools.ant.BuildEvent;
  import org.apache.tools.ant.DefaultLogger;
  import org.apache.tools.ant.Project;
  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.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 build failed] - Address to send
   *    failure messages to</li>
   *    <li> MailLogger.success.to [required if build failed] - 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.
   *
   *@author     Erik Hatcher <a href="mailto:erik@hatcher.net">erik@hatcher.net</a>
   *@created    December 12, 2001
   */
  public class MailLogger extends DefaultLogger {
    
    private StringBuffer buffer = new StringBuffer();
  
    /**
     *  Sends an e-mail with the log results.
     *
     *@param  event
     */
    public void buildFinished(BuildEvent 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);
          if (is != null) {
            fileProperties.load(is);
          }
        }
        catch (IOException ioe) {
          // ignore because properties file is not required
        } finally {
          if (is != null) {
            try {
              is.close();
            } catch (IOException e) {}
          }
        }
      }
  
      for (Enumeration enum = fileProperties.keys(); enum.hasMoreElements(); ) {
        String key = (String) enum.nextElement();
        properties.put(key, fileProperties.getProperty(key));
      }
  
      boolean success = (event.getException() == null);
      String prefix = success ? "success" : "failure";
  
      try {
        String mailhost = getValue(properties, "mailhost", "localhost");
        String from = getValue(properties, "from", null);
  
        boolean notify = Project.toBoolean(getValue(properties,
            prefix + ".notify", "on"));
  
        if (!notify) {
          return;
        }
  
        String toList = getValue(properties, prefix + ".to", null);
        String subject = getValue(properties, prefix + ".subject",
            (success) ? "Build Success" : "Build Failure");
  
        sendMail(mailhost, from, toList, subject, buffer.toString());
      }
      catch (Exception e) {
        System.out.println("MailLogger failed to send e-mail!");
        e.printStackTrace();
      }
    }
  
  
    /**
     *  Receives and buffers log messages.
     *
     *@param  message
     */
    protected void log(String message) {
      buffer.append(message + lSep);
    }
  
  
    /**
     *  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 {
      name = "MailLogger." + name;
      Object object = properties.get(name);
      String value = defaultValue;
  
      if (object != null) {
        value = (String) object;
      }
  
      if (value == null) {
        throw new Exception("Missing required parameter: " + name);
      }
  
      return value;
    }
  
  
    /**
     *  Send the mail
     *
     *@param  mailhost         mail server
     *@param  from             from address
     *@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, String from, String toList,
        String subject, String message) throws IOException {
      MailMessage mailMessage = new MailMessage(mailhost);
  
      mailMessage.from(from);
  
      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();
    }
  }
  
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message