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
This commit is contained in:
Edwin L. Punzalan 2006-02-08 02:10:14 +00:00
parent 83eabf5929
commit 321a75da80
5 changed files with 210 additions and 33 deletions

View File

@ -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() );
}
}
}

View File

@ -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();
}

View File

@ -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 );
}
}

View File

@ -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;
}
}

View File

@ -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() );
}