From: Edwin L. Punzalan Date: Wed, 8 Feb 2006 02:10:14 +0000 (+0000) Subject: PR: MRM-43 X-Git-Tag: archiva-0.9-alpha-1~945 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=321a75da80860c13d72185f92247d2f0ff91cbe1;p=archiva.git PR: MRM-43 Added a factory and made sure the proxy instance will always have a configuration when run. Current unit tests status also included. git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@375827 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 a0a2239ff..e4836fea8 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 @@ -45,7 +45,7 @@ import java.util.Map; /** * @author Edwin Punzalan - * @plexus.component role="org.apache.maven.repository.proxy.ProxyManager" + * @plexus.component role="org.apache.maven.repository.proxy.ProxyManager" role-hint="default" */ public class DefaultProxyManager extends AbstractLogEnabled @@ -54,7 +54,7 @@ public class DefaultProxyManager /** * @plexus.requirement */ - private WagonManager wagon; + private WagonManager wagonManager; /** * @plexus.requirement @@ -63,14 +63,14 @@ public class DefaultProxyManager private ProxyConfiguration config; - /** - * Constructor. - * - * @param configuration the configuration object to base the behavior of this instance - */ - public DefaultProxyManager( ProxyConfiguration configuration ) + public void setConfiguration( ProxyConfiguration config ) { - config = configuration; + this.config = config; + } + + public ProxyConfiguration getConfiguration() + { + return config; } /** @@ -79,7 +79,9 @@ public class DefaultProxyManager public File get( String path ) throws ProxyException, ResourceDoesNotExistException { - //@todo use wagon for cache use file:// as URL + checkConfiguration(); + + //@todo use wagonManager for cache use file:// as URL String cachePath = config.getRepositoryCachePath(); File cachedFile = new File( cachePath, path ); if ( !cachedFile.exists() ) @@ -95,6 +97,8 @@ public class DefaultProxyManager public File getRemoteFile( String path ) throws ProxyException, ResourceDoesNotExistException { + checkConfiguration(); + Artifact artifact = ArtifactUtils.buildArtifact( path, artifactFactory ); File remoteFile; @@ -135,7 +139,7 @@ public class DefaultProxyManager { try { - wagon.getArtifact( artifact, config.getRepositories() ); + wagonManager.getArtifact( artifact, config.getRepositories() ); } catch ( TransferFailedException e ) { @@ -190,9 +194,9 @@ public class DefaultProxyManager try { - wagon = this.wagon.getWagon( repository.getProtocol() ); + wagon = wagonManager.getWagon( repository.getProtocol() ); - //@todo configure wagon + //@todo configure wagonManager if ( useChecksum ) { @@ -249,7 +253,7 @@ public class DefaultProxyManager } catch ( UnsupportedProtocolException e ) { - getLogger().info( "Skipping repository " + repository.getUrl() + ": no wagon configured for protocol " + + getLogger().info( "Skipping repository " + repository.getUrl() + ": no wagonManager configured for protocol " + repository.getProtocol() ); } finally @@ -270,10 +274,10 @@ public class DefaultProxyManager } /** - * Used to add checksum observers as transfer listeners to the wagon object + * Used to add checksum observers as transfer listeners to the wagonManager object * - * @param wagon the wagon object to use the checksum with - * @return map of ChecksumObservers added into the wagon transfer listeners + * @param wagon the wagonManager object to use the checksum with + * @return map of ChecksumObservers added into the wagonManager transfer listeners */ private Map prepareChecksums( Wagon wagon ) { @@ -296,10 +300,10 @@ public class DefaultProxyManager } /** - * Used to remove the ChecksumObservers from the wagon object + * Used to remove the ChecksumObservers from the wagonManager object * - * @param wagon the wagon object to remote the ChecksumObservers from - * @param checksumMap the map representing the list of ChecksumObservers added to the wagon object + * @param wagon the wagonManager object to remote the ChecksumObservers from + * @param checksumMap the map representing the list of ChecksumObservers added to the wagonManager object */ private void releaseChecksums( Wagon wagon, Map checksumMap ) { @@ -311,11 +315,11 @@ public class DefaultProxyManager } /** - * Used to request the wagon object to connect to a repository + * Used to request the wagonManager object to connect to a repository * - * @param wagon the wagon object that will be used to connect to the repository - * @param repository the repository object to connect the wagon to - * @return true when the wagon is able to connect to the repository + * @param wagon the wagonManager object that will be used to connect to the repository + * @param repository the repository object to connect the wagonManager to + * @return true when the wagonManager is able to connect to the repository */ private boolean connectToRepository( Wagon wagon, ProxyRepository repository ) { @@ -338,11 +342,11 @@ public class DefaultProxyManager } /** - * Used to verify the checksum during a wagon download + * Used to verify the checksum during a wagonManager download * - * @param checksumMap the map of ChecksumObservers present in the wagon as transferlisteners + * @param checksumMap the map of ChecksumObservers present in the wagonManager as transferlisteners * @param path path of the remote object whose checksum is to be verified - * @param wagon the wagon object used to download the requested path + * @param wagon the wagonManager object used to download the requested path * @return true when the checksum succeeds and false when the checksum failed. */ private boolean doChecksumCheck( Map checksumMap, String path, Wagon wagon ) @@ -413,10 +417,19 @@ public class DefaultProxyManager return true; } + private void checkConfiguration() + throws ProxyException + { + if ( config == null ) + { + throw new ProxyException( "No proxy configuration defined." ); + } + } + /** - * Used to disconnect the wagon from its repository + * Used to disconnect the wagonManager from its repository * - * @param wagon the connected wagon object + * @param wagon the connected wagonManager object */ private void disconnectWagon( Wagon wagon ) { @@ -426,7 +439,7 @@ public class DefaultProxyManager } catch ( ConnectionException e ) { - getLogger().error( "Problem disconnecting from wagon - ignoring: " + e.getMessage() ); + getLogger().error( "Problem disconnecting from wagonManager - ignoring: " + e.getMessage() ); } } } diff --git a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java index 239d3e784..b70d5af0b 100644 --- a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java +++ b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManager.java @@ -17,6 +17,7 @@ package org.apache.maven.repository.proxy; */ import org.apache.maven.wagon.ResourceDoesNotExistException; +import org.apache.maven.repository.proxy.configuration.ProxyConfiguration; import java.io.File; @@ -27,6 +28,8 @@ import java.io.File; */ public interface ProxyManager { + static String ROLE = ProxyManager.class.getName(); + /** * Used to retrieve a cached path or retrieve one if the cache does not contain it yet. * @@ -36,7 +39,7 @@ public interface ProxyManager * @throws ResourceDoesNotExistException when the requested object can't be found in any of the * configured repositories */ - public File get( String path ) + File get( String path ) throws ProxyException, ResourceDoesNotExistException; /** @@ -49,6 +52,20 @@ public interface ProxyManager * @throws ResourceDoesNotExistException when the requested object can't be found in any of the * configured repositories */ - public File getRemoteFile( String path ) + File getRemoteFile( String path ) throws ProxyException, ResourceDoesNotExistException; + + /** + * Used by the factory to set the configuration of the proxy + * + * @param config the ProxyConfiguration to set the behavior of the proxy + */ + void setConfiguration( ProxyConfiguration config ); + + /** + * Used to retrieve the configuration describing the behavior of the proxy + * + * @return the ProxyConfiguration of this proxy + */ + ProxyConfiguration getConfiguration(); } diff --git a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManagerFactory.java b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManagerFactory.java new file mode 100644 index 000000000..04b45cc5d --- /dev/null +++ b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/ProxyManagerFactory.java @@ -0,0 +1,52 @@ +package org.apache.maven.repository.proxy; + +/* + * 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.repository.proxy.configuration.ProxyConfiguration; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; +import org.codehaus.plexus.context.Context; +import org.codehaus.plexus.context.ContextException; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.PlexusConstants; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; + +/** + * @author Edwin Punzalan + * + * @plexus.component role="org.apache.maven.repository.proxy.ProxyManagerFactory" + */ +public class ProxyManagerFactory + implements Contextualizable +{ + public static String ROLE = "org.apache.maven.repository.proxy.ProxyManagerFactory"; + + private PlexusContainer container; + + public ProxyManager getProxyManager( String proxy_type, ProxyConfiguration config ) + throws ComponentLookupException + { + ProxyManager proxy = (ProxyManager) container.lookup( ProxyManager.ROLE, proxy_type ); + proxy.setConfiguration( config ); + return proxy; + } + + public void contextualize( Context context ) + throws ContextException + { + container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); + } +} diff --git a/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java b/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java new file mode 100644 index 000000000..af80b7dce --- /dev/null +++ b/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java @@ -0,0 +1,96 @@ +package org.apache.maven.repository.proxy; + +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.apache.maven.repository.proxy.configuration.ProxyConfiguration; +import org.apache.maven.wagon.ResourceDoesNotExistException; + +/* + * 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. + */ + +/** + * @author Edwin Punzalan + */ +public class DefaultProxyManagerTest + extends PlexusTestCase +{ + private ProxyManager proxy; + + protected void setUp() + throws Exception + { + super.setUp(); + + ProxyManagerFactory factory = (ProxyManagerFactory) container.lookup( ProxyManagerFactory.ROLE ); + proxy = factory.getProxyManager( "default", getTestConfiguration() ); + } + + public void testExceptions() + { + proxy.setConfiguration( null ); + + try + { + proxy.get( "/invalid" ); + fail( "Expected empty configuration error." ); + } + catch ( ProxyException e ) + { + assertEquals( "Expected Exception not thrown.", "No proxy configuration defined.", e.getMessage() ); + } + catch ( ResourceDoesNotExistException e ) + { + fail( "Expected Exception not thrown." ); + } + + try + { + proxy.getRemoteFile( "/invalid" ); + fail( "Expected empty configuration error." ); + } + catch ( ProxyException e ) + { + assertEquals( "Expected Exception not thrown.", "No proxy configuration defined.", e.getMessage() ); + } + catch ( ResourceDoesNotExistException e ) + { + fail( "Expected Exception not thrown." ); + } + } + + public void testCache() + { + + } + + protected void tearDown() + throws Exception + { + container.release( proxy ); + + super.tearDown(); + } + + private ProxyConfiguration getTestConfiguration() + throws ComponentLookupException + { + ProxyConfiguration config = (ProxyConfiguration) container.lookup( ProxyConfiguration.ROLE ); + + config.setRepositoryCachePath( "target/proxy-cache" ); + + return config; + } +} 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 a3103bda6..afd2363af 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 @@ -52,7 +52,6 @@ public class ProxyConfigurationTest 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() ); }