ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/extension/resolvers LocationResolver.java URLResolver.java
Date Tue, 30 Apr 2002 14:33:55 GMT
donaldp     02/04/30 07:33:55

  Modified:    src/main/org/apache/tools/ant/taskdefs defaults.properties
  Added:       src/main/org/apache/tools/ant/taskdefs/optional/extension
                        ExtensionResolver.java JarLibResolveTask.java
               src/main/org/apache/tools/ant/taskdefs/optional/extension/resolvers
                        LocationResolver.java URLResolver.java
  Log:
  Add in a resolver task.
  The resolver takes an extension and a set of operations that can be
  used to get library coresponding to specified extension. The task
  will run through each operation until required extension is found.
  
  Revision  Changes    Path
  1.128     +1 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties,v
  retrieving revision 1.127
  retrieving revision 1.128
  diff -u -r1.127 -r1.128
  --- defaults.properties	30 Apr 2002 08:13:42 -0000	1.127
  +++ defaults.properties	30 Apr 2002 14:33:55 -0000	1.128
  @@ -162,6 +162,7 @@
   jarlib-display=org.apache.tools.ant.taskdefs.optional.extension.JarLibDisplayTask
   jarlib-manifest=org.apache.tools.ant.taskdefs.optional.extension.JarLibManifestTask
   jarlib-available=org.apache.tools.ant.taskdefs.optional.extension.JarLibAvailableTask
  +jarlib-resolve=org.apache.tools.ant.taskdefs.optional.extension.JarLibResolveTask
   
   # deprecated ant tasks (kept for back compatibility)
   starteam=org.apache.tools.ant.taskdefs.optional.scm.AntStarTeamCheckOut
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionResolver.java
  
  Index: ExtensionResolver.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.optional.extension;
  
  import java.io.File;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Project;
  
  /**
   * Interface to locate a File that satisfies extension.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/30 14:33:55 $
   */
  public interface ExtensionResolver
  {
      /**
       * Attempt to locate File that satisfies
       * extension via resolver.
       *
       * @param extension the extension
       * @return the File satisfying extension, null
       *         if can not resolve extension
       * @throws BuildException if error occurs attempting to
       *         resolve extension
       */
      File resolve( Extension extension, Project project )
          throws BuildException;
  }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/extension/JarLibResolveTask.java
  
  Index: JarLibResolveTask.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Ant", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.tools.ant.taskdefs.optional.extension;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.jar.Manifest;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.taskdefs.optional.extension.resolvers.LocationResolver;
  import org.apache.tools.ant.taskdefs.optional.extension.resolvers.URLResolver;
  
  /**
   * Try to locate a jar to satisfy an extension and place
   * location of jar into property.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @ant.task name="jarlib-resolver"
   */
  public class JarLibResolveTask
      extends Task
  {
      /**
       * The name of the property in which the location of
       * library is stored.
       */
      private String m_property;
  
      /**
       * The extension that is required.
       */
      private Extension m_extension;
  
      /**
       * The set of resolvers to use to attempt to locate library.
       */
      private final ArrayList m_resolvers = new ArrayList();
  
      /**
       * Flag to indicate that you should check that
       * the librarys resolved actually contain
       * extension and if they don't then raise
       * an exception.
       */
      private boolean m_checkExtension = true;
  
      /**
       * Flag indicating whether or not you should
       * throw a BuildException if you cannot resolve
       * library.
       */
      private boolean m_failOnError = true;
  
      /**
       * The name of the property in which the location of
       * library is stored.
       *
       * @param property The name of the property in which the location of
       *                 library is stored.
       */
      public void setProperty( final String property )
      {
          m_property = property;
      }
  
      public void setCheckExtension( final boolean checkExtension )
      {
          m_checkExtension = checkExtension;
      }
  
      public void setFailOnError( final boolean failOnError )
      {
          m_failOnError = failOnError;
      }
  
      public void addConfiguredLocation( final LocationResolver location )
      {
          m_resolvers.add( location );
      }
  
      public void addConfiguredUrl( final URLResolver url )
      {
          m_resolvers.add( url );
      }
  
      /**
       * Set the Extension looking for.
       *
       * @param extension Set the Extension looking for.
       */
      public void addConfiguredExtension( final ExtensionAdapter extension )
      {
          if( null != m_extension )
          {
              final String message = "Can not specify extension to " +
                  "resolve multiple times.";
              throw new BuildException( message );
          }
          m_extension = extension.toExtension();
      }
  
      public void execute()
          throws BuildException
      {
          validate();
  
          getProject().log( "Resolving extension: " + m_extension,
                            Project.MSG_VERBOSE );
  
          String candidate =
              getProject().getProperty( m_property );
  
          if( null != candidate )
          {
              final File file =
                  getProject().resolveFile( candidate );
              getProject().log( "Property Already set to: " + file,
                                Project.MSG_VERBOSE );
              try
              {
                  checkExtension( file );
                  return;
              }
              catch( final BuildException e )
              {
                  missingExtension();
                  return;
              }
          }
  
          final int size = m_resolvers.size();
          for( int i = 0; i < size; i++ )
          {
              final ExtensionResolver resolver =
                  (ExtensionResolver)m_resolvers.get( i );
  
              getProject().log( "Searching for extension using Resolver:" + resolver,
                                Project.MSG_VERBOSE );
  
              try
              {
                  final File file =
                      resolver.resolve( m_extension, getProject() );
                  try
                  {
                      checkExtension( file );
                      return;
                  }
                  catch( final BuildException be )
                  {
                      final String message =
                          "File " + file + " returned by resolver failed " +
                          "to satisfy extension due to: " + be.getMessage();
                      getProject().log( message, Project.MSG_WARN );
                  }
              }
              catch( final BuildException be )
              {
                  final String message =
                      "Failed to resolve extension to file " +
                      "using resolver " + resolver + " due to: " + be;
                  getProject().log( message, Project.MSG_WARN );
              }
          }
  
          missingExtension();
      }
  
      /**
       * Utility method that will throw a {@link BuildException}
       * if {@link #m_failOnError} is true else it just displays
       * a warning.
       */
      private void missingExtension()
      {
          final String message =
              "Unable to resolve extension to a file";
          if( m_failOnError )
          {
              throw new BuildException( message );
          }
          else
          {
              getProject().log( message, Project.MSG_ERR );
          }
      }
  
      /**
       * Check if specified file satisfies extension.
       * If it does then set the relevent property
       * else throw a BuildException.
       *
       * @param file the candidate library
       * @throws BuildException if library does not satisfy extension
       */
      private void checkExtension( final File file )
      {
          if( !file.exists() )
          {
              final String message =
                  "File " + file + " does not exist";
              throw new BuildException( message );
          }
          if( !file.isFile() )
          {
              final String message =
                  "File " + file + " is not a file";
              throw new BuildException( message );
          }
  
          if( !m_checkExtension )
          {
              final String message = "Setting property to " +
                  file + " without verifying library satisfies extension";
              getProject().log( message, Project.MSG_VERBOSE );
              setLibraryProperty( file );
          }
          else
          {
              getProject().log( "Checking file " + file +
                                " to see if it satisfies extension",
                                Project.MSG_VERBOSE );
              final Manifest manifest =
                  ExtensionUtil.getManifest( file );
              final Extension[] extensions =
                  Extension.getAvailable( manifest );
              for( int i = 0; i < extensions.length; i++ )
              {
                  final Extension extension = extensions[ i ];
                  if( extension.isCompatibleWith( m_extension ) )
                  {
                      setLibraryProperty( file );
                      return;
                  }
              }
  
              getProject().log( "File " + file + " skipped as it " +
                                "does not satisfy extension",
                                Project.MSG_VERBOSE );
  
              final String message =
                  "File " + file + " does not satisfy extension";
              throw new BuildException( message );
          }
      }
  
      /**
       * Utility method to set the appropriate property
       * to indicate that specified file satisfies library
       * requirements.
       *
       * @param file the library
       */
      private void setLibraryProperty( final File file )
      {
          getProject().setNewProperty( m_property,
                                       file.getAbsolutePath() );
      }
  
      /**
       * Validate the tasks parameters.
       *
       * @throws BuildException if invalid parameters found
       */
      private void validate()
          throws BuildException
      {
          if( null == m_property )
          {
              final String message = "Property attribute must be specified.";
              throw new BuildException( message );
          }
  
          if( null == m_extension )
          {
              final String message = "Extension element must be specified.";
              throw new BuildException( message );
          }
      }
  }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/extension/resolvers/LocationResolver.java
  
  Index: LocationResolver.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.optional.extension.resolvers;
  
  import java.io.File;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.taskdefs.optional.extension.Extension;
  import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
  
  /**
   * Resolver that just returns s specified location.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/30 14:33:55 $
   */
  public class LocationResolver
      implements ExtensionResolver
  {
      private String m_location;
  
      public void setLocation( final String location )
      {
          m_location = location;
      }
  
      public File resolve( final Extension extension,
                           final Project project )
          throws BuildException
      {
          if( null == m_location )
          {
              final String message = "No location specified for resolver";
              throw new BuildException( message );
          }
  
          return project.resolveFile( m_location );
      }
  
      public String toString()
      {
          return "Location[" + m_location + "]";
      }
  }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/extension/resolvers/URLResolver.java
  
  Index: URLResolver.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.optional.extension.resolvers;
  
  import java.io.File;
  import java.net.URL;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.taskdefs.Get;
  import org.apache.tools.ant.taskdefs.optional.extension.Extension;
  import org.apache.tools.ant.taskdefs.optional.extension.ExtensionResolver;
  
  /**
   * Resolver that just returns s specified location.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/04/30 14:33:55 $
   */
  public class URLResolver
      implements ExtensionResolver
  {
      private File m_destfile;
      private File m_destdir;
      private URL m_url;
  
      public void setUrl( final URL url )
      {
          m_url = url;
      }
  
      public void setDestfile( final File destfile )
      {
          m_destfile = destfile;
      }
  
      public void setDestdir( final File destdir )
      {
          m_destdir = destdir;
      }
  
      public File resolve( final Extension extension,
                           final Project project )
          throws BuildException
      {
          validate();
  
          final File file = getDest();
  
          final Get get = (Get)project.createTask( "get" );
          get.setDest( file );
          get.setSrc( m_url );
          get.execute();
  
          return file;
      }
  
      private File getDest()
      {
          if( null != m_destfile )
          {
              return m_destfile;
          }
          else
          {
              final String file = m_url.getFile();
              String filename = null;
              if( null == file || file.length() <= 1 )
              {
                  filename = "default.file";
              }
              else
              {
                  int index = file.lastIndexOf( '/' );
                  if( -1 == index )
                  {
                      index = 0;
                  }
                  filename = file.substring( index );
              }
  
              return new File( m_destdir, filename );
          }
      }
  
      private void validate()
      {
          if( null == m_url )
          {
              final String message = "Must specify URL";
              throw new BuildException( message );
          }
  
          if( null == m_destdir && null == m_destfile )
          {
              final String message = "Must specify destination file or directory";
              throw new BuildException( message );
          }
          else if( null != m_destdir && null != m_destfile )
          {
              final String message = "Must not specify both destination file or directory";
              throw new BuildException( message );
          }
      }
  
      public String toString()
      {
          return "URL[" + m_url + "]";
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message