Skip to content

Commit

Permalink
Revert toolchain changes for Java 22 upgrade. (elastic#107164)
Browse files Browse the repository at this point in the history
This is a follow up to elastic#107137 to also revert the Java toolchain changes
necessary to support the required toolchains.
  • Loading branch information
mark-vieira authored Apr 5, 2024
1 parent 5a9a9b8 commit 12d819a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.compress.utils.Lists;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaToolchainDownload;
import org.gradle.jvm.toolchain.JavaToolchainRequest;
Expand All @@ -20,25 +21,25 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.StreamSupport;

import static org.gradle.jvm.toolchain.JavaToolchainDownload.fromUri;

public abstract class AdoptiumJdkToolchainResolver extends AbstractCustomJavaToolchainResolver {

// package protected for better testing
final Map<AdoptiumVersionRequest, Optional<String>> CACHED_RELEASES = new ConcurrentHashMap<>();
final Map<AdoptiumVersionRequest, Optional<AdoptiumVersionInfo>> CACHED_SEMVERS = new ConcurrentHashMap<>();

@Override
public Optional<JavaToolchainDownload> resolve(JavaToolchainRequest request) {
if (requestIsSupported(request) == false) {
return Optional.empty();
}
AdoptiumVersionRequest versionRequestKey = toVersionRequest(request);
Optional<String> versionInfo = CACHED_RELEASES.computeIfAbsent(
Optional<AdoptiumVersionInfo> versionInfo = CACHED_SEMVERS.computeIfAbsent(
versionRequestKey,
(r) -> resolveAvailableVersion(versionRequestKey)
);
Expand All @@ -53,12 +54,12 @@ private AdoptiumVersionRequest toVersionRequest(JavaToolchainRequest request) {
return new AdoptiumVersionRequest(platform, arch, javaLanguageVersion);
}

private Optional<String> resolveAvailableVersion(AdoptiumVersionRequest requestKey) {
private Optional<AdoptiumVersionInfo> resolveAvailableVersion(AdoptiumVersionRequest requestKey) {
ObjectMapper mapper = new ObjectMapper();
try {
int languageVersion = requestKey.languageVersion.asInt();
URL source = new URL(
"https://api.adoptium.net/v3/info/release_names?architecture="
"https://api.adoptium.net/v3/info/release_versions?architecture="
+ requestKey.arch
+ "&image_type=jdk&os="
+ requestKey.platform
Expand All @@ -70,8 +71,14 @@ private Optional<String> resolveAvailableVersion(AdoptiumVersionRequest requestK
+ ")"
);
JsonNode jsonNode = mapper.readTree(source);
JsonNode versionsNode = jsonNode.get("releases");
return StreamSupport.stream(versionsNode.spliterator(), false).map(JsonNode::textValue).findFirst();
JsonNode versionsNode = jsonNode.get("versions");
return Optional.of(
Lists.newArrayList(versionsNode.iterator())
.stream()
.map(this::toVersionInfo)
.max(Comparator.comparing(AdoptiumVersionInfo::semver))
.get()
);
} catch (FileNotFoundException e) {
// request combo not supported (e.g. aarch64 + windows
return Optional.empty();
Expand All @@ -80,10 +87,21 @@ private Optional<String> resolveAvailableVersion(AdoptiumVersionRequest requestK
}
}

private URI resolveDownloadURI(AdoptiumVersionRequest request, String version) {
private AdoptiumVersionInfo toVersionInfo(JsonNode node) {
return new AdoptiumVersionInfo(
node.get("build").asInt(),
node.get("major").asInt(),
node.get("minor").asInt(),
node.get("openjdk_version").asText(),
node.get("security").asInt(),
node.get("semver").asText()
);
}

private URI resolveDownloadURI(AdoptiumVersionRequest request, AdoptiumVersionInfo versionInfo) {
return URI.create(
"https://api.adoptium.net/v3/binary/version/"
+ version
"https://api.adoptium.net/v3/binary/version/jdk-"
+ versionInfo.semver
+ "/"
+ request.platform
+ "/"
Expand All @@ -100,5 +118,7 @@ private boolean requestIsSupported(JavaToolchainRequest request) {
return anyVendorOr(request.getJavaToolchainSpec().getVendor().get(), JvmVendorSpec.ADOPTIUM);
}

record AdoptiumVersionInfo(int build, int major, int minor, String openjdkVersion, int security, String semver) {}

record AdoptiumVersionRequest(String platform, String arch, JavaLanguageVersion languageVersion) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ record JdkBuild(JavaLanguageVersion languageVersion, String version, String buil
);

// package private so it can be replaced by tests
List<JdkBuild> builds = List.of(getBundledJdkBuild());
List<JdkBuild> builds = List.of(
getBundledJdkBuild(),
// 22 release candidate
new JdkBuild(JavaLanguageVersion.of(22), "22", "36", "830ec9fcccef480bb3e73fb7ecafe059")
);

private JdkBuild getBundledJdkBuild() {
String bundledJdkVersion = VersionProperties.getBundledJdkVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package org.elasticsearch.gradle.internal.toolchain
import org.gradle.api.services.BuildServiceParameters
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.jvm.toolchain.JavaToolchainResolver
import org.gradle.platform.OperatingSystem

import static org.elasticsearch.gradle.internal.toolchain.AbstractCustomJavaToolchainResolver.toArchString
import static org.elasticsearch.gradle.internal.toolchain.AbstractCustomJavaToolchainResolver.toOsString
Expand All @@ -37,7 +38,12 @@ class AdoptiumJdkToolchainResolverSpec extends AbstractToolchainResolverSpec {
toOsString(it[2], it[1]),
toArchString(it[3]),
languageVersion);
resolver.CACHED_RELEASES.put(request, Optional.of('jdk-' + languageVersion.asInt() + '.1.1.1+37.1'))
resolver.CACHED_SEMVERS.put(request, Optional.of(new AdoptiumJdkToolchainResolver.AdoptiumVersionInfo(languageVersion.asInt(),
1,
1,
"" + languageVersion.asInt() + ".1.1.1+37",
0, "" + languageVersion.asInt() + ".1.1.1+37.1"
)))

}
return resolver
Expand Down

0 comments on commit 12d819a

Please sign in to comment.