From d9d87966655ca4a82e9be780caf3f70b2bcdc853 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Thu, 12 Sep 2019 13:10:22 +0200 Subject: [PATCH] Fixing proxy connector handling --- .../archiva/proxy/ArchivaProxyRegistry.java | 34 +++++++++++++------ .../managed/ManagedRepositoryAdminTest.java | 1 - .../archiva/proxy/HttpProxyTransferTest.java | 2 ++ .../ArchivaRepositoryConnectorFactory.java | 2 ++ .../maven2/MavenRepositoryProvider.java | 8 +++-- .../DependencyTreeBuilderTestMaven3.java | 2 ++ 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java index 17b1f4e64..b59091a4c 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/ArchivaProxyRegistry.java @@ -27,6 +27,9 @@ 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.*; +import org.apache.archiva.repository.events.Event; +import org.apache.archiva.repository.events.RepositoryEventListener; +import org.apache.archiva.repository.events.RepositoryRegistryEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -44,7 +47,7 @@ import java.util.stream.Collectors; */ @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" ) @Service("proxyRegistry#default") -public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListener { +public class ArchivaProxyRegistry implements ProxyRegistry, RepositoryEventListener { private static final Logger log = LoggerFactory.getLogger(ArchivaProxyRegistry.class); @@ -74,10 +77,10 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene if (repositoryProxyHandlers == null) { repositoryProxyHandlers = new ArrayList<>(); } - archivaConfiguration.addListener( this ); updateHandler(); updateConnectors(); updateNetworkProxies(); + repositoryRegistry.register(this, RepositoryRegistryEvent.RegistryEventType.RELOADED); } private ArchivaConfiguration getArchivaConfiguration() { @@ -125,7 +128,9 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene getArchivaConfiguration().getConfiguration().getProxyConnectors(); connectorList = proxyConnectorConfigurations.stream() - .map(configuration -> buildProxyConnector(configuration)) + .map(this::buildProxyConnector) + .filter(Optional::isPresent) + .map(Optional::get) .sorted(comparator).collect(Collectors.toList()); connectorMap = connectorList.stream().collect(Collectors.groupingBy(a -> a.getSourceRepository().getId())); for (RepositoryProxyHandler handler : repositoryProxyHandlers) { @@ -139,7 +144,7 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene return policies.stream().collect( Collectors.toMap( Function.identity(), p -> PolicyUtil.findOption( policyConfig.get(p.getId()), p ) ) ); } - private ProxyConnector buildProxyConnector(ProxyConnectorConfiguration configuration) { + private Optional buildProxyConnector(ProxyConnectorConfiguration configuration) { ProxyConnector proxyConnector = new ProxyConnector(); proxyConnector.setOrder(configuration.getOrder()); proxyConnector.setBlacklist(configuration.getBlackListPatterns()); @@ -153,10 +158,16 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene proxyConnector.setProperties(configuration.getProperties()); proxyConnector.setProxyId(configuration.getProxyId()); ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId()); + if (srcRepo==null) { + return Optional.empty(); + } proxyConnector.setSourceRepository(srcRepo); RemoteRepository targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId()); + if (targetRepo==null) { + return Optional.empty(); + } proxyConnector.setTargetRepository(targetRepo); - return proxyConnector; + return Optional.of(proxyConnector); } @Override @@ -183,11 +194,6 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene return this.handlerMap.containsKey(type); } - @Override - public void configurationEvent(ConfigurationEvent event) { - log.debug("Config changed updating proxy list"); - init( ); - } @Override public List getProxyConnectors() { @@ -205,4 +211,12 @@ public class ArchivaProxyRegistry implements ProxyRegistry, ConfigurationListene { init(); } + + @Override + public void raise(Event event) { + log.debug("Reload happened, updating proxy list"); + if (event.getType()== RepositoryRegistryEvent.RegistryEventType.RELOADED) { + init(); + } + } } diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java index 05b40c35e..2e542d5cc 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java @@ -316,7 +316,6 @@ public class ManagedRepositoryAdminTest managedRepositoryAdmin.updateManagedRepository( repo, true, getFakeAuditInformation(), false ); repo = managedRepositoryAdmin.getManagedRepository( repoId ); - System.err.println("REPOSITORY "+repo.getLocation()); assertNotNull( repo ); assertEquals( newName, repo.getName() ); assertEquals( Paths.get( repoLocation ).toAbsolutePath(), Paths.get( repo.getLocation() ).toAbsolutePath() ); diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java index b35967f3e..dd7448766 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/proxy/HttpProxyTransferTest.java @@ -219,6 +219,8 @@ public class HttpProxyTransferTest // Configure Connector (usually done within archiva.xml configuration) addConnector(); + managedDefaultRepository = repositoryRegistry.getManagedRepository(MANAGED_ID).getContent(); + Path expectedFile = Paths.get( managedDefaultRepository.getRepoRoot() ).resolve( path ); Files.deleteIfExists( expectedFile ); ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path ); diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/ArchivaRepositoryConnectorFactory.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/ArchivaRepositoryConnectorFactory.java index dd401aef4..f099292f9 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/ArchivaRepositoryConnectorFactory.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/ArchivaRepositoryConnectorFactory.java @@ -19,6 +19,7 @@ package org.apache.archiva.dependency.tree.maven2; */ import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory; +import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider; import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,6 +50,7 @@ public class ArchivaRepositoryConnectorFactory public ArchivaRepositoryConnectorFactory() { // no op but empty constructor needed by aether + delegate.setRepositoryLayoutProvider(new DefaultRepositoryLayoutProvider()); } @Override diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java index cdc03db86..15dbf6ad6 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven2/MavenRepositoryProvider.java @@ -22,6 +22,8 @@ package org.apache.archiva.repository.maven2; import org.apache.archiva.common.filelock.FileLockManager; import org.apache.archiva.configuration.*; import org.apache.archiva.repository.*; +import org.apache.archiva.repository.events.Event; +import org.apache.archiva.repository.events.RepositoryValueEvent; import org.apache.archiva.repository.storage.FilesystemStorage; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.archiva.repository.features.IndexCreationFeature; @@ -347,7 +349,9 @@ public class MavenRepositoryProvider implements RepositoryProvider { cfg.setPackedIndexDir(convertUriToPath(indexCreationFeature.getPackedIndexPath())); RemoteIndexFeature remoteIndexFeature = remoteRepository.getFeature(RemoteIndexFeature.class).get(); - cfg.setRemoteIndexUrl(remoteIndexFeature.getIndexUri().toString()); + if (remoteIndexFeature.getIndexUri()!=null) { + cfg.setRemoteIndexUrl(remoteIndexFeature.getIndexUri().toString()); + } cfg.setRemoteDownloadTimeout((int) remoteIndexFeature.getDownloadTimeout().get(ChronoUnit.SECONDS)); cfg.setDownloadRemoteIndexOnStartup(remoteIndexFeature.isDownloadRemoteIndexOnStartup()); cfg.setDownloadRemoteIndex(remoteIndexFeature.isDownloadRemoteIndex()); @@ -495,7 +499,7 @@ public class MavenRepositoryProvider implements RepositoryProvider { } @Override - public void raise(RepositoryEvent event) { + public void raise(Event event) { // } diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java index fb0b802d4..df2f10484 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java @@ -81,6 +81,8 @@ public class DependencyTreeBuilderTestMaven3 repoConfig.setId( TEST_REPO_ID ); repoConfig.setLocation(Paths.get("target/test-repository").toAbsolutePath().toString() ); configuration.addManagedRepository( repoConfig ); + + config.getConfiguration().getProxyConnectors().clear(); config.save( configuration ); repositoryRegistry.reload();