Skip to content

Commit

Permalink
Extension registry transfer listener
Browse files Browse the repository at this point in the history
(cherry picked from commit 555e11b)
  • Loading branch information
aloubyansky authored and gsmet committed Jun 22, 2021
1 parent 23c6d64 commit 52fe228
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -326,12 +333,13 @@ private static boolean isComplete(RegistryMavenRepoConfig config) {
return true;
}

private static MavenArtifactResolver newResolver(MavenArtifactResolver resolver, List<RemoteRepository> aggregatedRepos) {
private static MavenArtifactResolver newResolver(MavenArtifactResolver resolver, List<RemoteRepository> 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())
Expand All @@ -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<RemoteRepository> configuredRepos) {
final RegistryMavenConfig mavenConfig = config.getMaven() == null ? null : config.getMaven();
Expand Down

0 comments on commit 52fe228

Please sign in to comment.