Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 354   Methods: 12
NCLOC: 187   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JarLibResolveTask.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 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.optional.extension;
 55   
 
 56   
 import java.io.File;
 57   
 import java.util.ArrayList;
 58   
 import java.util.jar.Manifest;
 59   
 import org.apache.tools.ant.BuildException;
 60   
 import org.apache.tools.ant.Project;
 61   
 import org.apache.tools.ant.Task;
 62   
 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.AntResolver;
 63   
 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.LocationResolver;
 64   
 import org.apache.tools.ant.taskdefs.optional.extension.resolvers.URLResolver;
 65   
 
 66   
 /**
 67   
  * Tries to locate a JAR to satisfy an extension and place
 68   
  * location of JAR into property.
 69   
  *
 70   
  * @author <a href="mailto:peter@apache.org">Peter Donald</a>
 71   
  * @author <a href="mailto:jeff@socialchange.net.au">Jeff Turner</a>
 72   
  * @ant.task name="jarlib-resolve"
 73   
  */
 74   
 public class JarLibResolveTask
 75   
     extends Task
 76   
 {
 77   
     /**
 78   
      * The name of the property in which the location of
 79   
      * library is stored.
 80   
      */
 81   
     private String m_property;
 82   
 
 83   
     /**
 84   
      * The extension that is required.
 85   
      */
 86   
     private Extension m_extension;
 87   
 
 88   
     /**
 89   
      * The set of resolvers to use to attempt to locate library.
 90   
      */
 91   
     private final ArrayList m_resolvers = new ArrayList();
 92   
 
 93   
     /**
 94   
      * Flag to indicate that you should check that
 95   
      * the librarys resolved actually contain
 96   
      * extension and if they don't then raise
 97   
      * an exception.
 98   
      */
 99   
     private boolean m_checkExtension = true;
 100   
 
 101   
     /**
 102   
      * Flag indicating whether or not you should
 103   
      * throw a BuildException if you cannot resolve
 104   
      * library.
 105   
      */
 106   
     private boolean m_failOnError = true;
 107   
 
 108   
     /**
 109   
      * The name of the property in which the location of
 110   
      * library is stored.
 111   
      *
 112   
      * @param property The name of the property in which the location of
 113   
      *                 library is stored.
 114   
      */
 115  0
     public void setProperty( final String property )
 116   
     {
 117  0
         m_property = property;
 118   
     }
 119   
 
 120   
     /**
 121   
      * If true, libraries returned by nested resolvers should be
 122   
      * checked to see if they supply extension.
 123   
      */
 124  0
     public void setCheckExtension( final boolean checkExtension )
 125   
     {
 126  0
         m_checkExtension = checkExtension;
 127   
     }
 128   
 
 129   
     /**
 130   
      * If true, failure to locate library should fail build.
 131   
      */
 132  0
     public void setFailOnError( final boolean failOnError )
 133   
     {
 134  0
         m_failOnError = failOnError;
 135   
     }
 136   
 
 137   
     /**
 138   
      * Adds location resolver to look for a library in a location
 139   
      * relative to project directory.
 140   
      */
 141  0
     public void addConfiguredLocation( final LocationResolver location )
 142   
     {
 143  0
         m_resolvers.add( location );
 144   
     }
 145   
 
 146   
     /**
 147   
      * Adds a URL resolver to download a library from a URL
 148   
      * to a local file.
 149   
      */
 150  0
     public void addConfiguredUrl( final URLResolver url )
 151   
     {
 152  0
         m_resolvers.add( url );
 153   
     }
 154   
 
 155   
     /**
 156   
      * Adds Ant resolver to run an Ant build file to generate a library.
 157   
      */
 158  0
     public void addConfiguredAnt( final AntResolver ant )
 159   
     {
 160  0
         m_resolvers.add( ant );
 161   
     }
 162   
 
 163   
     /**
 164   
      * Set the Extension looking for.
 165   
      *
 166   
      * @param extension Set the Extension looking for.
 167   
      */
 168  0
     public void addConfiguredExtension( final ExtensionAdapter extension )
 169   
     {
 170  0
         if( null != m_extension )
 171   
         {
 172  0
             final String message = "Can not specify extension to " +
 173   
                 "resolve multiple times.";
 174  0
             throw new BuildException( message );
 175   
         }
 176  0
         m_extension = extension.toExtension();
 177   
     }
 178   
 
 179  0
     public void execute()
 180   
         throws BuildException
 181   
     {
 182  0
         validate();
 183   
 
 184  0
         getProject().log( "Resolving extension: " + m_extension,
 185   
                           Project.MSG_VERBOSE );
 186   
 
 187  0
         String candidate =
 188   
             getProject().getProperty( m_property );
 189   
 
 190  0
         if( null != candidate )
 191   
         {
 192  0
             final String message = "Property Already set to: " + candidate;
 193  0
             if( m_failOnError )
 194   
             {
 195  0
                 throw new BuildException( message );
 196   
             }
 197   
             else
 198   
             {
 199  0
                 getProject().log( message, Project.MSG_ERR );
 200  0
                 return;
 201   
             }
 202   
         }
 203   
 
 204  0
         final int size = m_resolvers.size();
 205  0
         for( int i = 0; i < size; i++ )
 206   
         {
 207  0
             final ExtensionResolver resolver =
 208   
                 (ExtensionResolver)m_resolvers.get( i );
 209   
 
 210  0
             getProject().log( "Searching for extension using Resolver:" + resolver,
 211   
                               Project.MSG_VERBOSE );
 212   
 
 213  0
             try
 214   
             {
 215  0
                 final File file =
 216   
                     resolver.resolve( m_extension, getProject() );
 217  0
                 try
 218   
                 {
 219  0
                     checkExtension( file );
 220  0
                     return;
 221   
                 }
 222   
                 catch( final BuildException be )
 223   
                 {
 224  0
                     final String message =
 225   
                         "File " + file + " returned by resolver failed " +
 226   
                         "to satisfy extension due to: " + be.getMessage();
 227  0
                     getProject().log( message, Project.MSG_WARN );
 228   
                 }
 229   
             }
 230   
             catch( final BuildException be )
 231   
             {
 232  0
                 final String message =
 233   
                     "Failed to resolve extension to file " +
 234   
                     "using resolver " + resolver + " due to: " + be;
 235  0
                 getProject().log( message, Project.MSG_WARN );
 236   
             }
 237   
         }
 238   
 
 239  0
         missingExtension();
 240   
     }
 241   
 
 242   
     /**
 243   
      * Utility method that will throw a {@link BuildException}
 244   
      * if {@link #m_failOnError} is true else it just displays
 245   
      * a warning.
 246   
      */
 247  0
     private void missingExtension()
 248   
     {
 249  0
         final String message =
 250   
             "Unable to resolve extension to a file";
 251  0
         if( m_failOnError )
 252   
         {
 253  0
             throw new BuildException( message );
 254   
         }
 255   
         else
 256   
         {
 257  0
             getProject().log( message, Project.MSG_ERR );
 258   
         }
 259   
     }
 260   
 
 261   
     /**
 262   
      * Check if specified file satisfies extension.
 263   
      * If it does then set the relevent property
 264   
      * else throw a BuildException.
 265   
      *
 266   
      * @param file the candidate library
 267   
      * @throws BuildException if library does not satisfy extension
 268   
      */
 269  0
     private void checkExtension( final File file )
 270   
     {
 271  0
         if( !file.exists() )
 272   
         {
 273  0
             final String message =
 274   
                 "File " + file + " does not exist";
 275  0
             throw new BuildException( message );
 276   
         }
 277  0
         if( !file.isFile() )
 278   
         {
 279  0
             final String message =
 280   
                 "File " + file + " is not a file";
 281  0
             throw new BuildException( message );
 282   
         }
 283   
 
 284  0
         if( !m_checkExtension )
 285   
         {
 286  0
             final String message = "Setting property to " +
 287   
                 file + " without verifying library satisfies extension";
 288  0
             getProject().log( message, Project.MSG_VERBOSE );
 289  0
             setLibraryProperty( file );
 290   
         }
 291   
         else
 292   
         {
 293  0
             getProject().log( "Checking file " + file +
 294   
                               " to see if it satisfies extension",
 295   
                               Project.MSG_VERBOSE );
 296  0
             final Manifest manifest =
 297   
                 ExtensionUtil.getManifest( file );
 298  0
             final Extension[] extensions =
 299   
                 Extension.getAvailable( manifest );
 300  0
             for( int i = 0; i < extensions.length; i++ )
 301   
             {
 302  0
                 final Extension extension = extensions[ i ];
 303  0
                 if( extension.isCompatibleWith( m_extension ) )
 304   
                 {
 305  0
                     setLibraryProperty( file );
 306  0
                     return;
 307   
                 }
 308   
             }
 309   
 
 310  0
             getProject().log( "File " + file + " skipped as it " +
 311   
                               "does not satisfy extension",
 312   
                               Project.MSG_VERBOSE );
 313   
 
 314  0
             final String message =
 315   
                 "File " + file + " does not satisfy extension";
 316  0
             throw new BuildException( message );
 317   
         }
 318   
     }
 319   
 
 320   
     /**
 321   
      * Utility method to set the appropriate property
 322   
      * to indicate that specified file satisfies library
 323   
      * requirements.
 324   
      *
 325   
      * @param file the library
 326   
      */
 327  0
     private void setLibraryProperty( final File file )
 328   
     {
 329  0
         getProject().setNewProperty( m_property,
 330   
                                      file.getAbsolutePath() );
 331   
     }
 332   
 
 333   
     /**
 334   
      * Validate the tasks parameters.
 335   
      *
 336   
      * @throws BuildException if invalid parameters found
 337   
      */
 338  0
     private void validate()
 339   
         throws BuildException
 340   
     {
 341  0
         if( null == m_property )
 342   
         {
 343  0
             final String message = "Property attribute must be specified.";
 344  0
             throw new BuildException( message );
 345   
         }
 346   
 
 347  0
         if( null == m_extension )
 348   
         {
 349  0
             final String message = "Extension element must be specified.";
 350  0
             throw new BuildException( message );
 351   
         }
 352   
     }
 353   
 }
 354