Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 529   Methods: 23
NCLOC: 234   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
EmailTask.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2002 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.taskdefs.email;
 55   
 
 56   
 // Ant imports
 57   
 
 58   
 import java.io.File;
 59   
 import java.util.Enumeration;
 60   
 import java.util.StringTokenizer;
 61   
 import java.util.Vector;
 62   
 import org.apache.tools.ant.BuildException;
 63   
 import org.apache.tools.ant.DirectoryScanner;
 64   
 import org.apache.tools.ant.Project;
 65   
 import org.apache.tools.ant.Task;
 66   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 67   
 import org.apache.tools.ant.types.FileSet;
 68   
 
 69   
 /**
 70   
  * A task to send SMTP email. This is a refactoring of the SendMail and
 71   
  * MimeMail tasks such that both are within a single task.
 72   
  *
 73   
  * @author Magesh Umasankar
 74   
  * @author glenn_twiggs@bmc.com
 75   
  * @author steve_l@iseran.com steve loughran
 76   
  * @author ehatcher@apache.org Erik Hatcher
 77   
  * @author paulo.gaspar@krankikom.de Paulo Gaspar
 78   
  * @author roxspring@imapmail.org Rob Oxspring
 79   
  * @since Ant 1.5
 80   
  * @ant.task name="mail" category="network"
 81   
  */
 82   
 public class EmailTask
 83   
      extends Task {
 84   
     /** Constant to show that the best available mailer should be used.  */
 85   
     public static final String AUTO = "auto";
 86   
     /** Constant to allow the Mime mailer to be requested  */
 87   
     public static final String MIME = "mime";
 88   
     /** Constant to allow the UU mailer to be requested  */
 89   
     public static final String UU = "uu";
 90   
     /** Constant to allow the plaintext mailer to be requested  */
 91   
     public static final String PLAIN = "plain";
 92   
 
 93   
 
 94   
     /**
 95   
      * Enumerates the encoding constants
 96   
      */
 97   
     public static class Encoding extends EnumeratedAttribute {
 98   
         /**
 99   
          * finds the valid encoding values
 100   
          *
 101   
          * @return a list of valid entries
 102   
          */
 103  0
         public String[] getValues() {
 104  0
             return new String[]
 105   
                 {AUTO, MIME, UU, PLAIN};
 106   
         }
 107   
     }
 108   
 
 109   
 
 110   
     private String encoding = AUTO;
 111   
     /** host running SMTP  */
 112   
     private String host = "localhost";
 113   
     private int port = 25;
 114   
     /** subject field  */
 115   
     private String subject = null;
 116   
     /** any text  */
 117   
     private Message message = null;
 118   
     /** failure flag */
 119   
     private boolean failOnError = true;
 120   
     private boolean includeFileNames = false;
 121   
     private String messageMimeType = null;
 122   
 
 123   
     /** sender  */
 124   
     private EmailAddress from = null;
 125   
     /** TO recipients  */
 126   
     private Vector toList = new Vector();
 127   
     /** CC (Carbon Copy) recipients  */
 128   
     private Vector ccList = new Vector();
 129   
     /** BCC (Blind Carbon Copy) recipients  */
 130   
     private Vector bccList = new Vector();
 131   
 
 132   
     /** file list  */
 133   
     private Vector files = new Vector();
 134   
     private Vector filesets = new Vector();
 135   
 
 136   
 
 137   
     /**
 138   
      * Allows the build writer to choose the preferred encoding method
 139   
      *
 140   
      * @param encoding The encoding (one of AUTO,MIME,UU,PLAIN)
 141   
      */
 142  0
     public void setEncoding(Encoding encoding) {
 143  0
         this.encoding = encoding.getValue();
 144   
     }
 145   
 
 146   
 
 147   
     /**
 148   
      * Sets the mail server port
 149   
      *
 150   
      * @param port The port to use
 151   
      */
 152  0
     public void setMailport(int port) {
 153  0
         this.port = port;
 154   
     }
 155   
 
 156   
 
 157   
     /**
 158   
      * Sets the host
 159   
      *
 160   
      * @param host The host to connect to
 161   
      */
 162  0
     public void setMailhost(String host) {
 163  0
         this.host = host;
 164   
     }
 165   
 
 166   
 
 167   
     /**
 168   
      * Sets the subject line of the email
 169   
      *
 170   
      * @param subject Subject of this email.
 171   
      */
 172  0
     public void setSubject(String subject) {
 173  0
         this.subject = subject;
 174   
     }
 175   
 
 176   
 
 177   
     /**
 178   
      * Shorthand method to set the message
 179   
      *
 180   
      * @param message Message body of this email.
 181   
      */
 182  0
     public void setMessage(String message) {
 183  0
         if (this.message != null) {
 184  0
             throw new BuildException("Only one message can be sent in an "
 185   
                  + "email");
 186   
         }
 187   
 
 188  0
         this.message = new Message(message);
 189  0
         this.message.setProject(getProject());
 190   
     }
 191   
 
 192   
 
 193   
     /**
 194   
      * Shorthand method to set the message from a file
 195   
      *
 196   
      * @param file The file from which to take the message
 197   
      */
 198  0
     public void setMessageFile(File file) {
 199  0
         if (this.message != null) {
 200  0
             throw new BuildException("Only one message can be sent in an "
 201   
                  + "email");
 202   
         }
 203   
 
 204  0
         this.message = new Message(file);
 205  0
         this.message.setProject(getProject());
 206   
     }
 207   
 
 208   
 
 209   
     /**
 210   
      * Shorthand method to set type of the text message, text/plain by default
 211   
      * but text/html or text/xml is quite feasible.
 212   
      *
 213   
      * @param type The new MessageMimeType value
 214   
      */
 215  0
     public void setMessageMimeType(String type) {
 216  0
         this.messageMimeType = type;
 217   
     }
 218   
 
 219   
 
 220   
     /**
 221   
      * Add a message elemnt
 222   
      *
 223   
      * @param message The message object
 224   
      * @throws BuildException if a message has already been added
 225   
      */
 226  0
     public void addMessage(Message message)
 227   
          throws BuildException {
 228  0
         if (this.message != null) {
 229  0
             throw new BuildException("Only one message can be sent in an "
 230   
                  + "email");
 231   
         }
 232   
 
 233  0
         this.message = message;
 234   
     }
 235   
 
 236   
 
 237   
     /**
 238   
      * Adds a from address element
 239   
      *
 240   
      * @param address The address to send from
 241   
      */
 242  0
     public void addFrom(EmailAddress address) {
 243  0
         if (this.from != null) {
 244  0
             throw new BuildException("Emails can only be from one address");
 245   
         }
 246   
 
 247  0
         this.from = address;
 248   
     }
 249   
 
 250   
 
 251   
     /**
 252   
      * Shorthand to set the from address element
 253   
      *
 254   
      * @param address The address to send mail from
 255   
      */
 256  0
     public void setFrom(String address) {
 257  0
         if (this.from != null) {
 258  0
             throw new BuildException("Emails can only be from one address");
 259   
         }
 260   
 
 261  0
         this.from = new EmailAddress(address);
 262   
     }
 263   
 
 264   
 
 265   
     /**
 266   
      * Adds a to address element
 267   
      *
 268   
      * @param address An email address
 269   
      */
 270  0
     public void addTo(EmailAddress address) {
 271  0
         toList.addElement(address);
 272   
     }
 273   
 
 274   
 
 275   
     /**
 276   
      * Adds "to" address elements
 277   
      *
 278   
      * @param list Comma separated list of addresses
 279   
      */
 280  0
     public void setToList(String list) {
 281  0
         StringTokenizer tokens = new StringTokenizer(list, ",");
 282   
 
 283  0
         while (tokens.hasMoreTokens()) {
 284  0
             toList.addElement(new EmailAddress(tokens.nextToken()));
 285   
         }
 286   
     }
 287   
 
 288   
 
 289   
     /**
 290   
      * Adds "cc" address element
 291   
      *
 292   
      * @param address The email address
 293   
      */
 294  0
     public void addCc(EmailAddress address) {
 295  0
         ccList.addElement(address);
 296   
     }
 297   
 
 298   
 
 299   
     /**
 300   
      * Adds "cc" address elements
 301   
      *
 302   
      * @param list Comma separated list of addresses
 303   
      */
 304  0
     public void setCcList(String list) {
 305  0
         StringTokenizer tokens = new StringTokenizer(list, ",");
 306   
 
 307  0
         while (tokens.hasMoreTokens()) {
 308  0
             ccList.addElement(new EmailAddress(tokens.nextToken()));
 309   
         }
 310   
     }
 311   
 
 312   
 
 313   
     /**
 314   
      * Adds "bcc" address elements
 315   
      *
 316   
      * @param address The email address
 317   
      */
 318  0
     public void addBcc(EmailAddress address) {
 319  0
         bccList.addElement(address);
 320   
     }
 321   
 
 322   
 
 323   
     /**
 324   
      * Adds "bcc" address elements
 325   
      *
 326   
      * @param list comma separated list of addresses
 327   
      */
 328  0
     public void setBccList(String list) {
 329  0
         StringTokenizer tokens = new StringTokenizer(list, ",");
 330   
 
 331  0
         while (tokens.hasMoreTokens()) {
 332  0
             bccList.addElement(new EmailAddress(tokens.nextToken()));
 333   
         }
 334   
     }
 335   
 
 336   
 
 337   
     /**
 338   
      * Indicates whether BuildExceptions should be passed back to the core
 339   
      *
 340   
      * @param failOnError The new FailOnError value
 341   
      */
 342  0
     public void setFailOnError(boolean failOnError) {
 343  0
         this.failOnError = failOnError;
 344   
     }
 345   
 
 346   
 
 347   
     /**
 348   
      * Adds a list of files to be attached
 349   
      *
 350   
      * @param filenames Comma separated list of files
 351   
      */
 352  0
     public void setFiles(String filenames) {
 353  0
         StringTokenizer t = new StringTokenizer(filenames, ", ");
 354   
 
 355  0
         while (t.hasMoreTokens()) {
 356  0
             files.addElement(getProject().resolveFile(t.nextToken()));
 357   
         }
 358   
     }
 359   
 
 360   
 
 361   
     /**
 362   
      * Adds a set of files (nested fileset attribute).
 363   
      *
 364   
      * @param fs The fileset
 365   
      */
 366  0
     public void addFileset(FileSet fs) {
 367  0
         filesets.addElement(fs);
 368   
     }
 369   
 
 370   
 
 371   
     /**
 372   
      * Sets Includefilenames attribute
 373   
      *
 374   
      * @param includeFileNames Whether to include filenames in the text of the
 375   
      *      message
 376   
      */
 377  0
     public void setIncludefilenames(boolean includeFileNames) {
 378  0
         this.includeFileNames = includeFileNames;
 379   
     }
 380   
 
 381   
 
 382   
     /**
 383   
      * Identifies whether file names should be included
 384   
      *
 385   
      * @return Identifies whether file names should be included
 386   
      */
 387  0
     public boolean getIncludeFileNames() {
 388  0
         return includeFileNames;
 389   
     }
 390   
 
 391   
 
 392   
     /** Sends an email  */
 393  0
     public void execute() {
 394  0
         Message savedMessage = message;
 395  0
         Vector savedFiles = (Vector) files.clone();
 396   
 
 397  0
         try {
 398  0
             Mailer mailer = null;
 399   
 
 400   
             // prepare for the auto select mechanism
 401  0
             boolean autoFound = false;
 402   
 
 403   
             // try MIME format
 404  0
             if (encoding.equals(MIME)
 405   
                  || (encoding.equals(AUTO) && !autoFound)) {
 406  0
                 try {
 407  0
                     mailer =
 408   
                         (Mailer) Class.forName("org.apache.tools.ant.taskdefs.email.MimeMailer")
 409   
                         .newInstance();
 410  0
                     autoFound = true;
 411  0
                     log("Using MIME mail", Project.MSG_VERBOSE);
 412   
                 } catch (Throwable e) {
 413  0
                     log("Failed to initialise MIME mail", Project.MSG_WARN);
 414   
                 }
 415   
             }
 416   
 
 417   
             // try UU format
 418  0
             if (encoding.equals(UU)
 419   
                  || (encoding.equals(AUTO) && !autoFound)) {
 420  0
                 try {
 421  0
                     mailer =
 422   
                         (Mailer) Class.forName("org.apache.tools.ant.taskdefs.email.UUMailer")
 423   
                         .newInstance();
 424  0
                     autoFound = true;
 425  0
                     log("Using UU mail", Project.MSG_VERBOSE);
 426   
                 } catch (Throwable e) {
 427  0
                     log("Failed to initialise UU mail", Project.MSG_WARN);
 428   
                 }
 429   
             }
 430   
 
 431   
             // try plain format
 432  0
             if (encoding.equals(PLAIN)
 433   
                  || (encoding.equals(AUTO) && !autoFound)) {
 434  0
                 mailer = new PlainMailer();
 435  0
                 autoFound = true;
 436  0
                 log("Using plain mail", Project.MSG_VERBOSE);
 437   
             }
 438   
 
 439   
             // a valid mailer must be present by now
 440  0
             if (mailer == null) {
 441  0
                 throw new BuildException("Failed to initialise encoding: "
 442   
                      + encoding);
 443   
             }
 444   
 
 445   
             // a valid message is required
 446  0
             if (message == null) {
 447  0
                 message = new Message();
 448  0
                 message.setProject(getProject());
 449   
             }
 450   
 
 451   
             // an address to send from is required
 452  0
             if (from == null || from.getAddress() == null) {
 453  0
                 throw new BuildException("A from element is required");
 454   
             }
 455   
 
 456   
             // at least one address to send to/cc/bcc is required
 457  0
             if (toList.isEmpty() && ccList.isEmpty() && bccList.isEmpty()) {
 458  0
                 throw new BuildException("At least one of to,cc or bcc must "
 459   
                      + "be supplied");
 460   
             }
 461   
 
 462   
             // set the mimetype if not done already (and required)
 463  0
             if (messageMimeType != null) {
 464  0
                 if (message.isMimeTypeSpecified()) {
 465  0
                     throw new BuildException("The mime type can only be "
 466   
                          + "specified in one location");
 467   
                 } else {
 468  0
                     message.setMimeType(messageMimeType);
 469   
                 }
 470   
             }
 471   
 
 472   
             // identify which files should be attached
 473  0
             Enumeration e = filesets.elements();
 474   
 
 475  0
             while (e.hasMoreElements()) {
 476  0
                 FileSet fs = (FileSet) e.nextElement();
 477   
 
 478  0
                 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 479  0
                 String[] includedFiles = ds.getIncludedFiles();
 480  0
                 File baseDir = ds.getBasedir();
 481   
 
 482  0
                 for (int j = 0; j < includedFiles.length; ++j) {
 483  0
                     File file = new File(baseDir, includedFiles[j]);
 484   
 
 485  0
                     files.addElement(file);
 486   
                 }
 487   
             }
 488   
 
 489   
             // let the user know what's going to happen
 490  0
             log("Sending email: " + subject, Project.MSG_INFO);
 491  0
             log("From " + from, Project.MSG_VERBOSE);
 492  0
             log("To " + toList, Project.MSG_VERBOSE);
 493  0
             log("Cc " + ccList, Project.MSG_VERBOSE);
 494  0
             log("Bcc " + bccList, Project.MSG_VERBOSE);
 495   
 
 496   
             // pass the params to the mailer
 497  0
             mailer.setHost(host);
 498  0
             mailer.setPort(port);
 499  0
             mailer.setMessage(message);
 500  0
             mailer.setFrom(from);
 501  0
             mailer.setToList(toList);
 502  0
             mailer.setCcList(ccList);
 503  0
             mailer.setBccList(bccList);
 504  0
             mailer.setFiles(files);
 505  0
             mailer.setSubject(subject);
 506  0
             mailer.setTask(this);
 507  0
             mailer.setIncludeFileNames(includeFileNames);
 508   
 
 509   
             // send the email
 510  0
             mailer.send();
 511   
 
 512   
             // let the user know what happened
 513  0
             int count = files.size();
 514   
 
 515  0
             log("Sent email with " + count + " attachment"
 516   
                  + (count == 1 ? "" : "s"), Project.MSG_INFO);
 517   
         } catch (BuildException e) {
 518  0
             log("Failed to send email", Project.MSG_WARN);
 519  0
             if (failOnError) {
 520  0
                 throw e;
 521   
             }
 522   
         } finally {
 523  0
             message = savedMessage;
 524  0
             files = savedFiles;
 525   
         }
 526   
     }
 527   
 }
 528   
 
 529