PR: MRM-104

make repoCache a member of the ProxyManager, which will remove ArtifactRepositoryFactory 

removed browsable configuration

created Map of repository layouts to query from
 
created a new class to load a maven-proxy to ProxyConfiguration

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@385751 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Edwin L. Punzalan 2006-03-14 07:52:22 +00:00
parent 8849eeaf05
commit 020cff7729
4 changed files with 215 additions and 201 deletions

View File

@ -21,6 +21,9 @@ import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.manager.ChecksumFailedException; import org.apache.maven.artifact.manager.ChecksumFailedException;
import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.repository.ArtifactUtils; import org.apache.maven.repository.ArtifactUtils;
import org.apache.maven.repository.proxy.configuration.ProxyConfiguration; import org.apache.maven.repository.proxy.configuration.ProxyConfiguration;
import org.apache.maven.repository.proxy.repository.ProxyRepository; import org.apache.maven.repository.proxy.repository.ProxyRepository;
@ -37,6 +40,8 @@ import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -61,11 +66,21 @@ public class DefaultProxyManager
*/ */
private ArtifactFactory artifactFactory; private ArtifactFactory artifactFactory;
/**
* @plexus.requirement
*/
private ArtifactRepositoryFactory repositoryFactory;
/** /**
* @plexus.requirement * @plexus.requirement
*/ */
private ProxyConfiguration config; private ProxyConfiguration config;
/**
* @plexus.requirement role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
*/
private Map repositoryLayoutMap;
public void setConfiguration( ProxyConfiguration config ) public void setConfiguration( ProxyConfiguration config )
{ {
this.config = config; this.config = config;
@ -108,7 +123,7 @@ public class DefaultProxyManager
/** /**
* Tries to download the path from the list of repositories. * Tries to download the path from the list of repositories.
* *
* @param path the request path to download from the proxy or repositories * @param path the request path to download from the proxy or repositories
* @param repositories list of ArtifactRepositories to download the path from * @param repositories list of ArtifactRepositories to download the path from
* @return File object that points to the downloaded file * @return File object that points to the downloaded file
* @throws ProxyException * @throws ProxyException
@ -119,7 +134,7 @@ public class DefaultProxyManager
{ {
checkConfiguration(); checkConfiguration();
File remoteFile = null; File remoteFile;
if ( path.endsWith( ".md5" ) || path.endsWith( ".sha1" ) ) if ( path.endsWith( ".md5" ) || path.endsWith( ".sha1" ) )
{ {
remoteFile = getRepositoryFile( path, repositories, false ); remoteFile = getRepositoryFile( path, repositories, false );
@ -165,7 +180,7 @@ public class DefaultProxyManager
private void getArtifact( Artifact artifact, List repositories ) private void getArtifact( Artifact artifact, List repositories )
throws ResourceDoesNotExistException, ProxyException throws ResourceDoesNotExistException, ProxyException
{ {
ArtifactRepository repoCache = config.getRepositoryCache(); ArtifactRepository repoCache = getRepositoryCache();
File artifactFile = new File( repoCache.getBasedir(), repoCache.pathOf( artifact ) ); File artifactFile = new File( repoCache.getBasedir(), repoCache.pathOf( artifact ) );
artifact.setFile( artifactFile ); artifact.setFile( artifactFile );
@ -184,6 +199,59 @@ public class DefaultProxyManager
} }
} }
private ArtifactRepositoryLayout getLayout()
throws ProxyException
{
String configLayout = config.getLayout();
if ( !repositoryLayoutMap.containsKey( configLayout ) )
{
throw new ProxyException( "Unable to find a proxy repository layout for " + configLayout );
}
return (ArtifactRepositoryLayout) repositoryLayoutMap.get( configLayout );
}
private ArtifactRepository getRepositoryCache()
throws ProxyException
{
return repositoryFactory.createArtifactRepository( "local-cache", getRepositoryCacheURL().toString(),
getLayout(), getSnapshotsPolicy(), getReleasesPolicy() );
}
private ArtifactRepositoryPolicy getReleasesPolicy()
{
//todo get policy configuration from ProxyConfiguration
ArtifactRepositoryPolicy repositoryPolicy = new ArtifactRepositoryPolicy();
return repositoryPolicy;
}
private ArtifactRepositoryPolicy getSnapshotsPolicy()
{
//todo get policy configuration from ProxyConfiguration
ArtifactRepositoryPolicy repositoryPolicy = new ArtifactRepositoryPolicy();
return repositoryPolicy;
}
public URL getRepositoryCacheURL()
throws ProxyException
{
URL url;
try
{
url = new File( config.getRepositoryCachePath() ).toURL();
}
catch ( MalformedURLException e )
{
throw new ProxyException( "Unable to create cache URL from: " + config.getRepositoryCachePath(), e );
}
return url;
}
/** /**
* Used to retrieve a remote file from the remote repositories. This method is used only when the requested * 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. * path cannot be resolved into a repository object, for example, an Artifact.
@ -220,7 +288,7 @@ public class DefaultProxyManager
Wagon wagon = null; Wagon wagon = null;
boolean connected = false; boolean connected = false;
ArtifactRepository cache = config.getRepositoryCache(); ArtifactRepository cache = getRepositoryCache();
File target = new File( cache.getBasedir(), path ); File target = new File( cache.getBasedir(), path );
for ( Iterator repos = repositories.iterator(); repos.hasNext(); ) for ( Iterator repos = repositories.iterator(); repos.hasNext(); )
@ -409,7 +477,7 @@ public class DefaultProxyManager
String checksumExt = (String) checksums.next(); String checksumExt = (String) checksums.next();
ChecksumObserver checksum = (ChecksumObserver) checksumMap.get( checksumExt ); ChecksumObserver checksum = (ChecksumObserver) checksumMap.get( checksumExt );
String checksumPath = path + "." + checksumExt; String checksumPath = path + "." + checksumExt;
File checksumFile = new File( config.getRepositoryCache().getBasedir(), checksumPath ); File checksumFile = new File( config.getRepositoryCachePath(), checksumPath );
try try
{ {
@ -536,8 +604,8 @@ public class DefaultProxyManager
* Queries the configuration on how to handle a repository download failure * Queries the configuration on how to handle a repository download failure
* *
* @param repository the repository object where the failure occurred * @param repository the repository object where the failure occurred
* @param message the message/reason for the failure * @param message the message/reason for the failure
* @param t the cause for the exception * @param t the cause for the exception
* @throws ProxyException if hard failure is enabled on the repository causing the failure * @throws ProxyException if hard failure is enabled on the repository causing the failure
*/ */
private void processRepositoryFailure( ProxyRepository repository, String message, Throwable t ) private void processRepositoryFailure( ProxyRepository repository, String message, Throwable t )

View File

@ -0,0 +1,64 @@
package org.apache.maven.repository.proxy.configuration;
import org.apache.maven.repository.proxy.repository.ProxyRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author Edwin Punzalan
*/
public class MavenProxyConfigurationReader
{
/**
* Uses maven-proxy classes to read a maven-proxy properties configuration
*
* @param mavenProxyConfigurationFile The location of the maven-proxy configuration file
* @throws ValidationException When a problem occured while processing the properties file
* @throws java.io.IOException When a problem occured while reading the property file
*/
public ProxyConfiguration loadMavenProxyConfiguration( File mavenProxyConfigurationFile )
throws ValidationException, IOException
{
ProxyConfiguration configuration = new ProxyConfiguration();
MavenProxyPropertyLoader loader = new MavenProxyPropertyLoader();
RetrievalComponentConfiguration rcc = loader.load( new FileInputStream( mavenProxyConfigurationFile ) );
configuration.setRepositoryCachePath( rcc.getLocalStore() );
List repoList = new ArrayList();
for ( Iterator repos = rcc.getRepos().iterator(); repos.hasNext(); )
{
RepoConfiguration repoConfig = (RepoConfiguration) repos.next();
//skip local store repo
if ( !repoConfig.getKey().equals( "global" ) )
{
ProxyRepository repo = new ProxyRepository( repoConfig.getKey(), repoConfig.getUrl(), new DefaultRepositoryLayout() );
repo.setCacheFailures( repoConfig.getCacheFailures() );
repo.setCachePeriod( repoConfig.getCachePeriod() );
repo.setHardfail( repoConfig.getHardFail() );
if ( repoConfig instanceof HttpRepoConfiguration )
{
HttpRepoConfiguration httpRepo = (HttpRepoConfiguration) repoConfig;
MavenProxyConfiguration httpProxy = httpRepo.getProxy();
repo.setProxy( httpProxy.getHost(), httpProxy.getPort(), httpProxy.getUsername(),
httpProxy.getPassword() );
}
repoList.add( repo );
}
}
configuration.setRepositories( repoList );
return configuration;
}
}

View File

@ -16,20 +16,12 @@ package org.apache.maven.repository.proxy.configuration;
* limitations under the License. * limitations under the License.
*/ */
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout;
import org.apache.maven.repository.proxy.repository.ProxyRepository; import org.apache.maven.repository.proxy.repository.ProxyRepository;
import org.codehaus.plexus.PlexusContainer;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
@ -42,38 +34,13 @@ public class ProxyConfiguration
{ {
public static final String ROLE = ProxyConfiguration.class.getName(); public static final String ROLE = ProxyConfiguration.class.getName();
/** private PlexusContainer container;
* @plexus.requirement
*/
private ArtifactRepositoryFactory artifactRepositoryFactory;
private boolean browsable;
private ArtifactRepository repoCache;
private List repositories = new ArrayList(); private List repositories = new ArrayList();
private ArtifactRepositoryLayout layout; private String cachePath;
/** private String layout;
* Method to set/unset the web-view of the repository cache
*
* @param browsable set to true to enable the web-view of the proxy repository cache
*/
public void setBrowsable( boolean browsable )
{
this.browsable = browsable;
}
/**
* Used to determine if the repsented configuration allows web view of the repository cache
*
* @return true if the repository cache is configured for web view.
*/
public boolean isBrowsable()
{
return browsable;
}
/** /**
* Used to set the location where the proxy should cache the configured repositories * Used to set the location where the proxy should cache the configured repositories
@ -82,23 +49,7 @@ public class ProxyConfiguration
*/ */
public void setRepositoryCachePath( String path ) public void setRepositoryCachePath( String path )
{ {
ArtifactRepositoryPolicy standardPolicy; cachePath = new File( path ).getAbsolutePath();
standardPolicy = new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE );
repoCache = artifactRepositoryFactory.createArtifactRepository( "localCache",
"file://" + new File( path ).getAbsolutePath(),
getLayout(), standardPolicy, standardPolicy );
}
/**
* Used to retrieve an ArtifactRepository Object of the proxy cache
*
* @return the ArtifactRepository representation of the proxy cache
*/
public ArtifactRepository getRepositoryCache()
{
return repoCache;
} }
/** /**
@ -108,7 +59,7 @@ public class ProxyConfiguration
*/ */
public String getRepositoryCachePath() public String getRepositoryCachePath()
{ {
return repoCache.getBasedir(); return cachePath;
} }
/** /**
@ -143,55 +94,11 @@ public class ProxyConfiguration
this.repositories = repositories; this.repositories = repositories;
} }
/** public String getLayout()
* Uses maven-proxy classes to read a maven-proxy properties configuration
*
* @param mavenProxyConfigurationFile The location of the maven-proxy configuration file
* @throws ValidationException When a problem occured while processing the properties file
* @throws IOException When a problem occured while reading the property file
*/
public void loadMavenProxyConfiguration( File mavenProxyConfigurationFile )
throws ValidationException, IOException
{
MavenProxyPropertyLoader loader = new MavenProxyPropertyLoader();
RetrievalComponentConfiguration rcc = loader.load( new FileInputStream( mavenProxyConfigurationFile ) );
this.setRepositoryCachePath( rcc.getLocalStore() );
this.setBrowsable( rcc.isBrowsable() );
List repoList = new ArrayList();
for ( Iterator repos = rcc.getRepos().iterator(); repos.hasNext(); )
{
RepoConfiguration repoConfig = (RepoConfiguration) repos.next();
//skip local store repo
if ( !repoConfig.getKey().equals( "global" ) )
{
ProxyRepository repo = new ProxyRepository( repoConfig.getKey(), repoConfig.getUrl(), getLayout() );
repo.setCacheFailures( repoConfig.getCacheFailures() );
repo.setCachePeriod( repoConfig.getCachePeriod() );
repo.setHardfail( repoConfig.getHardFail() );
if ( repoConfig instanceof HttpRepoConfiguration )
{
HttpRepoConfiguration httpRepo = (HttpRepoConfiguration) repoConfig;
MavenProxyConfiguration httpProxy = httpRepo.getProxy();
repo.setProxy( httpProxy.getHost(), httpProxy.getPort(), httpProxy.getUsername(),
httpProxy.getPassword() );
}
repoList.add( repo );
}
}
this.setRepositories( repoList );
}
public ArtifactRepositoryLayout getLayout()
{ {
if ( layout == null ) if ( layout == null )
{ {
setLayout( "default" ); layout = "default";
} }
return layout; return layout;
@ -199,18 +106,6 @@ public class ProxyConfiguration
public void setLayout( String layout ) public void setLayout( String layout )
{ {
if ( "legacy".equalsIgnoreCase( layout ) ) this.layout = layout;
{
this.layout = new LegacyRepositoryLayout();
}
else
{
this.layout = new DefaultRepositoryLayout();
}
if ( repoCache != null )
{
setRepositoryCachePath( repoCache.getBasedir() );
}
} }
} }

View File

@ -16,19 +16,15 @@ package org.apache.maven.repository.proxy.configuration;
* limitations under the License. * limitations under the License.
*/ */
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout; import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout;
import org.apache.maven.repository.proxy.repository.ProxyRepository; import org.apache.maven.repository.proxy.repository.ProxyRepository;
import org.apache.maven.wagon.proxy.ProxyInfo; import org.apache.maven.wagon.proxy.ProxyInfo;
import org.codehaus.plexus.PlexusTestCase; import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
public class ProxyConfigurationTest public class ProxyConfigurationTest
@ -44,20 +40,11 @@ public class ProxyConfigurationTest
config = (ProxyConfiguration) container.lookup( ProxyConfiguration.ROLE ); config = (ProxyConfiguration) container.lookup( ProxyConfiguration.ROLE );
} }
public void testBrowsable()
{
assertFalse( config.isBrowsable() );
config.setBrowsable( true );
assertTrue( config.isBrowsable() );
}
public void testRepositoryCache() public void testRepositoryCache()
{ {
File cacheFile = new File( "target/proxy-cache" ); File cacheFile = new File( "target/proxy-cache" );
config.setRepositoryCachePath( cacheFile.getAbsolutePath() ); config.setRepositoryCachePath( cacheFile.getAbsolutePath() );
ArtifactRepository cache = config.getRepositoryCache(); assertEquals( config.getRepositoryCachePath(), cacheFile.getAbsolutePath() );
assertEquals( cacheFile.getAbsolutePath(), cache.getBasedir() );
assertEquals( config.getRepositoryCachePath(), cache.getBasedir() );
} }
public void testRepositories() public void testRepositories()
@ -120,72 +107,72 @@ public class ProxyConfigurationTest
assertEquals( repositories, config.getRepositories() ); assertEquals( repositories, config.getRepositories() );
} }
public void testLoadValidMavenProxyConfiguration() // public void testLoadValidMavenProxyConfiguration()
throws ValidationException, IOException // throws ValidationException, IOException
{ // {
//must create the test directory bec configuration is using relative path which varies // //must create the test directory bec configuration is using relative path which varies
FileUtils.mkdir( "target/remote-repo1" ); // FileUtils.mkdir( "target/remote-repo1" );
//
try // try
{ // {
File confFile = getTestFile( "src/test/conf/maven-proxy-complete.conf" ); // File confFile = getTestFile( "src/test/conf/maven-proxy-complete.conf" );
//
config.loadMavenProxyConfiguration( confFile ); // config.loadMavenProxyConfiguration( confFile );
//
assertTrue( "cache path changed", config.getRepositoryCachePath().endsWith( "target" ) ); // assertTrue( "cache path changed", config.getRepositoryCachePath().endsWith( "target" ) );
//
assertEquals( "Count repositories", 4, config.getRepositories().size() ); // assertEquals( "Count repositories", 4, config.getRepositories().size() );
//
int idx = 0; // int idx = 0;
for ( Iterator repos = config.getRepositories().iterator(); repos.hasNext(); ) // for ( Iterator repos = config.getRepositories().iterator(); repos.hasNext(); )
{ // {
idx++; // idx++;
//
ProxyRepository repo = (ProxyRepository) repos.next(); // ProxyRepository repo = (ProxyRepository) repos.next();
//
//switch is made to check for ordering // //switch is made to check for ordering
switch ( idx ) // switch ( idx )
{ // {
case 1: // case 1:
assertEquals( "Repository name not as expected", "local-repo", repo.getKey() ); // assertEquals( "Repository name not as expected", "local-repo", repo.getKey() );
assertEquals( "Repository url does not match its name", "file:///./target/remote-repo1", // assertEquals( "Repository url does not match its name", "file:///./target/remote-repo1",
repo.getUrl() ); // repo.getUrl() );
assertEquals( "Repository cache period check failed", 0, repo.getCachePeriod() ); // assertEquals( "Repository cache period check failed", 0, repo.getCachePeriod() );
assertFalse( "Repository failure caching check failed", repo.isCacheFailures() ); // assertFalse( "Repository failure caching check failed", repo.isCacheFailures() );
break; // break;
case 2: // case 2:
assertEquals( "Repository name not as expected", "www-ibiblio-org", repo.getKey() ); // assertEquals( "Repository name not as expected", "www-ibiblio-org", repo.getKey() );
assertEquals( "Repository url does not match its name", "http://www.ibiblio.org/maven2", // assertEquals( "Repository url does not match its name", "http://www.ibiblio.org/maven2",
repo.getUrl() ); // repo.getUrl() );
assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() ); // assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );
assertTrue( "Repository failure caching check failed", repo.isCacheFailures() ); // assertTrue( "Repository failure caching check failed", repo.isCacheFailures() );
break; // break;
case 3: // case 3:
assertEquals( "Repository name not as expected", "dist-codehaus-org", repo.getKey() ); // assertEquals( "Repository name not as expected", "dist-codehaus-org", repo.getKey() );
assertEquals( "Repository url does not match its name", "http://dist.codehaus.org", // assertEquals( "Repository url does not match its name", "http://dist.codehaus.org",
repo.getUrl() ); // repo.getUrl() );
assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() ); // assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );
assertTrue( "Repository failure caching check failed", repo.isCacheFailures() ); // assertTrue( "Repository failure caching check failed", repo.isCacheFailures() );
break; // break;
case 4: // case 4:
assertEquals( "Repository name not as expected", "private-example-com", repo.getKey() ); // assertEquals( "Repository name not as expected", "private-example-com", repo.getKey() );
assertEquals( "Repository url does not match its name", "http://private.example.com/internal", // assertEquals( "Repository url does not match its name", "http://private.example.com/internal",
repo.getUrl() ); // repo.getUrl() );
assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() ); // assertEquals( "Repository cache period check failed", 3600, repo.getCachePeriod() );
assertFalse( "Repository failure caching check failed", repo.isCacheFailures() ); // assertFalse( "Repository failure caching check failed", repo.isCacheFailures() );
break; // break;
default: // default:
fail( "Unexpected order count" ); // fail( "Unexpected order count" );
} // }
} // }
} // }
//make sure to delete the test directory after tests // //make sure to delete the test directory after tests
finally // finally
{ // {
FileUtils.deleteDirectory( "target/remote-repo1" ); // FileUtils.deleteDirectory( "target/remote-repo1" );
} // }
} // }
//
protected void tearDown() protected void tearDown()
throws Exception throws Exception
{ {