Skip to content

Commit 7ec0aa7

Browse files
committed
Lots of API changes, move from nulls to throwing exceptions
1 parent e9e5372 commit 7ec0aa7

16 files changed

+674
-297
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ tasks.register('javaProbeJar', Jar) {
4949
}
5050

5151
dependencies {
52+
compileOnly libs.nulls
53+
5254
implementation libs.jopt
5355
implementation libs.gson
5456
implementation libs.jtar

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ dependencyResolutionManagement {
1414
plugin 'gradleutils', 'net.minecraftforge.gradleutils' version '2.6.0'
1515
plugin 'shadow', 'com.gradleup.shadow' version '9.0.0-beta13'
1616

17+
library 'nulls', 'org.jetbrains:annotations:26.0.2-1'
18+
1719
library 'gson', 'com.google.code.gson:gson:2.10.1' // > 2.9.0 needs Java 7
1820
library 'jopt', 'net.sf.jopt-simple:jopt-simple:6.0-alpha-3' // Java 8
1921
library 'jtar', 'org.kamranzafar:jtar:2.3'

src/main/java/net/minecraftforge/java_provisioner/Disco.java

Lines changed: 185 additions & 120 deletions
Large diffs are not rendered by default.

src/main/java/net/minecraftforge/java_provisioner/DiscoLocator.java

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
import java.io.File;
88
import java.util.ArrayList;
9-
import java.util.Collections;
109
import java.util.List;
10+
import java.util.Objects;
1111

12-
import net.minecraftforge.java_provisioner.Disco.Arch;
1312
import net.minecraftforge.java_provisioner.Disco.Distro;
1413
import net.minecraftforge.java_provisioner.api.IJavaInstall;
15-
import net.minecraftforge.util.os.OS;
14+
import net.minecraftforge.java_provisioner.api.JavaLocatingFailedException;
15+
import net.minecraftforge.java_provisioner.api.JavaProvisioningFailedException;
1616

1717
/**
1818
* Locates java installs that have been downloaded from the <a href="https://github.com/foojayio/discoapi">disco API</a>
@@ -44,45 +44,50 @@ public DiscoLocator(File cache, boolean offline) {
4444
}
4545

4646
@Override
47-
public File find(int version) {
48-
List<IJavaInstall> results = findInternal(version);
49-
return results.isEmpty() ? null : results.get(0).home();
47+
public File find(int version) throws JavaLocatingFailedException {
48+
return findInternal(version).installs().get(0).home();
5049
}
5150

5251
@Override
53-
public List<IJavaInstall> findAll() {
52+
public JavaLocatorResult findAll() throws JavaLocatingFailedException {
5453
return findInternal(-1);
5554
}
5655

57-
private List<IJavaInstall> findInternal(int version) {
56+
private JavaLocatorResult findInternal(int version) throws JavaLocatingFailedException {
5857
if (!cache.exists() || !cache.isDirectory())
59-
return Collections.emptyList();
58+
throw new JavaLocatingFailedException("Java Provisioner has not provisioned any Java installations");
6059

6160
List<IJavaInstall> results = new ArrayList<>();
62-
for (File dir : cache.listFiles()) {
61+
List<Throwable> errors = new ArrayList<>();
62+
63+
File[] listFiles;
64+
try {
65+
listFiles = Objects.requireNonNull(cache.listFiles());
66+
} catch (Exception e) {
67+
throw new JavaLocatingFailedException("An unexpected error occured trying to query the Disco cache: " + cache, e);
68+
}
69+
70+
for (File dir : listFiles) {
6371
if (!dir.isDirectory())
6472
continue;
6573

6674
log("Disco Cache: \"" + dir.getAbsolutePath() + "\"");
6775

68-
IJavaInstall ret = fromPath(dir);
69-
if (ret != null) {
70-
if (version == -1) {
71-
results.add(ret);
72-
} else if (ret.majorVersion() != version) {
73-
log(" Wrong version: Was " + ret.majorVersion() + " wanted " + version);
74-
} else {
75-
results.add(ret);
76-
return results;
77-
}
76+
try {
77+
results.add(fromPath(dir, version));
78+
} catch (Exception e) {
79+
errors.add(e);
7880
}
7981
}
8082

81-
return results;
83+
if (!results.isEmpty())
84+
return new JavaLocatorResult(results, errors);
85+
86+
throw Throwing.provisioningFailed("Failed to find any Java installations from Disco cache", errors);
8287
}
8388

8489
@Override
85-
public IJavaInstall provision(int version) {
90+
public IJavaInstall provision(int version, Distro distro) throws JavaProvisioningFailedException {
8691
log("Locators failed to find any suitable installs, attempting Disco download");
8792
Disco disco = new Disco(cache, offline) { // TODO: [DISCO][Logging] Add a proper logging handler sometime
8893
@Override
@@ -96,34 +101,31 @@ protected void error(String message) {
96101
}
97102
};
98103

99-
List<Disco.Package> jdks = disco.getPackages(version, Disco.CURRENT_OS, Distro.TEMURIN, Disco.CURRENT_ARCH);
100-
if (jdks == null || jdks.isEmpty()) {
101-
log("Failed to find any distros from Disco for " + version + " " + Disco.CURRENT_OS + " " + Disco.CURRENT_ARCH + " " + Distro.TEMURIN);
102-
103-
// Try any vendor
104-
jdks = disco.getPackages(version, Disco.CURRENT_OS, null, Disco.CURRENT_ARCH);
105-
if (jdks == null || jdks.isEmpty()) {
106-
log("Failed to find any distros from Disco for " + version + " " + Disco.CURRENT_OS + " " + Disco.CURRENT_ARCH);
107-
108-
// Try any Architecture and just hope for the best
109-
jdks = disco.getPackages(version, Disco.CURRENT_OS, null, null);
110-
if (jdks == null || jdks.isEmpty()) {
111-
log("Failed to find any distros from Disco for " + version + " " + Disco.CURRENT_OS);
112-
return null;
113-
}
104+
List<Disco.Package> jdks;
105+
try {
106+
jdks = disco.getPackages(version, Disco.CURRENT_OS, distro, Disco.CURRENT_ARCH);
107+
} catch (Exception e) {
108+
log(String.format("Failed to find any JDKs from Disco for: %s%d - %s %s", distro != null ? distro + " " : "", version, Disco.CURRENT_OS, Disco.CURRENT_ARCH));
109+
110+
// Try any Architecture and just hope for the best
111+
try {
112+
jdks = disco.getPackages(version, Disco.CURRENT_OS, distro, null);
113+
} catch (Exception suppressed) {
114+
log(String.format("Failed to find any JDKs from Disco for: %s%d - %s ANY", distro != null ? distro + " " : "", version, Disco.CURRENT_OS));
115+
e.addSuppressed(suppressed);
116+
throw new JavaProvisioningFailedException("Failed to provision Disco download", e);
114117
}
115118
}
116119

117-
log("Found " + jdks.size() + " download canidates");
120+
log("Found " + jdks.size() + " download candidates");
118121
Disco.Package pkg = jdks.get(0);
119122
log("Selected " + pkg.distribution + ": " + pkg.filename);
120123

121-
File java_home = disco.extract(pkg);
122-
123-
if (java_home == null)
124-
return null;
125-
126-
IJavaInstall result = fromPath(java_home);
127-
return result;
124+
try {
125+
File java_home = disco.extract(pkg);
126+
return fromPath(java_home);
127+
} catch (Exception e) {
128+
throw new JavaProvisioningFailedException("Failed to provision Disco download: " + pkg.filename, e);
129+
}
128130
}
129131
}

src/main/java/net/minecraftforge/java_provisioner/DiscoMain.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,17 @@ private static boolean downloadJdk(
100100
Log.info(" Cache: " + cache.getAbsolutePath());
101101
Disco disco = new Disco(new File(cache, "jdks"));
102102

103-
List<Disco.Package> jdks = disco.getPackages(javaVersion, os, distro, arch);
104-
Disco.Package pkg = null;
105-
if (jdks == null || jdks.isEmpty()) {
103+
List<Disco.Package> jdks;
104+
try {
105+
jdks = disco.getPackages(javaVersion, os, distro, arch);
106+
if (jdks.isEmpty())
107+
throw new Exception("No download found");
108+
} catch (Exception e) {
106109
Log.error("Failed to find any download, try specifying a different java version or distro");
107110
return false;
108-
} else if (jdks.size() == 1 || auto) {
111+
}
112+
Disco.Package pkg = null;
113+
if (jdks.size() == 1 || auto) {
109114
pkg = jdks.get(0);
110115
} else if (jdks.size() > 1) {
111116
for (int x = 0; x < jdks.size(); x++) {
@@ -135,10 +140,16 @@ private static boolean downloadJdk(
135140

136141
Log.info();
137142

138-
File java_home = disco.extract(pkg);
143+
File java_home;
144+
try {
145+
java_home = disco.extract(pkg);
146+
} catch (Exception e) {
147+
throw new RuntimeException("Failed to extract downloaded package", e);
148+
}
139149

140-
if (java_home == null)
141-
System.exit(1);
150+
// No longer needed as we throw if no java home, but just in case:
151+
//if (java_home == null)
152+
// System.exit(1);
142153

143154
ProcessUtils.Result result = ProcessUtils.testJdk(java_home);
144155
if (result.exitCode != 0) {

0 commit comments

Comments
 (0)