Skip to content

Commit

Permalink
Persist manifest name and expose it via SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrkob committed Oct 17, 2023
1 parent 45531f2 commit 26a2b4d
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<version.org.jboss.xnio>3.8.10.Final</version.org.jboss.xnio>
<version.org.wildfly.common>1.6.0.Final</version.org.wildfly.common>
<version.org.wildfly.galleon-plugins>6.4.3.Final</version.org.wildfly.galleon-plugins>
<version.org.wildfly.installation-manager>1.0.1.Final</version.org.wildfly.installation-manager>
<version.org.wildfly.installation-manager>1.0.2.Final-SNAPSHOT</version.org.wildfly.installation-manager>
<version.org.wildfly.maven.plugins.licenses-plugin>2.3.1.Final</version.org.wildfly.maven.plugins.licenses-plugin>
<version.org.mockito>5.3.0</version.org.mockito>
<version.org.slf4j>2.0.7</version.org.slf4j>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.wildfly.installationmanager.ChannelChange;
import org.wildfly.installationmanager.HistoryResult;
import org.wildfly.installationmanager.InstallationChanges;
import org.wildfly.installationmanager.ManifestVersion;
import org.wildfly.installationmanager.MavenOptions;
import org.wildfly.installationmanager.OperationNotAvailableException;
import org.wildfly.installationmanager.Repository;
Expand All @@ -21,12 +22,15 @@
import org.wildfly.prospero.actions.UpdateAction;
import org.wildfly.prospero.api.MavenOptions.Builder;
import org.wildfly.prospero.galleon.GalleonCallbackAdapter;
import org.wildfly.prospero.metadata.ManifestVersionRecord;
import org.wildfly.prospero.metadata.ProsperoMetadataUtils;
import org.wildfly.prospero.spi.internal.CliProvider;
import org.wildfly.prospero.api.SavedState;
import org.wildfly.prospero.api.exceptions.MetadataException;
import org.wildfly.prospero.api.exceptions.OperationException;
import org.wildfly.prospero.updates.UpdateSet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
Expand All @@ -41,6 +45,7 @@
import java.util.ServiceLoader;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ProsperoInstallationManager implements InstallationManager {

Expand Down Expand Up @@ -215,6 +220,25 @@ public String generateApplyRevertCommand(Path scriptHome, Path candidatePath, Os
return escape(scriptHome.resolve(cliProvider.getScriptName(shell))) + " " + cliProvider.getApplyRevertCommand(installationDir, candidatePath);
}

@Override
public Collection<ManifestVersion> getInstalledVersions() {
try {
final Optional<ManifestVersionRecord> versionRecord = ManifestVersionRecord.read(installationDir.resolve(ProsperoMetadataUtils.METADATA_DIR).resolve(ProsperoMetadataUtils.CURRENT_VERSION_FILE));
if (versionRecord.isPresent()) {
return Stream.concat(
versionRecord.get().getMavenManifests().stream()
.map(m->new ManifestVersion(m.getGroupId()+":"+m.getArtifactId(), m.getDescription(), m.getVersion(), ManifestVersion.Type.MAVEN)),
versionRecord.get().getUrlManifests().stream()
.map(m->new ManifestVersion(m.getUrl(), m.getDescription(), m.getHash(), ManifestVersion.Type.URL))
)
.collect(Collectors.toList());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return Collections.emptyList();
}

private String escape(Path absolutePath) {
return "\"" + absolutePath.toString() + "\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -33,19 +34,32 @@
import java.util.List;
import java.util.Optional;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ManifestVersionRecord {

@JsonIgnoreProperties(ignoreUnknown = true)
public static class MavenManifest {

private final String description;
private String groupId;
private String artifactId;
private String version;

@Deprecated
public MavenManifest(String groupId,
String artifactId,
String version) {
this(groupId, artifactId, version, null);
}

public MavenManifest(@JsonProperty("groupId") String groupId,
@JsonProperty("artifactId") String artifactId,
@JsonProperty("version") String version) {
@JsonProperty("version") String version,
@JsonProperty(value = "description", required = false) String description) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.description = description;
}

public String getGroupId() {
Expand All @@ -59,20 +73,37 @@ public String getArtifactId() {
public String getVersion() {
return version;
}

@JsonInclude(JsonInclude.Include.NON_NULL)
public String getDescription() {
return description;
}

@JsonIgnore
public String getSummary() {
return String.format("[%s:%s::%s]", groupId, artifactId, version);
}

}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class UrlManifest {

private String url;
private String hash;
public UrlManifest(@JsonProperty("url") String url, @JsonProperty("hash") String hash) {
private String description;

@Deprecated
public UrlManifest(String url, String hash) {
this.url = url;
this.hash = hash;
this.description = null;
}

public UrlManifest(@JsonProperty("url") String url, @JsonProperty("hash") String hash, @JsonProperty("description") String description) {
this.url = url;
this.hash = hash;
this.description = description;
}

public String getUrl() {
Expand All @@ -83,13 +114,19 @@ public String getHash() {
return hash;
}

@JsonInclude(JsonInclude.Include.NON_NULL)
public String getDescription() {
return description;
}

@JsonIgnore
public String getSummary() {
return String.format("[%s::%s]", url, hash);
}

}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class NoManifest {

private List<String> repos;
Expand All @@ -114,21 +151,26 @@ public String getSummary() {
}

}

private String schemaVersion;
private List<MavenManifest> mavenManifests = new ArrayList<>();

private List<UrlManifest> urlManifests = new ArrayList<>();
private List<NoManifest> noManifests = new ArrayList<>();

@JsonCreator
public ManifestVersionRecord(@JsonProperty("maven") List<MavenManifest> mavenManifests,
public ManifestVersionRecord(@JsonProperty("schemaVersion") String schemaVersion,
@JsonProperty("maven") List<MavenManifest> mavenManifests,
@JsonProperty("url") List<UrlManifest> urlManifests,
@JsonProperty("open") List<NoManifest> noManifests) {
this.schemaVersion = schemaVersion;
this.mavenManifests = mavenManifests==null?Collections.emptyList():mavenManifests;
this.urlManifests = urlManifests==null?Collections.emptyList():urlManifests;
this.noManifests = noManifests==null?Collections.emptyList():noManifests;
}

public ManifestVersionRecord() {
this.schemaVersion = "1.0.0";
}

@JsonProperty("maven")
Expand All @@ -149,6 +191,10 @@ public List<NoManifest> getOpenManifests() {
return noManifests;
}

public String getSchemaVersion() {
return schemaVersion;
}

public void addManifest(MavenManifest manifest) {
mavenManifests.add(manifest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.repository.LocalRepository;
import org.jboss.galleon.util.HashUtils;
import org.jboss.logging.Logger;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.Repository;
import org.wildfly.channel.maven.VersionResolverFactory;
import org.wildfly.channel.spi.MavenVersionsResolver;
Expand All @@ -48,6 +51,7 @@
* for open manifests it's list of repositories + strategy.
*/
public class ManifestVersionResolver {
private static final Logger LOG = Logger.getLogger(ManifestVersionResolver.class.getName());

private final VersionResolverFactory resolverFactory;

Expand Down Expand Up @@ -89,19 +93,53 @@ public ManifestVersionRecord getCurrentVersions(List<Channel> channels) throws I
} else if (manifestCoordinate.getUrl() != null) {
final String content = read(manifestCoordinate.getUrl());
final String hashCode = HashUtils.hash(content);
manifestVersionRecord.addManifest(new ManifestVersionRecord.UrlManifest(manifestCoordinate.getUrl().toExternalForm(), hashCode));
final ChannelManifest manifest = ChannelManifestMapper.from(manifestCoordinate.getUrl());
manifestVersionRecord.addManifest(new ManifestVersionRecord.UrlManifest(manifestCoordinate.getUrl().toExternalForm(), hashCode, manifest.getName()));
} else if (manifestCoordinate.getVersion() != null) {
manifestVersionRecord.addManifest(new ManifestVersionRecord.MavenManifest(manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(), manifestCoordinate.getVersion()));
final String description = getManifestDescription(manifestCoordinate, resolverFactory.create(channel.getRepositories()));
manifestVersionRecord.addManifest(new ManifestVersionRecord.MavenManifest(
manifestCoordinate.getGroupId(),
manifestCoordinate.getArtifactId(),
manifestCoordinate.getVersion(),
description));
} else {
final MavenVersionsResolver mavenVersionsResolver = resolverFactory.create(channel.getRepositories());
final Optional<String> latestVersion = VersionMatcher.getLatestVersion(mavenVersionsResolver.getAllVersions(manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(),
manifestCoordinate.getExtension(), manifestCoordinate.getClassifier()));
manifestVersionRecord.addManifest(new ManifestVersionRecord.MavenManifest(manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(), latestVersion.get()));
if (latestVersion.isPresent()) {
final String description = getManifestDescription(new ChannelManifestCoordinate(
manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(), latestVersion.get()
), mavenVersionsResolver);
manifestVersionRecord.addManifest(new ManifestVersionRecord.MavenManifest(
manifestCoordinate.getGroupId(),
manifestCoordinate.getArtifactId(),
latestVersion.get(),
description));
} else {
LOG.warn("Unable to determine version of manifest used: " + manifestCoordinate);
manifestVersionRecord.addManifest(new ManifestVersionRecord.MavenManifest(
manifestCoordinate.getGroupId(),
manifestCoordinate.getArtifactId(),
"",
null));
}
}
}
return manifestVersionRecord;
}

private String getManifestDescription(ChannelManifestCoordinate manifestCoordinate, MavenVersionsResolver mavenVersionsResolver) {
final List<URL> urls = mavenVersionsResolver.resolveChannelMetadata(List.of(manifestCoordinate));
final String description;
if (!urls.isEmpty()) {
final ChannelManifest manifest = ChannelManifestMapper.from(urls.get(0));
description = manifest.getName();
} else {
description = null;
}
return description;
}

private String read(URL url) throws IOException {
try(InputStream inputStream = url.openStream();
OutputStream outputStream = new ByteArrayOutputStream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@
package org.wildfly.prospero.metadata;

import org.jboss.galleon.util.HashUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.Repository;
import org.wildfly.channel.maven.VersionResolverFactory;
import org.wildfly.channel.spi.MavenVersionsResolver;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
Expand All @@ -50,13 +55,23 @@ public class ManifestVersionResolverTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Before
public void setUp() {
when(factory.create(any())).thenReturn(mavenResolver);
}

@Test
public void getVersionOfLatestMavenManifest() throws Exception {
final ManifestVersionResolver resolver = new ManifestVersionResolver(factory);

when(factory.create(any())).thenReturn(mavenResolver);
final File manifestFile = temp.newFile();
final URL url = manifestFile.toURI().toURL();
Files.writeString(manifestFile.toPath(), ChannelManifestMapper.toYaml(new ChannelManifest("test", null, null, Collections.emptyList())));

when(mavenResolver.getAllVersions("org.test", "test", "yaml", "manifest"))
.thenReturn(Set.of("1.0.0", "1.0.1"));
when(mavenResolver.resolveChannelMetadata(any()))
.thenReturn(List.of(url));

final Channel channel = new Channel("test", "", null, Collections.emptyList(),
new ChannelManifestCoordinate("org.test", "test"), null, null);
Expand All @@ -65,6 +80,9 @@ public void getVersionOfLatestMavenManifest() throws Exception {
assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getVersion)
.containsExactly("1.0.1");
assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getDescription)
.containsExactly("test");
assertThat(currentVersions.getOpenManifests()).isEmpty();
assertThat(currentVersions.getUrlManifests()).isEmpty();
}
Expand All @@ -84,11 +102,37 @@ public void getHardcodedVersionOfManifestIfPresent() throws Exception {
assertThat(currentVersions.getUrlManifests()).isEmpty();
}

@Test
public void emptyVersionIfLatestVersionCannotBeFound() throws Exception {
final ManifestVersionResolver resolver = new ManifestVersionResolver(factory);

when(mavenResolver.getAllVersions("org.test", "test", "yaml", "manifest"))
.thenReturn(Collections.emptySet());

final Channel channel = new Channel("test", "", null, Collections.emptyList(),
new ChannelManifestCoordinate("org.test", "test"), null, null);
final ManifestVersionRecord currentVersions = resolver.getCurrentVersions(List.of(channel));

assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getVersion)
.containsExactly("");
assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getGroupId)
.containsExactly("org.test");
assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getArtifactId)
.containsExactly("test");
assertThat(currentVersions.getOpenManifests()).isEmpty();
assertThat(currentVersions.getUrlManifests()).isEmpty();
}

@Test
public void getUrlAndHashOfManifest() throws Exception {
final ManifestVersionResolver resolver = new ManifestVersionResolver(factory);

final URL url = temp.newFile().toURI().toURL();
final File manifestFile = temp.newFile();
final URL url = manifestFile.toURI().toURL();
Files.writeString(manifestFile.toPath(), ChannelManifestMapper.toYaml(new ChannelManifest("test", null, null, Collections.emptyList())));
final Channel channel = new Channel("test", "", null, Collections.emptyList(),
new ChannelManifestCoordinate(url), null, null);
final ManifestVersionRecord currentVersions = resolver.getCurrentVersions(List.of(channel));
Expand All @@ -101,6 +145,9 @@ public void getUrlAndHashOfManifest() throws Exception {
assertThat(currentVersions.getUrlManifests())
.map(ManifestVersionRecord.UrlManifest::getHash)
.containsExactly(HashUtils.hashFile(Path.of(url.toURI())));
assertThat(currentVersions.getUrlManifests())
.map(ManifestVersionRecord.UrlManifest::getDescription)
.containsExactly("test");
}

@Test
Expand Down

0 comments on commit 26a2b4d

Please sign in to comment.