Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 299   Methods: 11
NCLOC: 111   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NetCommand.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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   
 /*
 55   
  *  build notes
 56   
  *  The reference CD to listen to while editing this file is
 57   
  *  Underworld Everything, Everything
 58   
  *  variable naming policy from Fowler's refactoring book.
 59   
  */
 60   
 // place below the optional ant tasks package
 61   
 
 62   
 package org.apache.tools.ant.taskdefs.optional.dotnet;
 63   
 
 64   
 // imports
 65   
 
 66   
 import java.io.File;
 67   
 import java.io.IOException;
 68   
 import java.util.Hashtable;
 69   
 
 70   
 import org.apache.tools.ant.BuildException;
 71   
 import org.apache.tools.ant.Project;
 72   
 import org.apache.tools.ant.Task;
 73   
 import org.apache.tools.ant.DirectoryScanner;
 74   
 import org.apache.tools.ant.taskdefs.Execute;
 75   
 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
 76   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 77   
 import org.apache.tools.ant.types.Commandline;
 78   
 
 79   
 /**
 80   
  *  This is a helper class to spawn net commands out. In its initial form it
 81   
  *  contains no .net specifics, just contains all the command line/exe
 82   
  *  construction stuff. However, it may be handy in future to have a means of
 83   
  *  setting the path to point to the dotnet bin directory; in which case the
 84   
  *  shared code should go in here.
 85   
  *
 86   
  *@author     Steve Loughran steve_l@iseran.com
 87   
  *@version    0.5
 88   
  */
 89   
 
 90   
 public class NetCommand {
 91   
 
 92   
     /**
 93   
      *  owner project
 94   
      */
 95   
     protected Task owner;
 96   
 
 97   
     /**
 98   
      *  executabe
 99   
      */
 100   
     protected Execute executable;
 101   
 
 102   
     /**
 103   
      *  what is the command line
 104   
      */
 105   
     protected Commandline commandLine;
 106   
 
 107   
     /**
 108   
      *  title of the command
 109   
      */
 110   
     protected String title;
 111   
 
 112   
     /**
 113   
      *  actual program to invoke
 114   
      */
 115   
     protected String program;
 116   
 
 117   
     /**
 118   
      *  trace flag
 119   
      */
 120   
     protected boolean traceCommandLine = false;
 121   
 
 122   
     /**
 123   
      *  flag to control action on execution trouble
 124   
      */
 125   
     protected boolean failOnError;
 126   
 
 127   
 
 128   
     /**
 129   
      *  constructor
 130   
      *
 131   
      *@param  title        (for logging/errors)
 132   
      *@param  owner        owner task
 133   
      *@param  program      app we are to run
 134   
      */
 135   
 
 136  0
     public NetCommand(Task owner, String title, String program) {
 137  0
         this.owner = owner;
 138  0
         this.title = title;
 139  0
         this.program = program;
 140  0
         commandLine = new Commandline();
 141  0
         commandLine.setExecutable(program);
 142  0
         prepareExecutor();
 143   
     }
 144   
 
 145   
 
 146   
     /**
 147   
      *  turn tracing on or off
 148   
      *
 149   
      *@param  b  trace flag
 150   
      */
 151  0
     public void setTraceCommandLine(boolean b) {
 152  0
         traceCommandLine = b;
 153   
     }
 154   
 
 155   
 
 156   
     /**
 157   
      *  set fail on error flag
 158   
      *
 159   
      *@param  b  fail flag -set to true to cause an exception to be raised if
 160   
      *      the return value != 0
 161   
      */
 162  0
     public void setFailOnError(boolean b) {
 163  0
         failOnError = b;
 164   
     }
 165   
 
 166   
 
 167   
     /**
 168   
      *  query fail on error flag
 169   
      *
 170   
      *@return    The failFailOnError value
 171   
      */
 172  0
     public boolean getFailFailOnError() {
 173  0
         return failOnError;
 174   
     }
 175   
 
 176   
 
 177   
     /**
 178   
      *  verbose text log
 179   
      *
 180   
      *@param  msg  string to add to log iff verbose is defined for the build
 181   
      */
 182  0
     protected void logVerbose(String msg) {
 183  0
         owner.getProject().log(msg, Project.MSG_VERBOSE);
 184   
     }
 185   
 
 186   
 
 187   
     /**
 188   
      *  error text log
 189   
      *
 190   
      *@param  msg  message to display as an error
 191   
      */
 192  0
     protected void logError(String msg) {
 193  0
         owner.getProject().log(msg, Project.MSG_ERR);
 194   
     }
 195   
 
 196   
 
 197   
     /**
 198   
      *  add an argument to a command line; do nothing if the arg is null or
 199   
      *  empty string
 200   
      *
 201   
      *@param  argument  The feature to be added to the Argument attribute
 202   
      */
 203  0
     public void addArgument(String argument) {
 204  0
         if (argument != null && argument.length() != 0) {
 205  0
             commandLine.createArgument().setValue(argument);
 206   
         }
 207   
     }
 208   
 
 209  0
     public void addArgument(String argument1, String argument2) {
 210  0
         if (argument2 != null && argument2.length() != 0) {
 211  0
             commandLine.createArgument().setValue(argument1 + argument2);
 212   
         }
 213   
     }    
 214   
 
 215   
     /**
 216   
      *  set up the command sequence..
 217   
      */
 218  0
     protected void prepareExecutor() {
 219   
         // default directory to the project's base directory
 220  0
         if (owner == null) {
 221  0
             throw new RuntimeException("no owner"); 
 222   
         }
 223  0
         if (owner.getProject() == null) {
 224  0
             throw new RuntimeException("Owner has no project"); 
 225   
         }
 226  0
         File dir = owner.getProject().getBaseDir();
 227  0
         ExecuteStreamHandler handler = new LogStreamHandler(owner,
 228   
                 Project.MSG_INFO, Project.MSG_WARN);
 229  0
         executable = new Execute(handler, null);
 230  0
         executable.setAntRun(owner.getProject());
 231  0
         executable.setWorkingDirectory(dir);
 232   
     }
 233   
 
 234   
 
 235   
     /**
 236   
      *  Run the command using the given Execute instance.
 237   
      *
 238   
      *@exception  BuildException  iff something goes wrong and the
 239   
      *      failOnError flag is true
 240   
      */
 241  0
     public void runCommand()
 242   
              throws BuildException {
 243  0
         int err = -1;
 244   
         // assume the worst
 245  0
         try {
 246  0
             if (traceCommandLine) {
 247  0
                 owner.log(commandLine.describeCommand());
 248   
             } else {
 249   
                 //in verbose mode we always log stuff
 250  0
                 logVerbose(commandLine.describeCommand());
 251   
             }
 252  0
             executable.setCommandline(commandLine.getCommandline());
 253  0
             err = executable.execute();
 254  0
             if (err != 0) {
 255  0
                 if (failOnError) {
 256  0
                     throw new BuildException(title + " returned: " + err, owner.getLocation());
 257   
                 } else {
 258  0
                     owner.log(title + "  Result: " + err, Project.MSG_ERR);
 259   
                 }
 260   
             }
 261   
         } catch (IOException e) {
 262  0
             throw new BuildException(title + " failed: " + e, e, owner.getLocation());
 263   
         }
 264   
     }
 265   
 
 266   
 
 267   
     /**
 268   
      * scan through one fileset for files to include
 269   
      * @param scanner
 270   
      * @param filesToBuild
 271   
      * @param outputTimestamp timestamp to compare against
 272   
      * @return #of files out of date
 273   
      * @todo: should FAT granularity be included here?
 274   
      */
 275  0
     public int scanOneFileset(DirectoryScanner scanner, Hashtable filesToBuild,
 276   
                                         long outputTimestamp) {
 277  0
         int filesOutOfDate = 0;
 278  0
         String[] dependencies = scanner.getIncludedFiles();
 279  0
         File base = scanner.getBasedir();
 280   
         //add to the list
 281  0
         for (int i = 0; i < dependencies.length; i++) {
 282  0
             File targetFile = new File(base, dependencies[i]);
 283  0
             if (filesToBuild.get(targetFile) == null) {
 284  0
                 filesToBuild.put(targetFile, targetFile);
 285  0
                 if (targetFile.lastModified() > outputTimestamp) {
 286  0
                     filesOutOfDate++;
 287  0
                     owner.log(targetFile.toString() + " is out of date",
 288   
                             Project.MSG_VERBOSE);
 289   
                 } else {
 290  0
                     owner.log(targetFile.toString(),
 291   
                             Project.MSG_VERBOSE);
 292   
                 }
 293   
             }
 294   
         }
 295  0
         return filesOutOfDate;
 296   
     }
 297   
 }
 298   
 
 299