Author: epunzalan Date: Fri Feb 3 18:26:29 2006 New Revision: 374817 URL: http://svn.apache.org/viewcvs?rev=374817&view=rev Log: PR: MRM-43
Added digester usage from utils Removed m1 client support from DefaultProxyManager.java (m1 support should be done in another class, maybe LegacyProxyManager) Removed unused classes: Checksum.java and DefaultRepositoryFileManager.java Added unit tests for configuration package Added: maven/repository-manager/trunk/maven-repository-proxy/src/test/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java Removed: maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/files/ Modified: maven/repository-manager/trunk/maven-repository-proxy/pom.xml 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/ProxyManager.java maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/repository/ProxyRepository.java Modified: maven/repository-manager/trunk/maven-repository-proxy/pom.xml URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-proxy/pom.xml?rev=374817&r1=374816&r2=374817&view=diff ============================================================================== --- maven/repository-manager/trunk/maven-repository-proxy/pom.xml (original) +++ maven/repository-manager/trunk/maven-repository-proxy/pom.xml Fri Feb 3 18:26:29 2006 @@ -26,6 +26,10 @@ <name>Maven Repository Proxy</name> <dependencies> <dependency> + <groupId>org.apache.maven.repository</groupId> + <artifactId>maven-repository-utils</artifactId> + </dependency> + <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-artifact</artifactId> </dependency> 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=374817&r1=374816&r2=374817&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 Fri Feb 3 18:26:29 2006 @@ -17,12 +17,14 @@ */ import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.manager.ChecksumFailedException; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.repository.ArtifactUtils; +import org.apache.maven.repository.digest.DefaultDigester; +import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.proxy.configuration.ProxyConfiguration; -import org.apache.maven.repository.proxy.files.Checksum; -import org.apache.maven.repository.proxy.files.DefaultRepositoryFileManager; import org.apache.maven.repository.proxy.repository.ProxyRepository; import org.apache.maven.wagon.ConnectionException; import org.apache.maven.wagon.ResourceDoesNotExistException; @@ -32,22 +34,34 @@ import org.apache.maven.wagon.authentication.AuthenticationException; import org.apache.maven.wagon.authorization.AuthorizationException; import org.apache.maven.wagon.observers.ChecksumObserver; +import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.io.IOException; import java.security.NoSuchAlgorithmException; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; /** * @author Edwin Punzalan + * @plexus.component role="org.apache.maven.repository.proxy.ProxyManager" */ public class DefaultProxyManager - //implements ProxyManager + extends AbstractLogEnabled + implements ProxyManager { - /* @plexus.requirement */ + /** + * @plexus.requirement + */ private WagonManager wagon; + /** + * @plexus.requirement + */ + private ArtifactFactory artifactFactory; + private ProxyConfiguration config; public DefaultProxyManager( ProxyConfiguration configuration ) @@ -58,6 +72,7 @@ public File get( String path ) throws ProxyException { + //@todo use wagon for cache use file:// as URL String cachePath = config.getRepositoryCachePath(); File cachedFile = new File( cachePath, path ); if ( !cachedFile.exists() ) @@ -72,40 +87,24 @@ { try { - if ( path.indexOf( "/jars/" ) >= 0 ) + Artifact artifact = ArtifactUtils.buildArtifact( path, artifactFactory ); + + File remoteFile; + if ( artifact != null ) { - //@todo maven 1 repo request - throw new ProxyException( "Maven 1 repository requests not yet supported." ); + remoteFile = getArtifactFile( artifact ); } - else if ( path.indexOf( "/poms/" ) >= 0 ) + else if ( path.endsWith( ".md5" ) || path.endsWith( ".sha1" ) ) { - //@todo maven 1 repo request - throw new ProxyException( "Maven 1 repository requests not yet supported." ); + remoteFile = getRepositoryFile( path, false ); } else { - //maven 2 repo request - Object obj = new DefaultRepositoryFileManager().getRequestedObjectFromPath( path ); - - if ( obj == null ) - { - //right now, only metadata is known to fit here - return getRepositoryFile( path ); - } - else if ( obj instanceof Checksum ) - { - return getRepositoryFile( path, false ); - } - else if ( obj instanceof Artifact ) - { - Artifact artifact = (Artifact) obj; - return getArtifactFile( artifact ); - } - else - { - throw new ProxyException( "Could not hande repository object: " + obj.getClass() ); - } + // as of now, only metadata fits here + remoteFile = getRepositoryFile( path ); } + + return remoteFile; } catch ( TransferFailedException e ) { @@ -155,20 +154,10 @@ //@todo configure wagon - ChecksumObserver listener = null; - try - { - listener = repository.getChecksumObserver(); - - if ( listener != null ) - { - wagon.addTransferListener( listener ); - } - } - catch ( NoSuchAlgorithmException e ) + Map checksums = null; + if ( useChecksum ) { - System.out.println( - "Skipping checksum validation for unsupported algorithm: " + repository.getChecksum() ); + checksums = prepareChecksums( wagon ); } if ( connectToRepository( wagon, repository ) ) @@ -187,7 +176,8 @@ if ( useChecksum ) { - success = doChecksumCheck( listener, repository, path, wagon ); + releaseChecksums( wagon, checksums ); + success = doChecksumCheck( checksums, repository, path, wagon ); } else { @@ -207,7 +197,7 @@ } catch ( TransferFailedException e ) { - System.out.println( "Skipping repository " + repository.getUrl() + ": " + e.getMessage() ); + getLogger().info( "Skipping repository " + repository.getUrl() + ": " + e.getMessage() ); } catch ( ResourceDoesNotExistException e ) { @@ -215,18 +205,47 @@ } catch ( AuthorizationException e ) { - System.out.println( "Skipping repository " + repository.getUrl() + ": " + e.getMessage() ); + getLogger().info( "Skipping repository " + repository.getUrl() + ": " + e.getMessage() ); } catch ( UnsupportedProtocolException e ) { - System.out.println( "Skipping repository " + repository.getUrl() + - ": no wagon configured for protocol " + repository.getProtocol() ); + getLogger().info( "Skipping repository " + repository.getUrl() + ": no wagon configured for protocol " + + repository.getProtocol() ); } } throw new ProxyException( "Could not find " + path + " in any of the repositories." ); } + private Map prepareChecksums( Wagon wagon ) + { + Map checksums = new HashMap(); + try + { + ChecksumObserver checksum = new ChecksumObserver( "SHA-1" ); + wagon.addTransferListener( checksum ); + checksums.put( "sha1", checksum ); + + checksum = new ChecksumObserver( "MD5" ); + wagon.addTransferListener( checksum ); + checksums.put( "md5", checksum ); + } + catch ( NoSuchAlgorithmException e ) + { + getLogger().info( "An error occurred while preparing checksum observers", e ); + } + return checksums; + } + + private void releaseChecksums( Wagon wagon, Map checksumMap ) + { + for ( Iterator checksums = checksumMap.values().iterator(); checksums.hasNext(); ) + { + ChecksumObserver listener = (ChecksumObserver) checksums.next(); + wagon.removeTransferListener( listener ); + } + } + private boolean connectToRepository( Wagon wagon, ProxyRepository repository ) { boolean connected = false; @@ -237,44 +256,87 @@ } catch ( ConnectionException e ) { - System.out.println( "Could not connect to " + repository.getId() + ": " + e.getMessage() ); + getLogger().info( "Could not connect to " + repository.getId() + ": " + e.getMessage() ); } catch ( AuthenticationException e ) { - System.out.println( "Could not connect to " + repository.getId() + ": " + e.getMessage() ); + getLogger().info( "Could not connect to " + repository.getId() + ": " + e.getMessage() ); } return connected; } - private boolean doChecksumCheck( ChecksumObserver listener, ProxyRepository repository, String path, Wagon wagon ) - //throws ChecksumFailedException + private boolean doChecksumCheck( Map checksumMap, ProxyRepository repository, String path, Wagon wagon ) { - boolean success = false; - - try + for ( Iterator checksums = checksumMap.keySet().iterator(); checksums.hasNext(); ) { - String checksumExt = repository.getChecksum().getFileExtension(); + String checksumExt = (String) checksums.next(); + ChecksumObserver checksum = (ChecksumObserver) checksumMap.get( checksumExt ); String remotePath = path + "." + checksumExt; File checksumFile = new File( config.getRepositoryCache().getBasedir(), remotePath ); - verifyChecksum( listener.getActualChecksum(), checksumFile, remotePath, checksumExt, wagon ); + try + { + File tempChecksumFile = new File( checksumFile.getAbsolutePath() + "." + checksumExt ); + + wagon.get( remotePath + "." + checksumExt, tempChecksumFile ); - wagon.removeTransferListener( listener ); + String algorithm; + if ( "md5".equals( checksumExt ) ) + { + algorithm = "MD5"; + } + else + { + algorithm = "SHA-1"; + } - success = true; - } - catch ( ChecksumFailedException e ) - { - System.out.println( "*** CHECKSUM FAILED - " + e.getMessage() + " - RETRYING" ); + Digester digester = new DefaultDigester(); + try + { + return digester.verifyChecksum( tempChecksumFile, checksum.getActualChecksum(), algorithm ); + } + catch ( NoSuchAlgorithmException e ) + { + getLogger().info( "Failed to initialize checksum: " + algorithm + "\n " + e.getMessage() ); + return false; + } + catch ( IOException e ) + { + getLogger().info( "Failed to verify checksum: " + algorithm + "\n " + e.getMessage() ); + return false; + } + + } + catch ( ChecksumFailedException e ) + { + return false; + } + catch ( TransferFailedException e ) + { + getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() ); + // do nothing try the next checksum + } + catch ( ResourceDoesNotExistException e ) + { + getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() ); + // do nothing try the next checksum + } + catch ( AuthorizationException e ) + { + getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() ); + // do nothing try the next checksum + } } - return success; + getLogger().info( "Skipping checksum validation for " + path + ": No remote checksums available." ); + + return true; } private void verifyChecksum( String actualChecksum, File destination, String remotePath, String checksumFileExtension, Wagon wagon ) - throws ChecksumFailedException + throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { try { @@ -323,18 +385,6 @@ "'; remote = '" + expectedChecksum + "'" ); } } - catch ( TransferFailedException e ) - { - System.out.println( "Skipping checksum validation for " + remotePath + ": " + e.getMessage() ); - } - catch ( ResourceDoesNotExistException e ) - { - System.out.println( "Skipping checksum validation for " + remotePath + ": " + e.getMessage() ); - } - catch ( AuthorizationException e ) - { - System.out.println( "Skipping checksum validation for " + remotePath + ": " + e.getMessage() ); - } catch ( IOException e ) { throw new ChecksumFailedException( "Invalid checksum file", e ); @@ -349,7 +399,7 @@ } catch ( ConnectionException e ) { - System.err.println( "Problem disconnecting from wagon - ignoring: " + e.getMessage() ); + getLogger().error( "Problem disconnecting from wagon - ignoring: " + e.getMessage() ); } } } Modified: maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java?rev=374817&r1=374816&r2=374817&view=diff ============================================================================== --- maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java (original) +++ maven/repository-manager/trunk/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java Fri Feb 3 18:26:29 2006 @@ -16,22 +16,16 @@ * limitations under the License. */ -import org.apache.maven.artifact.Artifact; -import org.apache.maven.wagon.ResourceDoesNotExistException; -import org.apache.maven.wagon.TransferFailedException; - import java.io.File; -import java.io.IOException; -import java.io.InputStream; /** * @author Edwin Punzalan */ public interface ProxyManager { - File getArtifactFile( Artifact artifact ) - throws TransferFailedException, ResourceDoesNotExistException, IOException; + public File get( String path ) + throws ProxyException; - InputStream getArtifactAsStream( Artifact artifact ) - throws TransferFailedException, ResourceDoesNotExistException, IOException; + public File getRemoteFile( String path ) + throws ProxyException; } 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=374817&r1=374816&r2=374817&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 Fri Feb 3 18:26:29 2006 @@ -18,10 +18,6 @@ import org.apache.maven.artifact.repository.DefaultArtifactRepository; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.repository.proxy.files.Checksum; -import org.apache.maven.wagon.observers.ChecksumObserver; - -import java.security.NoSuchAlgorithmException; /** * @author Edwin Punzalan @@ -29,26 +25,8 @@ public class ProxyRepository extends DefaultArtifactRepository { - private Checksum checksum; - public ProxyRepository( String id, String url, ArtifactRepositoryLayout layout ) { super( id, url, layout ); - } - - public void setChecksum( String algorithm ) - { - this.checksum = new Checksum( algorithm ); - } - - public Checksum getChecksum() - { - return checksum; - } - - public ChecksumObserver getChecksumObserver() - throws NoSuchAlgorithmException - { - return new ChecksumObserver( checksum.getAlgorithm() ); } } Added: 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=374817&view=auto ============================================================================== --- maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java (added) +++ maven/repository-manager/trunk/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/configuration/ProxyConfigurationTest.java Fri Feb 3 18:26:29 2006 @@ -0,0 +1,103 @@ +package org.apache.maven.repository.proxy.configuration; + +/* + * Copyright 2005-2006 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. + */ + +import org.apache.maven.artifact.repository.ArtifactRepository; +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.codehaus.plexus.PlexusTestCase; + +import java.io.File; +import java.util.List; +import java.util.ArrayList; + +public class ProxyConfigurationTest + extends PlexusTestCase +{ + private ProxyConfiguration config; + + protected void setUp() + throws Exception + { + super.setUp(); + + config = (ProxyConfiguration) container.lookup( ProxyConfiguration.ROLE ); + } + + public void testBrowsable() + { + assertFalse( config.isBrowsable() ); + config.setBrowsable( true ); + assertTrue( config.isBrowsable() ); + } + + public void testRepositoryCache() + { + File cacheFile = new File( "target/proxy-cache" ); + config.setRepositoryCachePath( "file://" + cacheFile.getAbsolutePath() ); + ArtifactRepository cache = config.getRepositoryCache(); + System.out.println( cache.getUrl() ); + assertEquals( cacheFile.getAbsolutePath(), cache.getBasedir() ); + assertEquals( config.getRepositoryCachePath(), cache.getBasedir() ); + } + + public void testRepositories() + { + ArtifactRepositoryLayout defLayout = new DefaultRepositoryLayout(); + ProxyRepository repo1 = new ProxyRepository( "repo1", "http://www.ibiblio.org/maven2", defLayout ); + config.addRepository( repo1 ); + assertEquals( 1, config.getRepositories().size() ); + + ArtifactRepositoryLayout legacyLayout = new LegacyRepositoryLayout(); + ProxyRepository repo2 = new ProxyRepository( "repo2", "http://www.ibiblio.org/maven", legacyLayout ); + config.addRepository( repo2 ); + assertEquals( 2, config.getRepositories().size() ); + + List repositories = config.getRepositories(); + ProxyRepository repo = (ProxyRepository) repositories.get( 0 ); + assertEquals( repo1, repo ); + + repo = (ProxyRepository) repositories.get( 1 ); + assertEquals( repo2, repo ); + + try + { + repositories.add( new ProxyRepository( "repo", "url", defLayout ) ); + fail( "Expected UnsupportedOperationException not thrown." ); + } + catch ( java.lang.UnsupportedOperationException e ) + { + assertTrue( true ); + } + + repositories = new ArrayList(); + repositories.add( repo1 ); + repositories.add( repo2 ); + config.setRepositories( repositories ); + assertEquals( repositories, config.getRepositories() ); + } + + protected void tearDown() + throws Exception + { + config = null; + + super.tearDown(); + } +} \ No newline at end of file