Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 241   Methods: 4
NCLOC: 93   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MailLogger.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.listener;
 55   
 
 56   
 import java.io.FileInputStream;
 57   
 import java.io.IOException;
 58   
 import java.io.InputStream;
 59   
 import java.io.PrintStream;
 60   
 import java.util.Enumeration;
 61   
 import java.util.Hashtable;
 62   
 import java.util.Properties;
 63   
 import java.util.StringTokenizer;
 64   
 import org.apache.tools.ant.BuildEvent;
 65   
 import org.apache.tools.ant.DefaultLogger;
 66   
 import org.apache.tools.ant.Project;
 67   
 import org.apache.tools.ant.util.DateUtils;
 68   
 import org.apache.tools.ant.util.StringUtils;
 69   
 import org.apache.tools.mail.MailMessage;
 70   
 
 71   
 /**
 72   
  *  Buffers log messages from DefaultLogger, and sends an e-mail with the
 73   
  *  results. The following Project properties are used to send the mail.
 74   
  *  <ul>
 75   
  *    <li> MailLogger.mailhost [default: localhost] - Mail server to use</li>
 76   
  *    <li> MailLogger.port [default: 25] - Default port for SMTP </li>
 77   
  *    <li> MailLogger.from [required] - Mail "from" address</li>
 78   
  *    <li> MailLogger.failure.notify [default: true] - Send build failure
 79   
  *    e-mails?</li>
 80   
  *    <li> MailLogger.success.notify [default: true] - Send build success
 81   
  *    e-mails?</li>
 82   
  *    <li> MailLogger.failure.to [required if failure mail to be sent] - Address
 83   
  *    to send failure messages to</li>
 84   
  *    <li> MailLogger.success.to [required if success mail to be sent] - Address
 85   
  *    to send success messages to</li>
 86   
  *    <li> MailLogger.failure.subject [default: "Build Failure"] - Subject of
 87   
  *    failed build</li>
 88   
  *    <li> MailLogger.success.subject [default: "Build Success"] - Subject of
 89   
  *    successful build</li>
 90   
  *  </ul>
 91   
  *  These properties are set using standard Ant property setting mechanisms
 92   
  *  (&lt;property&gt;, command-line -D, etc). Ant properties can be overridden
 93   
  *  by specifying the filename of a properties file in the <i>
 94   
  *  MailLogger.properties.file property</i> . Any properties defined in that
 95   
  *  file will override Ant properties.
 96   
  *
 97   
  * @author Erik Hatcher
 98   
  *         <a href="mailto:ehatcher@apache.org">ehatcher@apache.org</a>
 99   
  */
 100   
 public class MailLogger extends DefaultLogger {
 101   
     /** Buffer in which the message is constructed prior to sending */
 102   
     private StringBuffer buffer = new StringBuffer();
 103   
 
 104   
     /**
 105   
      *  Sends an e-mail with the log results.
 106   
      *
 107   
      * @param event the build finished event
 108   
      */
 109  0
     public void buildFinished(BuildEvent event) {
 110  0
         super.buildFinished(event);
 111   
 
 112  0
         Project project = event.getProject();
 113  0
         Hashtable properties = project.getProperties();
 114   
 
 115   
         // overlay specified properties file (if any), which overrides project
 116   
         // settings
 117  0
         Properties fileProperties = new Properties();
 118  0
         String filename = (String) properties.get("MailLogger.properties.file");
 119  0
         if (filename != null) {
 120  0
             InputStream is = null;
 121  0
             try {
 122  0
                 is = new FileInputStream(filename);
 123  0
                 fileProperties.load(is);
 124   
             } catch (IOException ioe) {
 125   
                 // ignore because properties file is not required
 126   
             } finally {
 127  0
                 if (is != null) {
 128  0
                     try {
 129  0
                         is.close();
 130   
                     } catch (IOException e) {
 131   
                     }
 132   
                 }
 133   
             }
 134   
         }
 135   
 
 136  0
         for (Enumeration e = fileProperties.keys(); e.hasMoreElements();) {
 137  0
             String key = (String) e.nextElement();
 138  0
             String value = fileProperties.getProperty(key);
 139  0
             properties.put(key, project.replaceProperties(value));
 140   
         }
 141   
 
 142  0
         boolean success = (event.getException() == null);
 143  0
         String prefix = success ? "success" : "failure";
 144   
 
 145  0
         try {
 146  0
             boolean notify = Project.toBoolean(getValue(properties,
 147   
                     prefix + ".notify", "on"));
 148   
 
 149  0
             if (!notify) {
 150  0
                 return;
 151   
             }
 152   
 
 153  0
             String mailhost = getValue(properties, "mailhost", "localhost");
 154  0
             int port = Integer.parseInt(getValue(properties,"port",String.valueOf(MailMessage.DEFAULT_PORT)));
 155  0
             String from = getValue(properties, "from", null);
 156   
 
 157  0
             String toList = getValue(properties, prefix + ".to", null);
 158  0
             String subject = getValue(properties, prefix + ".subject",
 159   
                     (success) ? "Build Success" : "Build Failure");
 160   
 
 161  0
             sendMail(mailhost, port, from, toList, subject, buffer.substring(0));
 162   
         } catch (Exception e) {
 163  0
             System.out.println("MailLogger failed to send e-mail!");
 164  0
             e.printStackTrace(System.err);
 165   
         }
 166   
     }
 167   
 
 168   
 
 169   
     /**
 170   
      *  Receives and buffers log messages.
 171   
      *
 172   
      * @param message the message being logger
 173   
      */
 174  0
     protected void log(String message) {
 175  0
         buffer.append(message).append(StringUtils.LINE_SEP);
 176   
     }
 177   
 
 178   
 
 179   
     /**
 180   
      *  Gets the value of a property.
 181   
      *
 182   
      * @param  properties     Properties to obtain value from
 183   
      * @param  name           suffix of property name. "MailLogger." will be
 184   
      *      prepended internally.
 185   
      * @param  defaultValue   value returned if not present in the properties.
 186   
      *      Set to null to make required.
 187   
      * @return                The value of the property, or default value.
 188   
      * @exception  Exception  thrown if no default value is specified and the
 189   
      *      property is not present in properties.
 190   
      */
 191  0
     private String getValue(Hashtable properties, String name,
 192   
                             String defaultValue) throws Exception {
 193  0
         String propertyName = "MailLogger." + name;
 194  0
         String value = (String) properties.get(propertyName);
 195   
 
 196  0
         if (value == null) {
 197  0
             value = defaultValue;
 198   
         }
 199   
 
 200  0
         if (value == null) {
 201  0
             throw new Exception("Missing required parameter: " + propertyName);
 202   
         }
 203   
 
 204  0
         return value;
 205   
     }
 206   
 
 207   
 
 208   
     /**
 209   
      *  Send the mail
 210   
      *
 211   
      * @param  mailhost         mail server
 212   
      * @param  port             mail server port number
 213   
      * @param  from             from address
 214   
      * @param  toList           comma-separated recipient list
 215   
      * @param  subject          mail subject
 216   
      * @param  message          mail body
 217   
      * @exception  IOException  thrown if sending message fails
 218   
      */
 219  0
     private void sendMail(String mailhost, int port, String from, String toList,
 220   
                           String subject, String message) throws IOException {
 221  0
         MailMessage mailMessage = new MailMessage(mailhost, port);
 222  0
         mailMessage.setHeader("Date", DateUtils.getDateForHeader());
 223   
 
 224  0
         mailMessage.from(from);
 225   
 
 226  0
         StringTokenizer t = new StringTokenizer(toList, ", ", false);
 227  0
         while (t.hasMoreTokens()) {
 228  0
             mailMessage.to(t.nextToken());
 229   
         }
 230   
 
 231  0
         mailMessage.setSubject(subject);
 232   
 
 233  0
         PrintStream ps = mailMessage.getPrintStream();
 234  0
         ps.println(message);
 235   
 
 236  0
         mailMessage.sendAndClose();
 237   
     }
 238   
 }
 239   
 
 240   
 
 241