From: Edwin L. Punzalan Date: Fri, 17 Feb 2006 03:39:45 +0000 (+0000) Subject: PR: MRM-95 X-Git-Tag: archiva-0.9-alpha-1~932 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4ad366c501be107a0d60ddc21e09effd0b4abf88;p=archiva.git PR: MRM-95 Added cacheFailure and cachePeriod to the repository configuration. cacheFailure not yet implemented in this commit. git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@378422 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java index 638899ee1..bfd670179 100644 --- a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java +++ b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java @@ -41,8 +41,10 @@ import java.io.FileInputStream; 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 @@ public class DefaultProxyManager { 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 @@ public class DefaultProxyManager { 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 @@ public class DefaultProxyManager /** * 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 @@ public class DefaultProxyManager { try { - wagonManager.getArtifact( artifact, config.getRepositories() ); + //@todo usage of repository cache period + wagonManager.getArtifact( artifact, repositories ); } catch ( TransferFailedException e ) { @@ -158,30 +187,32 @@ public class DefaultProxyManager * 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 @@ public class DefaultProxyManager 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 @@ public class DefaultProxyManager 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 @@ public class DefaultProxyManager throw new ProxyException( "Checksum failures occurred while downloading " + path ); } } + while ( !success ); + disconnectWagon( wagon ); copyTempToTarget( temp, target ); @@ -250,6 +287,7 @@ public class DefaultProxyManager } catch ( ResourceDoesNotExistException e ) { + //@todo usage for cacheFailure //do nothing, file not found in this repository } catch ( AuthorizationException e ) diff --git a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java index 1c1bda993..659381597 100644 --- a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java +++ b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/configuration/ProxyConfiguration.java @@ -168,6 +168,8 @@ public class ProxyConfiguration 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 ); } diff --git a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java index 80c3c8ae6..efbe6b3d4 100644 --- a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java +++ b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java @@ -28,8 +28,43 @@ import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 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; + } } diff --git a/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java b/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java index c4ea859cc..08b1f086f 100644 --- a/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java +++ b/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java @@ -63,19 +63,31 @@ public class ProxyConfigurationTest { 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 @@ public class ProxyConfigurationTest 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" ); } } }