TheBrokenRail
2020b895b9
All checks were successful
FreshCoffee/pipeline/head This commit looks good
136 lines
4.4 KiB
Java
136 lines
4.4 KiB
Java
package com.thebrokenrail.freshcoffee.download;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonSyntaxException;
|
|
import com.google.gson.annotations.SerializedName;
|
|
import com.thebrokenrail.freshcoffee.config.HardcodedConfig;
|
|
import com.thebrokenrail.freshcoffee.util.limit.LimitUtil;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.net.URL;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public final class AdoptOpenJDK {
|
|
public static final class JDK {
|
|
public Binary binary;
|
|
public Version version;
|
|
}
|
|
|
|
@SuppressWarnings("unused")
|
|
public static final class Binary {
|
|
private String architecture;
|
|
private String heap_size;
|
|
private String image_type;
|
|
private String os;
|
|
@SerializedName("package")
|
|
public Package packageObj;
|
|
private String project;
|
|
|
|
private boolean isValid() {
|
|
return heap_size.equals(HardcodedConfig.HEAP_SIZE) && image_type.equals("jre") && project.equals("jdk") && (packageObj.link.endsWith(".zip") || packageObj.link.endsWith(".tar.gz"));
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("unused")
|
|
public static final class Package {
|
|
public String link;
|
|
public int size;
|
|
}
|
|
|
|
@SuppressWarnings("unused")
|
|
public static final class Version {
|
|
public String semver;
|
|
}
|
|
|
|
private static String urlToString(@SuppressWarnings("SameParameterValue") String urlStr) {
|
|
try {
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
URL url = new URL(urlStr);
|
|
|
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()))) {
|
|
String line;
|
|
while ((line = in.readLine()) != null) {
|
|
stringBuilder.append(line);
|
|
stringBuilder.append('\n');
|
|
}
|
|
}
|
|
|
|
return stringBuilder.toString();
|
|
} catch (IOException e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static class Platform {
|
|
private final String os;
|
|
private final String architecture;
|
|
|
|
private Platform(String os, String architecture) {
|
|
this.os = os;
|
|
this.architecture = architecture;
|
|
}
|
|
|
|
private boolean isValid(JDK jdk) {
|
|
return jdk.binary.os.equals(os) && jdk.binary.architecture.equals(architecture);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return os + '-' + architecture;
|
|
}
|
|
}
|
|
|
|
private static final String[] PLATFORM_TYPES = new String[]{"linux", "win", "macosx"};
|
|
private static final String[] ARCHITECTURE_TYPES = new String[]{"x64", "x32", "aarch64", "arm"};
|
|
|
|
private static final Platform[] PLATFORMS;
|
|
|
|
static {
|
|
PLATFORMS = new Platform[PLATFORM_TYPES.length * ARCHITECTURE_TYPES.length];
|
|
int pos = 0;
|
|
for (String platformType : PLATFORM_TYPES) {
|
|
for (String architectureType : ARCHITECTURE_TYPES) {
|
|
PLATFORMS[pos++] = new Platform(platformType, architectureType);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Map<String, JDK> get() {
|
|
String data = urlToString(String.format("https://api.adoptopenjdk.net/v3/assets/latest/%s/%s", LimitUtil.getMinimumJavaVersion().getValue(), HardcodedConfig.JAVA_IMPLEMENTATION));
|
|
if (data != null) {
|
|
JDK[] allJDKs = null;
|
|
|
|
try {
|
|
Gson gson = new Gson();
|
|
allJDKs = gson.fromJson(data, JDK[].class);
|
|
} catch (JsonSyntaxException ignored) {
|
|
}
|
|
|
|
if (allJDKs != null) {
|
|
Map<String, JDK> selectedJDKs = new HashMap<>();
|
|
for (JDK jdk : allJDKs) {
|
|
if (jdk.binary.isValid()) {
|
|
Platform platform = null;
|
|
for (Platform option : PLATFORMS) {
|
|
if (option.isValid(jdk)) {
|
|
platform = option;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (platform != null) {
|
|
selectedJDKs.put(platform.toString(), jdk);
|
|
}
|
|
}
|
|
}
|
|
return selectedJDKs;
|
|
}
|
|
}
|
|
return Collections.emptyMap();
|
|
}
|
|
}
|