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. * under the License.
*/ */
import org.apache.archiva.repository.ManagedRepository;
import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RemoteRepository;
import org.apache.archiva.repository.RemoteRepositoryContent; import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.connector.RepositoryConnector; import org.apache.archiva.repository.connector.RepositoryConnector;
@ -33,9 +35,9 @@ import java.util.Map;
public class ProxyConnector public class ProxyConnector
implements RepositoryConnector implements RepositoryConnector
{ {
private ManagedRepositoryContent sourceRepository; private ManagedRepository sourceRepository;
private RemoteRepositoryContent targetRepository; private RemoteRepository targetRepository;
private List<String> blacklist; private List<String> blacklist;
@ -80,23 +82,23 @@ public class ProxyConnector
} }
@Override @Override
public ManagedRepositoryContent getSourceRepository() public ManagedRepository getSourceRepository()
{ {
return sourceRepository; return sourceRepository;
} }
public void setSourceRepository( ManagedRepositoryContent sourceRepository ) public void setSourceRepository( ManagedRepository sourceRepository )
{ {
this.sourceRepository = sourceRepository; this.sourceRepository = sourceRepository;
} }
@Override @Override
public RemoteRepositoryContent getTargetRepository() public RemoteRepository getTargetRepository()
{ {
return targetRepository; return targetRepository;
} }
public void setTargetRepository( RemoteRepositoryContent targetRepository ) public void setTargetRepository( RemoteRepository targetRepository )
{ {
this.targetRepository = targetRepository; this.targetRepository = targetRepository;
} }
@ -138,8 +140,8 @@ public class ProxyConnector
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append( "ProxyConnector[\n" ); sb.append( "ProxyConnector[\n" );
sb.append( " source: [managed] " ).append( this.sourceRepository.getRepoRoot() ).append( "\n" ); sb.append( " source: [managed] " ).append( this.sourceRepository.getId() ).append( "\n" );
sb.append( " target: [remote] " ).append( this.targetRepository.getRepository().getLocation() ).append( "\n" ); sb.append( " target: [remote] " ).append( this.targetRepository.getId() ).append( "\n" );
sb.append( " proxyId:" ).append( this.proxyId ).append( "\n" ); sb.append( " proxyId:" ).append( this.proxyId ).append( "\n" );
Iterator<String> keys = this.policies.keySet().iterator(); 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.NetworkProxy;
import org.apache.archiva.proxy.model.ProxyConnector; import org.apache.archiva.proxy.model.ProxyConnector;
import org.apache.archiva.proxy.model.RepositoryProxyHandler; import org.apache.archiva.proxy.model.RepositoryProxyHandler;
import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.*;
import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.RepositoryType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -128,9 +125,9 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene
proxyConnector.setPolicies(configuration.getPolicies()); proxyConnector.setPolicies(configuration.getPolicies());
proxyConnector.setProperties(configuration.getProperties()); proxyConnector.setProperties(configuration.getProperties());
proxyConnector.setProxyId(configuration.getProxyId()); proxyConnector.setProxyId(configuration.getProxyId());
ManagedRepositoryContent srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId()).getContent(); ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId());
proxyConnector.setSourceRepository(srcRepo); proxyConnector.setSourceRepository(srcRepo);
RemoteRepositoryContent targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId()).getContent(); RemoteRepository targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId());
proxyConnector.setTargetRepository(targetRepo); proxyConnector.setTargetRepository(targetRepo);
return proxyConnector; return proxyConnector;
} }

View File

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

View File

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

View File

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