Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 383   Methods: 21
NCLOC: 197   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractMetamataTask.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-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.taskdefs.optional.metamata;
 55   
 
 56   
 import java.io.File;
 57   
 import java.io.FileWriter;
 58   
 import java.io.IOException;
 59   
 import java.io.PrintWriter;
 60   
 import java.util.Enumeration;
 61   
 import java.util.Hashtable;
 62   
 import java.util.Vector;
 63   
 import org.apache.tools.ant.BuildException;
 64   
 import org.apache.tools.ant.DirectoryScanner;
 65   
 import org.apache.tools.ant.Project;
 66   
 import org.apache.tools.ant.Task;
 67   
 import org.apache.tools.ant.taskdefs.Execute;
 68   
 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
 69   
 import org.apache.tools.ant.types.Commandline;
 70   
 import org.apache.tools.ant.types.CommandlineJava;
 71   
 import org.apache.tools.ant.types.FileSet;
 72   
 import org.apache.tools.ant.types.Path;
 73   
 import org.apache.tools.ant.util.FileUtils;
 74   
 import org.apache.tools.ant.util.JavaEnvUtils;
 75   
 
 76   
 /**
 77   
  * Somewhat abstract framework to be used for other metama 2.0 tasks.
 78   
  * This should include, audit, metrics, cover and mparse.
 79   
  *
 80   
  * For more information, visit the website at
 81   
  * <a href="http://www.metamata.com">www.metamata.com</a>
 82   
  *
 83   
  * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
 84   
  */
 85   
 public abstract class AbstractMetamataTask extends Task {
 86   
 
 87   
     /**
 88   
      * The user classpath to be provided. It matches the -classpath of the
 89   
      * command line. The classpath must includes both the <tt>.class</tt> and the
 90   
      * <tt>.java</tt> files for accurate audit.
 91   
      */
 92   
     protected Path classPath = null;
 93   
 
 94   
     /** the path to the source file */
 95   
     protected Path sourcePath = null;
 96   
 
 97   
     /**
 98   
      * Metamata home directory. It will be passed as a <tt>metamata.home</tt> property
 99   
      * and should normally matches the environment property <tt>META_HOME</tt>
 100   
      * set by the Metamata installer.
 101   
      */
 102   
     protected File metamataHome = null;
 103   
 
 104   
     /** the command line used to run MAudit */
 105   
     protected CommandlineJava cmdl = new CommandlineJava();
 106   
 
 107   
     /** the set of files to be audited */
 108   
     protected Vector fileSets = new Vector();
 109   
 
 110   
     /** the options file where are stored the command line options */
 111   
     protected File optionsFile = null;
 112   
 
 113   
     // this is used to keep track of which files were included. It will
 114   
     // be set when calling scanFileSets();
 115   
     protected Hashtable includedFiles = null;
 116   
 
 117  0
     public AbstractMetamataTask() {
 118   
     }
 119   
 
 120   
     /** initialize the task with the classname of the task to run */
 121  0
     protected AbstractMetamataTask(String className) {
 122  0
         cmdl.setVm(JavaEnvUtils.getJreExecutable("java"));
 123  0
         cmdl.setClassname(className);
 124   
     }
 125   
 
 126   
     /**
 127   
      * the metamata.home property to run all tasks.
 128   
      * @ant.attribute ignore="true"
 129   
      */
 130  0
     public void setHome(final File value) {
 131  0
         this.metamataHome = value;
 132   
     }
 133   
 
 134   
     /**
 135   
      * The home directory containing the Metamata distribution; required
 136   
      */
 137  0
     public void setMetamatahome(final File value) {
 138  0
         setHome(value);
 139   
     }
 140   
 
 141   
     /**
 142   
      * Sets the class path (also source path unless one explicitly set).
 143   
      * Overrides METAPATH/CLASSPATH environment variables.
 144   
      */
 145  0
     public Path createClasspath() {
 146  0
         if (classPath == null) {
 147  0
             classPath = new Path(getProject());
 148   
         }
 149  0
         return classPath;
 150   
     }
 151   
 
 152   
     /**
 153   
      * Sets the source path. 
 154   
      * Overrides the SOURCEPATH environment variable. 
 155   
      */
 156  0
     public Path createSourcepath() {
 157  0
         if (sourcePath == null) {
 158  0
             sourcePath = new Path(getProject());
 159   
         }
 160  0
         return sourcePath;
 161   
     }
 162   
 
 163   
     /**
 164   
      * Additional optional parameters to pass to the JVM. 
 165   
      * You can avoid using the  <code>&lt;jvmarg&gt;</code> by adding these empty 
 166   
      * entries to <code>metamata.properties</code> located at <code>${metamata.home}/bin</code>
 167   
      *
 168   
      * <pre>metamata.classpath=
 169   
      * metamata.sourcepath=
 170   
      * metamata.baseclasspath=
 171   
      * </pre>
 172   
      */
 173  0
     public Commandline.Argument createJvmarg() {
 174  0
         return cmdl.createVmArgument();
 175   
     }
 176   
 
 177   
     /**
 178   
      * Set the maximum memory for the JVM; optional.
 179   
      * -mx or -Xmx depending on VM version 
 180   
      */
 181  0
     public void setMaxmemory(String max) {
 182  0
         cmdl.setMaxmemory(max);
 183   
     }
 184   
 
 185   
 
 186   
     /** 
 187   
      * The java files or directory to audit.
 188   
      * Whatever the filter is, only the files that end 
 189   
      * with .java will be included for processing. 
 190   
      * Note that the base directory used for the fileset 
 191   
      * MUST be the root of the source files otherwise package names
 192   
      * deduced from the file path will be incorrect. 
 193   
      */
 194  0
     public void addFileSet(FileSet fs) {
 195  0
         fileSets.addElement(fs);
 196   
     }
 197   
 
 198   
     /** execute the command line */
 199  0
     public void execute() throws BuildException {
 200  0
         try {
 201  0
             setUp();
 202  0
             ExecuteStreamHandler handler = createStreamHandler();
 203  0
             execute0(handler);
 204   
         } finally {
 205  0
             cleanUp();
 206   
         }
 207   
     }
 208   
 
 209   
     /** check the options and build the command line */
 210  0
     protected void setUp() throws BuildException {
 211  0
         checkOptions();
 212   
 
 213   
         // set the classpath as the jar file
 214  0
         File jar = getMetamataJar(metamataHome);
 215  0
         final Path classPath = cmdl.createClasspath(getProject());
 216  0
         classPath.createPathElement().setLocation(jar);
 217   
 
 218   
         // set the metamata.home property
 219  0
         final Commandline.Argument vmArgs = cmdl.createVmArgument();
 220  0
         vmArgs.setValue("-Dmetamata.home=" + metamataHome.getAbsolutePath());
 221   
 
 222   
         // retrieve all the files we want to scan
 223  0
         includedFiles = scanSources(new Hashtable());
 224   
         //String[] entries = sourcePath.list();
 225   
         //includedFiles = scanSources(new Hashtable(), entries);
 226  0
         log(includedFiles.size() + " files added for audit", Project.MSG_VERBOSE);
 227   
 
 228   
         // write all the options to a temp file and use it ro run the process
 229  0
         Vector options = getOptions();
 230  0
         optionsFile = createTmpFile();
 231  0
         generateOptionsFile(optionsFile, options);
 232  0
         Commandline.Argument args = cmdl.createArgument();
 233  0
         args.setLine("-arguments " + optionsFile.getAbsolutePath());
 234   
     }
 235   
 
 236   
     /**
 237   
      * create a stream handler that will be used to get the output since
 238   
      * metamata tools do not report with convenient files such as XML.
 239   
      */
 240   
     protected abstract ExecuteStreamHandler createStreamHandler();
 241   
 
 242   
 
 243   
     /** execute the process with a specific handler */
 244  0
     protected void execute0(ExecuteStreamHandler handler) throws BuildException {
 245  0
         final Execute process = new Execute(handler);
 246  0
         log(cmdl.describeCommand(), Project.MSG_VERBOSE);
 247  0
         process.setCommandline(cmdl.getCommandline());
 248  0
         try {
 249  0
             if (process.execute() != 0) {
 250  0
                 throw new BuildException("Metamata task failed.");
 251   
             }
 252   
         } catch (IOException e) {
 253  0
             throw new BuildException("Failed to launch Metamata task", e);
 254   
         }
 255   
     }
 256   
 
 257   
     /** clean up all the mess that we did with temporary objects */
 258  0
     protected void cleanUp() {
 259  0
         if (optionsFile != null) {
 260  0
             optionsFile.delete();
 261  0
             optionsFile = null;
 262   
         }
 263   
     }
 264   
 
 265   
     /** return the location of the jar file used to run */
 266  0
     protected final File getMetamataJar(File home) {
 267  0
         return new File(home, "lib/metamata.jar");
 268   
     }
 269   
 
 270   
     /** validate options set */
 271  0
     protected void checkOptions() throws BuildException {
 272   
         // do some validation first
 273  0
         if (metamataHome == null || !metamataHome.exists()) {
 274  0
             throw new BuildException("'home' must point to Metamata home directory.");
 275   
         }
 276  0
         File jar = getMetamataJar(metamataHome);
 277  0
         if (!jar.exists()) {
 278  0
             throw new BuildException(jar + " does not exist. Check your metamata installation.");
 279   
         }
 280   
     }
 281   
 
 282   
     /** return all options of the command line as string elements */
 283   
     protected abstract Vector getOptions();
 284   
 
 285   
 
 286  0
     protected void generateOptionsFile(File tofile, Vector options) throws BuildException {
 287  0
         FileWriter fw = null;
 288  0
         try {
 289  0
             fw = new FileWriter(tofile);
 290  0
             PrintWriter pw = new PrintWriter(fw);
 291  0
             final int size = options.size();
 292  0
             for (int i = 0; i < size; i++) {
 293  0
                 pw.println(options.elementAt(i));
 294   
             }
 295  0
             pw.flush();
 296   
         } catch (IOException e) {
 297  0
             throw new BuildException("Error while writing options file " + tofile, e);
 298   
         } finally {
 299  0
             if (fw != null) {
 300  0
                 try {
 301  0
                     fw.close();
 302   
                 } catch (IOException ignored) {
 303   
                 }
 304   
             }
 305   
         }
 306   
     }
 307   
 
 308   
 
 309  0
     protected Hashtable getFileMapping() {
 310  0
         return includedFiles;
 311   
     }
 312   
 
 313   
     /**
 314   
      * convenient method for JDK 1.1. Will copy all elements from src to dest
 315   
      */
 316  0
     protected static final void addAllVector(Vector dest, Enumeration files) {
 317  0
         while (files.hasMoreElements()) {
 318  0
             dest.addElement(files.nextElement());
 319   
         }
 320   
     }
 321   
 
 322  0
     protected final File createTmpFile() {
 323  0
         return FileUtils.newFileUtils().createTempFile("metamata", ".tmp", getProject().getBaseDir());
 324   
     }
 325   
 
 326   
     /**
 327   
      * @return the list of .java files (as their absolute path) that should
 328   
      *         be audited.
 329   
      */
 330   
 
 331  0
     protected Hashtable scanSources(Hashtable map) {
 332  0
         Hashtable files = new Hashtable();
 333  0
         for (int i = 0; i < fileSets.size(); i++) {
 334  0
             FileSet fs = (FileSet) fileSets.elementAt(i);
 335  0
             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 336  0
             ds.scan();
 337  0
             String[] f = ds.getIncludedFiles();
 338  0
             log(i + ") Adding " + f.length + " files from directory " + ds.getBasedir(), Project.MSG_VERBOSE);
 339  0
             for (int j = 0; j < f.length; j++) {
 340  0
                 String pathname = f[j];
 341  0
                 if (pathname.endsWith(".java")) {
 342  0
                     File file = new File(ds.getBasedir(), pathname);
 343   
 //                  file = project.resolveFile(file.getAbsolutePath());
 344  0
                     String classname = pathname.substring(0, pathname.length() - ".java".length());
 345  0
                     classname = classname.replace(File.separatorChar, '.');
 346  0
                     files.put(file.getAbsolutePath(), classname); // it's a java file, add it.
 347   
                 }
 348   
             }
 349   
         }
 350  0
         return files;
 351   
     }
 352   
 
 353  0
     protected Hashtable scanSources(final Hashtable mapping, final String[] entries) {
 354  0
         final Vector javaFiles = new Vector(512);
 355  0
         for (int i = 0; i < entries.length; i++) {
 356  0
             final File f = new File(entries[i]);
 357  0
             if (f.isDirectory()) {
 358  0
                 DirectoryScanner ds = new DirectoryScanner();
 359  0
                 ds.setBasedir(f);
 360  0
                 ds.setIncludes(new String[]{"**/*.java"});
 361  0
                 ds.scan();
 362  0
                 String[] included = ds.getIncludedFiles();
 363  0
                 for (int j = 0; j < included.length; j++) {
 364  0
                     javaFiles.addElement(new File(f, included[j]));
 365   
                 }
 366  0
             } else if (entries[i].endsWith(".java")) {
 367  0
                 javaFiles.addElement(f);
 368   
             }
 369   
         }
 370   
         // do the mapping paths/classname
 371  0
         final int count = javaFiles.size();
 372  0
         for (int i = 0; i < count; i++) {
 373  0
             File file = (File) javaFiles.elementAt(i);
 374  0
             String pathname = Path.translateFile(file.getAbsolutePath());
 375  0
             String classname = pathname.substring(0, pathname.length() - ".java".length());
 376  0
             classname = classname.replace(File.separatorChar, '.');
 377  0
             mapping.put(pathname, classname);
 378   
         }
 379  0
         return mapping;
 380   
     }
 381   
 
 382   
 }
 383