Refactoring proxy classes

This commit is contained in:
Martin Stockhammer 2019-09-03 21:42:35 +02:00
parent ebf95723a5
commit 926dc7990b
5 changed files with 46 additions and 63 deletions

View File

@ -19,7 +19,9 @@ package org.apache.archiva.proxy.model;
* under the License.
*/
import org.apache.archiva.repository.ManagedRepository;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RemoteRepository;
import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.connector.RepositoryConnector;
@ -33,9 +35,9 @@ import java.util.Map;
public class ProxyConnector
implements RepositoryConnector
{
private ManagedRepositoryContent sourceRepository;
private ManagedRepository sourceRepository;
private RemoteRepositoryContent targetRepository;
private RemoteRepository targetRepository;
private List<String> blacklist;
@ -80,23 +82,23 @@ public class ProxyConnector
}
@Override
public ManagedRepositoryContent getSourceRepository()
public ManagedRepository getSourceRepository()
{
return sourceRepository;
}
public void setSourceRepository( ManagedRepositoryContent sourceRepository )
public void setSourceRepository( ManagedRepository sourceRepository )
{
this.sourceRepository = sourceRepository;
}
@Override
public RemoteRepositoryContent getTargetRepository()
public RemoteRepository getTargetRepository()
{
return targetRepository;
}
public void setTargetRepository( RemoteRepositoryContent targetRepository )
public void setTargetRepository( RemoteRepository targetRepository )
{
this.targetRepository = targetRepository;
}
@ -138,8 +140,8 @@ public class ProxyConnector
StringBuilder sb = new StringBuilder();
sb.append( "ProxyConnector[\n" );
sb.append( " source: [managed] " ).append( this.sourceRepository.getRepoRoot() ).append( "\n" );
sb.append( " target: [remote] " ).append( this.targetRepository.getRepository().getLocation() ).append( "\n" );
sb.append( " source: [managed] " ).append( this.sourceRepository.getId() ).append( "\n" );
sb.append( " target: [remote] " ).append( this.targetRepository.getId() ).append( "\n" );
sb.append( " proxyId:" ).append( this.proxyId ).append( "\n" );
Iterator<String> keys = this.policies.keySet().iterator();

View File

@ -23,10 +23,7 @@ import org.apache.archiva.configuration.*;
import org.apache.archiva.proxy.model.NetworkProxy;
import org.apache.archiva.proxy.model.ProxyConnector;
import org.apache.archiva.proxy.model.RepositoryProxyHandler;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@ -128,9 +125,9 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene
proxyConnector.setPolicies(configuration.getPolicies());
proxyConnector.setProperties(configuration.getProperties());
proxyConnector.setProxyId(configuration.getProxyId());
ManagedRepositoryContent srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId()).getContent();
ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId());
proxyConnector.setSourceRepository(srcRepo);
RemoteRepositoryContent targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId()).getContent();
RemoteRepository targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId());
proxyConnector.setTargetRepository(targetRepo);
return proxyConnector;
}

View File

@ -73,12 +73,11 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
@Inject
@Named(value = "archivaConfiguration#default")
private ArchivaConfiguration archivaConfiguration;
@Inject
@Named(value = "repositoryContentFactory#default")
private RepositoryContentFactory repositoryFactory;
@Inject
@Named(value = "metadataTools#default")
private MetadataTools metadataTools;
@Inject
private Map<String, PreDownloadPolicy> preDownloadPolicies;
@Inject
@ -86,11 +85,13 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
@Inject
private Map<String, DownloadErrorPolicy> downloadErrorPolicies;
private ConcurrentMap<String, List<ProxyConnector>> proxyConnectorMap = new ConcurrentHashMap<>();
@Inject
@Named(value = "archivaTaskScheduler#repository")
private ArchivaTaskScheduler<RepositoryTask> scheduler;
@Inject
private RepositoryRegistry repositoryRegistry;
@Inject
@Named(value = "fileLockManager#default")
private FileLockManager fileLockManager;
@ -132,13 +133,13 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
log.error("Cannot find source repository after config change "+proxyConfig.getSourceRepoId());
continue;
}
connector.setSourceRepository(repo.getContent());
connector.setSourceRepository(repo);
RemoteRepository rRepo = repositoryRegistry.getRemoteRepository( proxyConfig.getTargetRepoId() );
if (rRepo==null) {
log.error("Cannot find target repository after config change "+proxyConfig.getSourceRepoId());
continue;
}
connector.setTargetRepository(rRepo.getContent());
connector.setTargetRepository(rRepo);
connector.setProxyId( proxyConfig.getProxyId() );
connector.setPolicies( proxyConfig.getPolicies() );
@ -266,10 +267,10 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
continue;
}
RemoteRepositoryContent targetRepository = connector.getTargetRepository();
RemoteRepository targetRepository = connector.getTargetRepository();
requestProperties.setProperty( "remoteRepositoryId", targetRepository.getId() );
String targetPath = targetRepository.toPath( artifact );
String targetPath = targetRepository.getContent().toPath( artifact );
if ( SystemUtils.IS_OS_WINDOWS )
{
@ -280,7 +281,7 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
try
{
StorageAsset downloadedFile =
transferFile( connector, targetRepository, targetPath, repository, localFile, requestProperties,
transferFile( connector, targetRepository.getContent(), targetPath, repository, localFile, requestProperties,
true );
if ( fileExists(downloadedFile) )
@ -292,17 +293,17 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
catch ( NotFoundException e )
{
log.debug( "Artifact {} not found on repository \"{}\".", Keys.toKey( artifact ),
targetRepository.getRepository().getId() );
targetRepository.getId() );
}
catch ( NotModifiedException e )
{
log.debug( "Artifact {} not updated on repository \"{}\".", Keys.toKey( artifact ),
targetRepository.getRepository().getId() );
targetRepository.getId() );
}
catch ( ProxyException e )
{
validatePolicies( this.downloadErrorPolicies, connector.getPolicies(), requestProperties, artifact,
targetRepository, localFile, e, previousExceptions );
targetRepository.getContent(), localFile, e, previousExceptions );
}
}
@ -340,7 +341,7 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
continue;
}
RemoteRepositoryContent targetRepository = connector.getTargetRepository();
RemoteRepository targetRepository = connector.getTargetRepository();
requestProperties.setProperty( "remoteRepositoryId", targetRepository.getId() );
String targetPath = path;
@ -348,7 +349,7 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
try
{
StorageAsset downloadedFile =
transferFile( connector, targetRepository, targetPath, repository, localFile, requestProperties,
transferFile( connector, targetRepository.getContent(), targetPath, repository, localFile, requestProperties,
false );
if ( fileExists( downloadedFile ) )
@ -360,22 +361,22 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
catch ( NotFoundException e )
{
log.debug( "Resource {} not found on repository \"{}\".", path,
targetRepository.getRepository().getId() );
targetRepository.getId() );
}
catch ( NotModifiedException e )
{
log.debug( "Resource {} not updated on repository \"{}\".", path,
targetRepository.getRepository().getId() );
targetRepository.getId() );
}
catch ( ProxyException e )
{
log.warn(
"Transfer error from repository {} for resource {}, continuing to next repository. Error message: {}",
targetRepository.getRepository().getId(), path, e.getMessage() );
targetRepository.getId(), path, e.getMessage() );
log.debug( MarkerFactory.getDetachedMarker( "transfer.error" ),
"Transfer error from repository \"{}"
+ "\" for resource {}, continuing to next repository. Error message: {}",
targetRepository.getRepository().getId(), path, e.getMessage(), e );
targetRepository.getId(), path, e.getMessage(), e );
}
}
@ -403,14 +404,14 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
continue;
}
RemoteRepositoryContent targetRepository = connector.getTargetRepository();
RemoteRepository targetRepository = connector.getTargetRepository();
StorageAsset localRepoFile = toLocalRepoFile( repository, targetRepository, logicalPath );
StorageAsset localRepoFile = toLocalRepoFile( repository, targetRepository.getContent(), logicalPath );
long originalMetadataTimestamp = getLastModified( localRepoFile );
try
{
transferFile( connector, targetRepository, logicalPath, repository, localRepoFile, requestProperties,
transferFile( connector, targetRepository.getContent(), logicalPath, repository, localRepoFile, requestProperties,
true );
if ( hasBeenUpdated( localRepoFile, originalMetadataTimestamp ) )
@ -422,21 +423,21 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
{
log.debug( "Metadata {} not found on remote repository '{}'.", logicalPath,
targetRepository.getRepository().getId(), e );
targetRepository.getId(), e );
}
catch ( NotModifiedException e )
{
log.debug( "Metadata {} not updated on remote repository '{}'.", logicalPath,
targetRepository.getRepository().getId(), e );
targetRepository.getId(), e );
}
catch ( ProxyException e )
{
log.warn(
"Transfer error from repository {} for versioned Metadata {}, continuing to next repository. Error message: {}",
targetRepository.getRepository().getId(), logicalPath, e.getMessage() );
targetRepository.getId(), logicalPath, e.getMessage() );
log.debug( "Full stack trace", e );
}
}
@ -659,7 +660,7 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
{
// Just-in-time update of the index and database by executing the consumers for this artifact
//consumers.executeConsumers( connector.getSourceRepository().getRepository(), resource );
queueRepositoryTask( connector.getSourceRepository().getRepository().getId(), resource );
queueRepositoryTask( connector.getSourceRepository().getId(), resource );
}
return resource;
@ -667,7 +668,7 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
protected abstract void transferResources( ProxyConnector connector, RemoteRepositoryContent remoteRepository,
StorageAsset tmpResource, StorageAsset[] checksumFiles, String url, String remotePath, StorageAsset resource, Path workingDirectory,
ManagedRepositoryContent repository ) throws ProxyException, NotModifiedException;
ManagedRepositoryContent repository ) throws ProxyException;
private void queueRepositoryTask(String repositoryId, StorageAsset localFile )
{
@ -952,16 +953,6 @@ public abstract class DefaultRepositoryProxyHandler implements RepositoryProxyHa
this.archivaConfiguration = archivaConfiguration;
}
public RepositoryContentFactory getRepositoryFactory()
{
return repositoryFactory;
}
public void setRepositoryFactory(RepositoryContentFactory repositoryFactory )
{
this.repositoryFactory = repositoryFactory;
}
public MetadataTools getMetadataTools()
{
return metadataTools;

View File

@ -19,21 +19,22 @@ package org.apache.archiva.repository.connector;
* under the License.
*/
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.ManagedRepository;
import org.apache.archiva.repository.RemoteRepository;
import java.util.List;
/**
* RepositoryConnector
*
* A RepositoryConnector maps a managed repository to a remote repository.
*
*
*/
public interface RepositoryConnector
{
ManagedRepositoryContent getSourceRepository();
ManagedRepository getSourceRepository();
RemoteRepositoryContent getTargetRepository();
RemoteRepository getTargetRepository();
List<String> getBlacklist();

View File

@ -47,19 +47,11 @@
</property>
</bean>
<bean name="repositoryContentFactory#mocked" class="org.apache.archiva.repository.RepositoryContentFactory">
<property name="archivaConfiguration" ref="archivaConfiguration#mock"/>
</bean>
<bean name="repositoryContentProvider#mocked" class="org.apache.archiva.repository.mock.RepositoryContentProviderMock" >
</bean>
<bean name="repositoryContentProvider#mocked" class="org.apache.archiva.repository.mock.RepositoryContentProviderMock" />
<bean name="repositoryProxyConnectors#test" class="org.apache.archiva.proxy.maven.MavenRepositoryProxyHandler">
<property name="archivaConfiguration" ref="archivaConfiguration#mock"/>
<property name="repositoryFactory" ref="repositoryContentFactory#mocked"/>
<property name="metadataTools" ref="metadataTools#mocked"/>
</bean>