You are viewing a plain text version of this content. The canonical link for it is here.
Posted to m2-dev@maven.apache.org by jv...@apache.org on 2005/03/03 16:37:25 UTC

cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/transform ArtifactRequestTransformation.java ArtifactRequestTransformationException.java SnapshotRequestTransformation.java

jvanzyl     2005/03/03 07:37:25

  Modified:    maven-artifact/src/main/java/org/apache/maven/artifact/resolver
                        ArtifactResolver.java DefaultArtifactResolver.java
  Added:       maven-artifact/src/main/java/org/apache/maven/artifact/resolver/transform
                        ArtifactRequestTransformation.java
                        ArtifactRequestTransformationException.java
                        SnapshotRequestTransformation.java
  Removed:     maven-artifact/src/main/java/org/apache/maven/artifact/request
                        ArtifactRequest.java
                        ArtifactRequestTransformation.java
                        ArtifactRequestTransformationException.java
  Log:
  o moving artifact request transformation mechanism inside the resolver
    package as they artifact transformations will be utilized from within
    the resolver.
  
  Revision  Changes    Path
  1.3       +4 -2      maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolver.java
  
  Index: ArtifactResolver.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ArtifactResolver.java	6 Oct 2004 14:30:00 -0000	1.2
  +++ ArtifactResolver.java	3 Mar 2005 15:37:25 -0000	1.3
  @@ -1,6 +1,7 @@
   package org.apache.maven.artifact.resolver;
   
   import org.apache.maven.artifact.Artifact;
  +import org.apache.maven.artifact.resolver.transform.ArtifactRequestTransformation;
   import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
   import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
   import org.apache.maven.artifact.repository.ArtifactRepository;
  @@ -49,6 +50,7 @@
                                                     ArtifactMetadataSource source,
                                                     ArtifactFilter filter )
           throws ArtifactResolutionException;
  -    
  +
  +    void addArtifactRequestTransformation( org.apache.maven.artifact.resolver.transform.ArtifactRequestTransformation requestTransformation );
       
   }
  
  
  
  1.12      +16 -9     maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java
  
  Index: DefaultArtifactResolver.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- DefaultArtifactResolver.java	1 Mar 2005 06:20:30 -0000	1.11
  +++ DefaultArtifactResolver.java	3 Mar 2005 15:37:25 -0000	1.12
  @@ -2,6 +2,7 @@
   
   import org.apache.maven.artifact.AbstractArtifactComponent;
   import org.apache.maven.artifact.Artifact;
  +import org.apache.maven.artifact.resolver.transform.ArtifactRequestTransformation;
   import org.apache.maven.artifact.handler.manager.ArtifactHandlerNotFoundException;
   import org.apache.maven.artifact.manager.WagonManager;
   import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
  @@ -22,8 +23,22 @@
       extends AbstractArtifactComponent
       implements ArtifactResolver
   {
  +    // ----------------------------------------------------------------------
  +    // Fields
  +    // ----------------------------------------------------------------------
  +
  +    private List requestTransformations;
  +    
  +    // ----------------------------------------------------------------------
  +    // Components
  +    // ----------------------------------------------------------------------
  +
       private WagonManager wagonManager;
   
  +    // ----------------------------------------------------------------------
  +    // Implementation
  +    // ----------------------------------------------------------------------
  +
       public Artifact resolve( Artifact artifact,
                                Set remoteRepositories,
                                ArtifactRepository localRepository )
  @@ -284,16 +299,8 @@
           conflicts.add( newArtifact );
       }
   
  -    protected boolean includeArtifact( String artifactId, String[] artifactExcludes )
  +    public void addArtifactRequestTransformation( ArtifactRequestTransformation requestTransformation )
       {
  -        for ( int b = 0; b < artifactExcludes.length; b++ )
  -        {
  -            if ( artifactId.equals( artifactExcludes[b] ) )
  -            {
  -                return false;
  -            }
  -        }
   
  -        return true;
       }
   }
  
  
  
  1.1                  maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/transform/ArtifactRequestTransformation.java
  
  Index: ArtifactRequestTransformation.java
  ===================================================================
  package org.apache.maven.artifact.resolver.transform;
  
  import org.apache.maven.artifact.Artifact;
  import org.apache.maven.artifact.repository.ArtifactRepository;
  
  import java.util.Map;
  import java.util.Set;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /**
   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
   * @version $Id: ArtifactRequestTransformation.java,v 1.1 2005/03/03 15:37:25 jvanzyl Exp $
   */
  public interface ArtifactRequestTransformation
  {
      static String ROLE = ArtifactRequestTransformation.class.getName();
  
      /**
       * Take in a artifact and return the transformed artifact. If no
       * transformation has occured the original artifact is returned.
       *
       * @param artifact Artifact to be transformed.
       * @return The transformed Artifact
       */
      Artifact transform( Artifact artifact,
                          ArtifactRepository localRepository,
                          Set remoteRepositories,
                          Map parameters )
          throws Exception;
  }
  
  
  
  1.1                  maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/transform/ArtifactRequestTransformationException.java
  
  Index: ArtifactRequestTransformationException.java
  ===================================================================
  package org.apache.maven.artifact.resolver.transform;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /**
   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
   * @version $Id: ArtifactRequestTransformationException.java,v 1.1 2005/03/03 15:37:25 jvanzyl Exp $
   */
  public class ArtifactRequestTransformationException
      extends Exception
  {
      public ArtifactRequestTransformationException( String message )
      {
          super( message );
      }
  
      public ArtifactRequestTransformationException( Throwable cause )
      {
          super( cause );
      }
  
      public ArtifactRequestTransformationException( String message, Throwable cause )
      {
          super( message, cause );
      }
  }
  
  
  
  1.1                  maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/transform/SnapshotRequestTransformation.java
  
  Index: SnapshotRequestTransformation.java
  ===================================================================
  package org.apache.maven.artifact.resolver.transform;
  
  import org.apache.maven.artifact.Artifact;
  import org.apache.maven.artifact.repository.ArtifactRepository;
  import org.apache.maven.artifact.resolver.ArtifactResolver;
  import org.codehaus.plexus.util.FileUtils;
  
  import java.io.File;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Map;
  import java.util.Set;
  import java.util.TimeZone;
  
  /**
   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
   * @version $Id: SnapshotRequestTransformation.java,v 1.1 2005/03/03 15:37:25 jvanzyl Exp $
   */
  public class SnapshotRequestTransformation
      implements ArtifactRequestTransformation
  {
      private ArtifactResolver artifactResolver;
  
      public Artifact transform( Artifact artifact,
                                 ArtifactRepository localRepository,
                                 Set repositories,
                                 Map parameters )
          throws Exception
      {
          Date localVersion = getLocalVersion( artifact, localRepository );
  
          Date remoteVersion = getRemoteVersion( artifact, repositories, localRepository );
  
          if ( remoteVersion != null )
          {
              //if  local version is unknown (null) it means that
              //we don't have this file locally. so we will be happy
              // to have any snapshot.
              // we wil download in two cases:
              //  a) we don't have any snapot in local repo
              //  b) we have found newer version in remote repository
              if ( localVersion == null || localVersion.before( remoteVersion ) )
              {
                  // here we know that we have artifact like foo-1.2-SNAPSHOT.jar
                  // and the remote timestamp is something like 20010304.121212
                  // so we might as well fetch foo-1.2-20010304.121212.jar
                  // but we are just going to fetch foo-1.2-SNAPSHOT.jar.
                  // We can change the strategy which is used here later on
  
                  // @todo we will delete old file first.
                  //it is not really a right thing to do. Artifact Dowloader should
                  // fetch to temprary file and replace the old file with the new
                  // one once download was finished
  
                  artifact.getFile().delete();
  
                  artifactResolver.resolve( artifact, repositories, localRepository );
  
                  File snapshotVersionFile = getSnapshotVersionFile( artifact, localRepository );
  
                  String timestamp = getTimestamp( remoteVersion );
  
                  // delete old one
                  if ( snapshotVersionFile.exists() )
                  {
                      snapshotVersionFile.delete();
                  }
  
                  FileUtils.fileWrite( snapshotVersionFile.getPath(), timestamp );
              }
          }
  
          return artifact;
      }
  
      private File getSnapshotVersionFile( Artifact artifact, ArtifactRepository localRepository )
      {
          return null;
          //return new File( localRepository.fullArtifactPath( artifact ) );
      }
  
      private Date getRemoteVersion( Artifact artifact, Set remoteRepositories, ArtifactRepository localRepository )
          throws Exception
      {
          Date retValue = null;
  
          artifactResolver.resolve( artifact, remoteRepositories, localRepository );
  
          String timestamp = FileUtils.fileRead( artifact.getPath() );
  
          retValue = parseTimestamp( timestamp );
  
          return retValue;
      }
  
      private Date getLocalVersion( Artifact artifact, ArtifactRepository localRepository )
      {
          //assert artifact.exists();
  
          Date retValue = null;
  
          try
          {
              File file = getSnapshotVersionFile( artifact, localRepository );
  
              if ( file.exists() )
              {
                  String timestamp = FileUtils.fileRead( file );
  
                  retValue = parseTimestamp( timestamp );
  
              }
          }
          catch ( Exception e )
          {
              // ignore
          }
  
          if ( retValue == null )
          {
              //try "traditional method" used in maven1 for obtaining snapshot version
  
              File file = artifact.getFile();
  
              if ( file.exists() )
              {
                  retValue = new Date( file.lastModified() );
  
                  //@todo we should "normalize" the time.
  
                  /*TimeZone gmtTimeZone = TimeZone.getTimeZone( "GMT" );
  
                  TimeZone userTimeZone = TimeZone.getDefault();
  
                  long diff =
                  */
              }
          }
  
          return retValue;
      }
  
      private final static String DATE_FORMAT = "yyyyMMdd.HHmmss";
  
      private static SimpleDateFormat getFormatter()
      {
          SimpleDateFormat formatter = new SimpleDateFormat( DATE_FORMAT );
  
          formatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
  
          return formatter;
      }
  
      public static String getTimestamp()
      {
          Date now = new Date();
  
          SimpleDateFormat formatter = getFormatter();
  
          String retValue = formatter.format( now );
  
          return retValue;
      }
  
      public static Date parseTimestamp ( String timestamp )
          throws ParseException
      {
          Date retValue = getFormatter().parse( timestamp );
  
          return retValue;
      }
  
      public static String getTimestamp ( Date snapshotVersion )
      {
          String retValue = getFormatter().format( snapshotVersion );
  
          return retValue;
      }
  }