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;
/**
{
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;
}
{
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;
/**
* 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();
{
try
{
- wagonManager.getArtifact( artifact, config.getRepositories() );
+ //@todo usage of repository cache period
+ wagonManager.getArtifact( artifact, repositories );
}
catch ( TransferFailedException e )
{
* 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;
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
{
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 )
throw new ProxyException( "Checksum failures occurred while downloading " + path );
}
}
+ while ( !success );
+
disconnectWagon( wagon );
copyTempToTarget( temp, target );
}
catch ( ResourceDoesNotExistException e )
{
+ //@todo usage for cacheFailure
//do nothing, file not found in this repository
}
catch ( AuthorizationException e )
{\r
ArtifactRepositoryLayout defLayout = new DefaultRepositoryLayout();\r
ProxyRepository repo1 = new ProxyRepository( "repo1", "http://www.ibiblio.org/maven2", defLayout );\r
+ repo1.setCacheFailures( true );\r
+ repo1.setCachePeriod( 0 );\r
config.addRepository( repo1 );\r
assertEquals( 1, config.getRepositories().size() );\r
\r
ArtifactRepositoryLayout legacyLayout = new LegacyRepositoryLayout();\r
ProxyRepository repo2 = new ProxyRepository( "repo2", "http://www.ibiblio.org/maven", legacyLayout );\r
+ repo2.setCacheFailures( false );\r
+ repo2.setCachePeriod( 3600 );\r
config.addRepository( repo2 );\r
assertEquals( 2, config.getRepositories().size() );\r
\r
List repositories = config.getRepositories();\r
ProxyRepository repo = (ProxyRepository) repositories.get( 0 );\r
+ assertEquals( "repo1", repo.getId() );\r
+ assertEquals( "http://www.ibiblio.org/maven2", repo.getUrl() );\r
+ assertTrue( repo.isCacheFailures() );\r
+ assertEquals( 0, repo.getCachePeriod() );\r
assertEquals( repo1, repo );\r
\r
repo = (ProxyRepository) repositories.get( 1 );\r
+ assertEquals( "repo2", repo.getId() );\r
+ assertEquals( "http://www.ibiblio.org/maven", repo.getUrl() );\r
+ assertFalse( repo.isCacheFailures() );\r
+ assertEquals( 3600, repo.getCachePeriod() );\r
assertEquals( repo2, repo );\r
\r
try\r
\r
config.loadMavenProxyConfiguration( confFile );\r
\r
- assertTrue( config.getRepositoryCachePath().endsWith( "target" ) );\r
+ assertTrue( "cache path changed", config.getRepositoryCachePath().endsWith( "target" ) );\r
\r
assertEquals( "Count repositories", 4, config.getRepositories().size() );\r
\r
+ int idx = 0;\r
for ( Iterator repos = config.getRepositories().iterator(); repos.hasNext(); )\r
{\r
+ idx++;\r
+\r
ProxyRepository repo = (ProxyRepository) repos.next();\r
\r
- if ( "local-repo".equals( repo.getKey() ) )\r
- {\r
- assertEquals( "file:///./target/remote-repo1", repo.getUrl() );\r
- }\r
- else if ( "www-ibiblio-org".equals( repo.getKey() ) )\r
- {\r
- assertEquals( "http://www.ibiblio.org/maven2", repo.getUrl() );\r
- }\r
- else if ( "dist-codehaus-org".equals( repo.getKey() ) )\r
- {\r
- assertEquals( "http://dist.codehaus.org", repo.getUrl() );\r
- }\r
- else if ( "private-example-com".equals( repo.getKey() ) )\r
+ //switch is made to check for ordering\r
+ switch ( idx )\r
{\r
- assertEquals( "http://private.example.com/internal", repo.getUrl() );\r
+ case 1:\r
+ assertEquals( "Repository name not as expected", "local-repo", repo.getKey() );\r
+ assertEquals( "Repository url does not match its name", "file:///./target/remote-repo1",\r
+ repo.getUrl() );\r
+ assertEquals( "Repository cache period check failed", 0, repo.getCachePeriod() );\r
+ assertFalse( "Repository failure caching check failed", repo.isCacheFailures() );\r
+ break;\r
+ case 2:\r
+ assertEquals( "Repository name not as expected", "www-ibiblio-org", repo.getKey() );\r
+ assertEquals( "Repository url does not match its name", "http://www.ibiblio.org/maven2",\r
+ repo.getUrl() );\r
+ assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );\r
+ assertTrue( "Repository failure caching check failed", repo.isCacheFailures() );\r
+ break;\r
+ case 3:\r
+ assertEquals( "Repository name not as expected", "dist-codehaus-org", repo.getKey() );\r
+ assertEquals( "Repository url does not match its name", "http://dist.codehaus.org",\r
+ repo.getUrl() );\r
+ assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );\r
+ assertTrue( "Repository failure caching check failed", repo.isCacheFailures() );\r
+ break;\r
+ case 4:\r
+ assertEquals( "Repository name not as expected", "private-example-com", repo.getKey() );\r
+ assertEquals( "Repository url does not match its name", "http://private.example.com/internal",\r
+ repo.getUrl() );\r
+ assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );\r
+ assertFalse( "Repository failure caching check failed", repo.isCacheFailures() );\r
+ break;\r
+ default:\r
+ fail( "Unexpected order count" );\r
}\r
}\r
}\r