You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ep...@apache.org on 2006/02/17 04:39:48 UTC

svn commit: r378422 - in /maven/repository-manager/trunk/maven-repository-proxy/src: main/java/org/apache/maven/repository/proxy/ main/java/org/apache/maven/repository/proxy/configuration/ main/java/org/apache/maven/repository/proxy/repository/ test/ja...

Author: epunzalan
Date: Thu Feb 16 19:39:45 2006
New Revision: 378422

URL: http://svn.apache.org/viewcvs?rev=378422&view=rev
Log:
PR: MRM-95

Added cacheFailure and cachePeriod to the repository configuration.  cacheFailure not yet implemented in this commit.

Modified:
    maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java
    maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java
    maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java
    maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java

Modified: maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java?rev=378422&r1=378421&r2=378422&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java (original)
+++ maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java Thu Feb 16 19:39:45 2006
@@ -41,8 +41,10 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -90,6 +92,23 @@
         {
             getRemoteFile( path );
         }
+        else
+        {
+            List repos = new ArrayList();
+            for ( Iterator confRepos = config.getRepositories().iterator(); confRepos.hasNext(); )
+            {
+                ProxyRepository repo = (ProxyRepository) confRepos.next();
+                if ( repo.getCachePeriod() > 0 &&
+                    ( repo.getCachePeriod() * 1000 ) + cachedFile.lastModified() < System.currentTimeMillis() )
+                {
+                    repos.add( repo );
+                }
+            }
+            if ( repos.size() > 0 )
+            {
+                getRemoteFile( path, repos );
+            }
+        }
         return cachedFile;
     }
 
@@ -101,21 +120,29 @@
     {
         checkConfiguration();
 
+        return getRemoteFile( path, config.getRepositories() );
+    }
+
+    private File getRemoteFile( String path, List repositories )
+        throws ProxyException, ResourceDoesNotExistException
+    {
+        checkConfiguration();
+
         Artifact artifact = ArtifactUtils.buildArtifact( path, artifactFactory );
 
         File remoteFile;
         if ( artifact != null )
         {
-            remoteFile = getArtifactFile( artifact );
+            remoteFile = getArtifactFile( artifact, repositories );
         }
         else if ( path.endsWith( ".md5" ) || path.endsWith( ".sha1" ) )
         {
-            remoteFile = getRepositoryFile( path, false );
+            remoteFile = getRepositoryFile( path, repositories, false );
         }
         else
         {
             // as of now, only metadata fits here
-            remoteFile = getRepositoryFile( path );
+            remoteFile = getRepositoryFile( path, repositories );
         }
 
         return remoteFile;
@@ -124,13 +151,14 @@
     /**
      * Used to download an artifact object from the remote repositories.
      *
-     * @param artifact the artifact object to be downloaded from a remote repository
+     * @param artifact     the artifact object to be downloaded from a remote repository
+     * @param repositories the list of ProxyRepositories to retrieve the artifact from
      * @return File object representing the remote artifact in the repository cache
      * @throws ProxyException                when an error occurred during retrieval of the requested artifact
      * @throws ResourceDoesNotExistException when the requested artifact cannot be found in any of the
      *                                       configured repositories
      */
-    private File getArtifactFile( Artifact artifact )
+    private File getArtifactFile( Artifact artifact, List repositories )
         throws ResourceDoesNotExistException, ProxyException
     {
         ArtifactRepository repoCache = config.getRepositoryCache();
@@ -142,7 +170,8 @@
         {
             try
             {
-                wagonManager.getArtifact( artifact, config.getRepositories() );
+                //@todo usage of repository cache period
+                wagonManager.getArtifact( artifact, repositories );
             }
             catch ( TransferFailedException e )
             {
@@ -158,30 +187,32 @@
      * Used to retrieve a remote file from the remote repositories.  This method is used only when the requested
      * path cannot be resolved into a repository object, for example, an Artifact.
      *
-     * @param path the remote path to use to search for the requested file
+     * @param path         the remote path to use to search for the requested file
+     * @param repositories the list of repositories to retrieve the file from
      * @return File object representing the remote file in the repository cache
      * @throws ResourceDoesNotExistException when the requested path cannot be found in any of the configured
      *                                       repositories.
      * @throws ProxyException                when an error occurred during the retrieval of the requested path
      */
-    private File getRepositoryFile( String path )
+    private File getRepositoryFile( String path, List repositories )
         throws ResourceDoesNotExistException, ProxyException
     {
-        return getRepositoryFile( path, true );
+        return getRepositoryFile( path, repositories, true );
     }
 
     /**
      * Used to retrieve a remote file from the remote repositories.  This method is used only when the requested
      * path cannot be resolved into a repository object, for example, an Artifact.
      *
-     * @param path        the remote path to use to search for the requested file
-     * @param useChecksum forces the download to whether use a checksum (if present in the remote repository) or not
+     * @param path         the remote path to use to search for the requested file
+     * @param repositories the list of repositories to retrieve the file from
+     * @param useChecksum  forces the download to whether use a checksum (if present in the remote repository) or not
      * @return File object representing the remote file in the repository cache
      * @throws ResourceDoesNotExistException when the requested path cannot be found in any of the configured
      *                                       repositories.
      * @throws ProxyException                when an error occurred during the retrieval of the requested path
      */
-    private File getRepositoryFile( String path, boolean useChecksum )
+    private File getRepositoryFile( String path, List repositories, boolean useChecksum )
         throws ResourceDoesNotExistException, ProxyException
     {
         Map checksums = null;
@@ -191,9 +222,9 @@
         ArtifactRepository cache = config.getRepositoryCache();
         File target = new File( cache.getBasedir(), path );
 
-        for ( Iterator repositories = config.getRepositories().iterator(); repositories.hasNext(); )
+        for ( Iterator repos = repositories.iterator(); repos.hasNext(); )
         {
-            ProxyRepository repository = (ProxyRepository) repositories.next();
+            ProxyRepository repository = (ProxyRepository) repos.next();
 
             try
             {
@@ -212,23 +243,27 @@
                     File temp = new File( target.getAbsolutePath() + ".tmp" );
 
                     int tries = 0;
-                    boolean success = false;
+                    boolean success = true;
 
-                    while ( !success )
+                    do
                     {
                         tries++;
 
                         getLogger().info( "Trying " + path + " from " + repository.getId() + "..." );
 
-                        wagon.get( path, temp );
-
-                        if ( useChecksum )
+                        if ( !target.exists() )
                         {
-                            success = doChecksumCheck( checksums, path, wagon );
+                            wagon.get( path, temp );
                         }
                         else
                         {
-                            success = true;
+                            long repoTimestamp = target.lastModified() + repository.getCachePeriod() * 1000;
+                            wagon.getIfNewer( path, temp, repoTimestamp );
+                        }
+
+                        if ( useChecksum )
+                        {
+                            success = doChecksumCheck( checksums, path, wagon );
                         }
 
                         if ( tries > 1 && !success )
@@ -236,6 +271,8 @@
                             throw new ProxyException( "Checksum failures occurred while downloading " + path );
                         }
                     }
+                    while ( !success );
+
                     disconnectWagon( wagon );
 
                     copyTempToTarget( temp, target );
@@ -250,6 +287,7 @@
             }
             catch ( ResourceDoesNotExistException e )
             {
+                //@todo usage for cacheFailure 
                 //do nothing, file not found in this repository
             }
             catch ( AuthorizationException e )

Modified: maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java?rev=378422&r1=378421&r2=378422&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java (original)
+++ maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java Thu Feb 16 19:39:45 2006
@@ -168,6 +168,8 @@
             if ( !repoConfig.getKey().equals( "global" ) )
             {
                 ProxyRepository repo = new ProxyRepository( repoConfig.getKey(), repoConfig.getUrl(), layout );
+                repo.setCacheFailures( repoConfig.getCacheFailures() );
+                repo.setCachePeriod( repoConfig.getCachePeriod() );
 
                 repoList.add( repo );
             }

Modified: maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java?rev=378422&r1=378421&r2=378422&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java (original)
+++ maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java Thu Feb 16 19:39:45 2006
@@ -28,8 +28,43 @@
 public class ProxyRepository
     extends DefaultArtifactRepository
 {
+    // zero does not cache
+    private long cachePeriod = 0;
+
+    private boolean cacheFailures = false;
+
+    public ProxyRepository( String id, String url, ArtifactRepositoryLayout layout, boolean cacheFailures,
+                            long cachePeriod )
+    {
+        this( id, url, layout );
+
+        setCacheFailures( cacheFailures );
+
+        setCachePeriod( cachePeriod );
+    }
+
     public ProxyRepository( String id, String url, ArtifactRepositoryLayout layout )
     {
         super( id, url, layout );
+    }
+
+    public long getCachePeriod()
+    {
+        return cachePeriod;
+    }
+
+    public void setCachePeriod( long cachePeriod )
+    {
+        this.cachePeriod = cachePeriod;
+    }
+
+    public boolean isCacheFailures()
+    {
+        return cacheFailures;
+    }
+
+    public void setCacheFailures( boolean cacheFailures )
+    {
+        this.cacheFailures = cacheFailures;
     }
 }

Modified: maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java?rev=378422&r1=378421&r2=378422&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java (original)
+++ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java Thu Feb 16 19:39:45 2006
@@ -63,19 +63,31 @@
     {
         ArtifactRepositoryLayout defLayout = new DefaultRepositoryLayout();
         ProxyRepository repo1 = new ProxyRepository( "repo1", "http://www.ibiblio.org/maven2", defLayout );
+        repo1.setCacheFailures( true );
+        repo1.setCachePeriod( 0 );
         config.addRepository( repo1 );
         assertEquals( 1, config.getRepositories().size() );
 
         ArtifactRepositoryLayout legacyLayout = new LegacyRepositoryLayout();
         ProxyRepository repo2 = new ProxyRepository( "repo2", "http://www.ibiblio.org/maven", legacyLayout );
+        repo2.setCacheFailures( false );
+        repo2.setCachePeriod( 3600 );
         config.addRepository( repo2 );
         assertEquals( 2, config.getRepositories().size() );
 
         List repositories = config.getRepositories();
         ProxyRepository repo = (ProxyRepository) repositories.get( 0 );
+        assertEquals( "repo1", repo.getId() );
+        assertEquals( "http://www.ibiblio.org/maven2", repo.getUrl() );
+        assertTrue( repo.isCacheFailures() );
+        assertEquals( 0, repo.getCachePeriod() );
         assertEquals( repo1, repo );
 
         repo = (ProxyRepository) repositories.get( 1 );
+        assertEquals( "repo2", repo.getId() );
+        assertEquals( "http://www.ibiblio.org/maven", repo.getUrl() );
+        assertFalse( repo.isCacheFailures() );
+        assertEquals( 3600, repo.getCachePeriod() );
         assertEquals( repo2, repo );
 
         try
@@ -107,29 +119,50 @@
 
             config.loadMavenProxyConfiguration( confFile );
 
-            assertTrue( config.getRepositoryCachePath().endsWith( "target" ) );
+            assertTrue( "cache path changed", config.getRepositoryCachePath().endsWith( "target" ) );
 
             assertEquals( "Count repositories", 4, config.getRepositories().size() );
 
+            int idx = 0;
             for ( Iterator repos = config.getRepositories().iterator(); repos.hasNext(); )
             {
+                idx++;
+
                 ProxyRepository repo = (ProxyRepository) repos.next();
 
-                if ( "local-repo".equals( repo.getKey() ) )
-                {
-                    assertEquals( "file:///./target/remote-repo1", repo.getUrl() );
-                }
-                else if ( "www-ibiblio-org".equals( repo.getKey() ) )
-                {
-                    assertEquals( "http://www.ibiblio.org/maven2", repo.getUrl() );
-                }
-                else if ( "dist-codehaus-org".equals( repo.getKey() ) )
-                {
-                    assertEquals( "http://dist.codehaus.org", repo.getUrl() );
-                }
-                else if ( "private-example-com".equals( repo.getKey() ) )
+                //switch is made to check for ordering
+                switch ( idx )
                 {
-                    assertEquals( "http://private.example.com/internal", repo.getUrl() );
+                    case 1:
+                        assertEquals( "Repository name not as expected", "local-repo", repo.getKey() );
+                        assertEquals( "Repository url does not match its name", "file:///./target/remote-repo1",
+                                      repo.getUrl() );
+                        assertEquals( "Repository cache period check failed", 0, repo.getCachePeriod() );
+                        assertFalse( "Repository failure caching check failed", repo.isCacheFailures() );
+                        break;
+                    case 2:
+                        assertEquals( "Repository name not as expected", "www-ibiblio-org", repo.getKey() );
+                        assertEquals( "Repository url does not match its name", "http://www.ibiblio.org/maven2",
+                                      repo.getUrl() );
+                        assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );
+                        assertTrue( "Repository failure caching check failed", repo.isCacheFailures() );
+                        break;
+                    case 3:
+                        assertEquals( "Repository name not as expected", "dist-codehaus-org", repo.getKey() );
+                        assertEquals( "Repository url does not match its name", "http://dist.codehaus.org",
+                                      repo.getUrl() );
+                        assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );
+                        assertTrue( "Repository failure caching check failed", repo.isCacheFailures() );
+                        break;
+                    case 4:
+                        assertEquals( "Repository name not as expected", "private-example-com", repo.getKey() );
+                        assertEquals( "Repository url does not match its name", "http://private.example.com/internal",
+                                      repo.getUrl() );
+                        assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );
+                        assertFalse( "Repository failure caching check failed", repo.isCacheFailures() );
+                        break;
+                    default:
+                        fail( "Unexpected order count" );
                 }
             }
         }