Skip to content

Commit

Permalink
Merge pull request #19419 from aloubyansky/platform-catalog-last-updated
Browse files Browse the repository at this point in the history
Add last-updated metadata attribute to the PlatformCatalog as reported by Maven metadata
  • Loading branch information
aloubyansky authored Aug 16, 2021
2 parents ca53ffc + fa026cb commit d67d5ec
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface Constants {
String PLATFORM_PROPERTIES_ARTIFACT_ID_SUFFIX = "-quarkus-platform-properties";

String JSON = "json";

String LAST_UPDATED = "last-updated";
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,30 @@ public PlatformCatalog resolvePlatformCatalog(String quarkusVersion) throws Regi
final JsonPlatformCatalog result = new JsonPlatformCatalog();

final List<Platform> collectedPlatforms = new ArrayList<>();
result.setPlatforms(collectedPlatforms);

final Set<String> collectedPlatformKeys = new HashSet<>();
String lastUpdated = null;
boolean sawUnknownLastUpdate = false;
for (PlatformCatalog c : catalogs) {
collectPlatforms(c, collectedPlatforms, collectedPlatformKeys);
if (!sawUnknownLastUpdate) {
final Object catalogLastUpdated = c.getMetadata().get(Constants.LAST_UPDATED);
if (catalogLastUpdated == null) {
// if for one of the catalogs it's unknown, it's going to be unknown for the merged catalog
lastUpdated = null;
sawUnknownLastUpdate = true;
} else if (lastUpdated == null) {
lastUpdated = catalogLastUpdated.toString();
} else if (lastUpdated.compareTo(catalogLastUpdated.toString()) < 0) {
lastUpdated = catalogLastUpdated.toString();
}
}
}

if (lastUpdated != null) {
result.getMetadata().put(Constants.LAST_UPDATED, lastUpdated);
}
result.setPlatforms(collectedPlatforms);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

abstract class JsonEntityWithAnySupport {

private Map<String, Object> metadata;
private Map<String, Object> metadata = new HashMap<>(0);

@JsonAnyGetter
public Map<String, Object> getMetadata() {
return metadata == null ? Collections.emptyMap() : metadata;
return metadata;
}

public void setMetadata(Map<String, Object> metadata) {
Expand All @@ -21,9 +20,6 @@ public void setMetadata(Map<String, Object> metadata) {

@JsonAnySetter
public void setAny(String name, Object value) {
if (metadata == null) {
metadata = new HashMap<>();
}
metadata.put(name, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.maven.ArtifactCoords;
import io.quarkus.registry.Constants;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.PlatformCatalog;
import io.quarkus.registry.catalog.json.JsonCatalogMapperHelper;
Expand All @@ -11,8 +12,10 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.ArtifactResult;

public class MavenPlatformsResolver implements RegistryPlatformsResolver {

Expand All @@ -33,18 +36,41 @@ public PlatformCatalog resolvePlatforms(String quarkusVersion) throws RegistryRe
final Artifact catalogArtifact = new DefaultArtifact(baseCoords.getGroupId(), baseCoords.getArtifactId(),
quarkusVersion, baseCoords.getType(), baseCoords.getVersion());
log.debug("Resolving platform catalog %s", catalogArtifact);
final Path jsonFile;
final ArtifactResult artifactResult;
try {
jsonFile = artifactResolver.resolve(catalogArtifact);
artifactResult = artifactResolver.resolveArtifact(catalogArtifact);
} catch (Exception e) {
log.debug("Failed to resolve platform catalog %s", catalogArtifact);
return null;
}
final Path jsonFile = artifactResult.getArtifact().getFile().toPath();
final JsonPlatformCatalog catalog;
try {
return JsonCatalogMapperHelper.deserialize(jsonFile, JsonPlatformCatalog.class);
catalog = JsonCatalogMapperHelper.deserialize(jsonFile, JsonPlatformCatalog.class);
} catch (IOException e) {
throw new RegistryResolutionException(
"Failed to load platform catalog from " + jsonFile, e);
}

try {
final Metadata mavenMetadata = artifactResolver.resolveMetadata(artifactResult);
if (mavenMetadata != null) {
final String lastUpdated = mavenMetadata.getVersioning() == null ? null
: mavenMetadata.getVersioning().getLastUpdated();
if (lastUpdated != null) {
/*
* This is how it can be parsed
* java.util.TimeZone timezone = java.util.TimeZone.getTimeZone("UTC");
* java.text.DateFormat fmt = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
* fmt.setTimeZone(timezone);
* final Date date = fmt.parse(lastUpdated);
*/
catalog.getMetadata().put(Constants.LAST_UPDATED, lastUpdated);
}
}
} catch (Exception e) {
log.debug("Failed to resolve Maven metadata for %s", catalogArtifact);
}
return catalog;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import java.nio.file.Path;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.resolution.ArtifactResult;

public interface MavenRegistryArtifactResolver {

Path resolve(Artifact artifact) throws BootstrapMavenException;
default Path resolve(Artifact artifact) throws BootstrapMavenException {
return resolveArtifact(artifact).getArtifact().getFile().toPath();
}

ArtifactResult resolveArtifact(Artifact artifact) throws BootstrapMavenException;

Path findArtifactDirectory(Artifact artifact) throws BootstrapMavenException;

String getLatestVersionFromRange(Artifact artifact, String versionRange) throws BootstrapMavenException;

Metadata resolveMetadata(ArtifactResult result) throws BootstrapMavenException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import java.io.BufferedReader;
import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.metadata.DefaultMetadata;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.metadata.Metadata.Nature;
import org.eclipse.aether.repository.LocalRepositoryManager;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.MetadataRequest;
import org.eclipse.aether.resolution.MetadataResult;

public class MavenRegistryArtifactResolverWithCleanup implements MavenRegistryArtifactResolver {

Expand All @@ -23,9 +31,8 @@ public MavenRegistryArtifactResolverWithCleanup(MavenArtifactResolver resolver,
}

@Override
public Path resolve(Artifact artifact) throws BootstrapMavenException {
return resolveAndCleanupOldTimestampedVersions(resolver, artifact, cleanupTimestampedVersions).getArtifact().getFile()
.toPath();
public ArtifactResult resolveArtifact(Artifact artifact) throws BootstrapMavenException {
return resolveAndCleanupOldTimestampedVersions(resolver, artifact, cleanupTimestampedVersions);
}

@Override
Expand Down Expand Up @@ -78,6 +85,40 @@ protected static ArtifactResult resolveAndCleanupOldTimestampedVersions(MavenArt
}
}
}

return result;
}

@Override
public org.apache.maven.artifact.repository.metadata.Metadata resolveMetadata(ArtifactResult result)
throws BootstrapMavenException {
final Artifact artifact = result.getArtifact();
Metadata md = new DefaultMetadata(artifact.getGroupId(), artifact.getArtifactId(),
artifact.isSnapshot() ? artifact.getBaseVersion() : artifact.getVersion(),
"maven-metadata.xml", artifact.isSnapshot() ? Nature.SNAPSHOT : Nature.RELEASE);

final MetadataRequest mdr = new MetadataRequest().setMetadata(md);
final String repoId = result.getRepository().getId();
if (repoId != null && !repoId.equals("local")) {
for (RemoteRepository r : resolver.getRepositories()) {
if (r.getId().equals(repoId)) {
mdr.setRepository(r);
break;
}
}
}

final List<MetadataResult> mdResults = resolver.getSystem().resolveMetadata(resolver.getSession(), Arrays.asList(mdr));
if (!mdResults.isEmpty()) {
md = mdResults.get(0).getMetadata();
if (md != null && md.getFile() != null && md.getFile().exists()) {
try (BufferedReader reader = new BufferedReader(new java.io.FileReader(md.getFile()))) {
return new MetadataXpp3Reader().read(reader);
} catch (Exception e) {
// ignore for now
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import org.eclipse.aether.artifact.DefaultArtifact;

Expand All @@ -24,8 +25,15 @@ public class MavenRegistryCache implements RegistryCache {
public MavenRegistryCache(RegistryConfig config, MavenRegistryArtifactResolver resolver,
MessageWriter log) {
this.config = config;
this.artifacts = Arrays.asList(config.getDescriptor().getArtifact(),
config.getNonPlatformExtensions().getArtifact(), config.getPlatforms().getArtifact());
final List<ArtifactCoords> artifacts = new ArrayList<>(3);
artifacts.add(config.getDescriptor().getArtifact());
if (config.getNonPlatformExtensions() != null) {
artifacts.add(config.getNonPlatformExtensions().getArtifact());
}
if (config.getPlatforms() != null) {
artifacts.add(config.getPlatforms().getArtifact());
}
this.artifacts = artifacts;
this.resolver = Objects.requireNonNull(resolver);
this.log = Objects.requireNonNull(log);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public RegistryClient buildRegistryClient(RegistryConfig config) throws Registry
}

return new RegistryClientDispatcher(config, platformsResolver,
Boolean.TRUE.equals(config.getPlatforms().getExtensionCatalogsIncluded())
Boolean.TRUE.equals(platformsConfig == null ? Boolean.FALSE : platformsConfig.getExtensionCatalogsIncluded())
? new MavenPlatformExtensionsResolver(defaultResolver, log)
: new MavenPlatformExtensionsResolver(defaultResolver(originalResolver, cleanupTimestampedArtifacts),
log),
Expand Down
Loading

0 comments on commit d67d5ec

Please sign in to comment.