From 52fe228e04eddc7b94153d03bf3040e7ea4d1954 Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Tue, 22 Jun 2021 14:32:00 +0200 Subject: [PATCH] Extension registry transfer listener (cherry picked from commit 555e11b1de7e696792f1f521e9a64d362538dd07) --- ...enRegistryArtifactResolverWithCleanup.java | 6 +- .../maven/MavenRegistryClientFactory.java | 71 ++++++++++++++++++- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryArtifactResolverWithCleanup.java b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryArtifactResolverWithCleanup.java index 4ca003847f1ff..5dffc6cbe6e0d 100644 --- a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryArtifactResolverWithCleanup.java +++ b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryArtifactResolverWithCleanup.java @@ -48,7 +48,7 @@ public String getLatestVersionFromRange(Artifact artifact, String versionRange) */ protected static ArtifactResult resolveAndCleanupOldTimestampedVersions(MavenArtifactResolver resolver, Artifact artifact, boolean cleanupOldTimestampedVersions) throws BootstrapMavenException { - if (!cleanupOldTimestampedVersions) { + if (!artifact.isSnapshot() || !cleanupOldTimestampedVersions) { return resolver.resolve(artifact); } @@ -63,7 +63,9 @@ protected static ArtifactResult resolveAndCleanupOldTimestampedVersions(MavenArt if (jsonDirContent != null && jsonDirContent.length > existingFiles.size()) { final String fileName = result.getArtifact().getFile().getName(); for (File c : jsonDirContent) { - if (c.getName().length() > fileName.length() && c.getName().startsWith(artifact.getArtifactId()) + if (c.getName().length() > fileName.length() + && c.getName().startsWith(artifact.getArtifactId()) + && c.getName().endsWith(artifact.getClassifier()) && existingFiles.contains(c.getName())) { c.deleteOnExit(); } diff --git a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryClientFactory.java b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryClientFactory.java index 4284d21e4ec3b..2c9e8129ac54c 100644 --- a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryClientFactory.java +++ b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryClientFactory.java @@ -33,11 +33,16 @@ import java.util.List; import java.util.Objects; import java.util.Set; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.repository.RepositoryPolicy; import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.aether.transfer.TransferCancelledException; +import org.eclipse.aether.transfer.TransferEvent; +import org.eclipse.aether.transfer.TransferListener; public class MavenRegistryClientFactory implements RegistryClientFactory { @@ -70,7 +75,9 @@ public RegistryClient buildRegistryClient(RegistryConfig config) throws Registry Collections.emptyList(), singleRegistryRepos, true); aggregatedRepos = resolver.getRemoteRepositoryManager().aggregateRepositories(resolver.getSession(), aggregatedRepos, resolver.getRepositories(), false); - resolver = newResolver(resolver, aggregatedRepos); + resolver = newResolver(resolver, aggregatedRepos, config, log); + } else { + resolver = newResolver(resolver, resolver.getRepositories(), config, log); } final boolean cleanupTimestampedArtifacts = isCleanupTimestampedArtifacts(config); @@ -326,12 +333,13 @@ private static boolean isComplete(RegistryMavenRepoConfig config) { return true; } - private static MavenArtifactResolver newResolver(MavenArtifactResolver resolver, List aggregatedRepos) { + private static MavenArtifactResolver newResolver(MavenArtifactResolver resolver, List aggregatedRepos, + RegistryConfig config, MessageWriter log) { try { final BootstrapMavenContext mvnCtx = new BootstrapMavenContext( BootstrapMavenContext.config() .setRepositorySystem(resolver.getSystem()) - .setRepositorySystemSession(resolver.getSession()) + .setRepositorySystemSession(setRegistryTransferListener(config, log, resolver.getSession())) .setRemoteRepositoryManager(resolver.getRemoteRepositoryManager()) .setRemoteRepositories(aggregatedRepos) .setLocalRepository(resolver.getMavenContext().getLocalRepo()) @@ -342,6 +350,63 @@ private static MavenArtifactResolver newResolver(MavenArtifactResolver resolver, } } + private static DefaultRepositorySystemSession setRegistryTransferListener(RegistryConfig config, MessageWriter log, + RepositorySystemSession session) { + final DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(session); + final TransferListener tl = newSession.getTransferListener(); + newSession.setTransferListener(new TransferListener() { + + boolean refreshingLocalCache; + + @Override + public void transferInitiated(TransferEvent event) throws TransferCancelledException { + if (!refreshingLocalCache) { + refreshingLocalCache = true; + log.info("Refreshing the local extension catalog cache of " + config.getId()); + } + if (tl != null) { + tl.transferInitiated(event); + } + } + + @Override + public void transferStarted(TransferEvent event) throws TransferCancelledException { + if (tl != null) { + tl.transferStarted(event); + } + } + + @Override + public void transferProgressed(TransferEvent event) throws TransferCancelledException { + if (tl != null) { + tl.transferProgressed(event); + } + } + + @Override + public void transferCorrupted(TransferEvent event) throws TransferCancelledException { + if (tl != null) { + tl.transferCorrupted(event); + } + } + + @Override + public void transferSucceeded(TransferEvent event) { + if (tl != null) { + tl.transferSucceeded(event); + } + } + + @Override + public void transferFailed(TransferEvent event) { + if (tl != null) { + tl.transferFailed(event); + } + } + }); + return newSession; + } + private void determineExtraRepos(RegistryConfig config, List configuredRepos) { final RegistryMavenConfig mavenConfig = config.getMaven() == null ? null : config.getMaven();