Skip to content

Commit

Permalink
New JavaHomeFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Nov 3, 2023
1 parent 57c26dc commit 806faa8
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 16 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ public class CustomLoader {
}
```

To find a JDK 21 on your local system with a specific distribution:

```java
final JavaHome jdk21 = new JavaHomeFinder()
.jdk()
.version(21)
.preferredDistributions(JavaDistribution.ZULU)
.find();
```

## Demo

To run a demo of a "cat" executable
Expand Down
42 changes: 26 additions & 16 deletions src/main/java/com/fizzed/jne/JavaDistribution.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,46 @@
*/
public enum JavaDistribution {

ZULU("Azul Systems", "https://www.azul.com/downloads", new String[] { "zulu", "azul" }),
LIBERICA("BellSoft", "https://bell-sw.com/libericajdk", new String[] { "liberica", "bellsoft" }),
TEMURIN("Eclipse", "https://adoptium.net", new String[] { "temurin", "eclipse", "adoptium" }),
CORRETTO("Amazon", "https://docs.aws.amazon.com/corretto", new String[] { "corretto", "amazon" }),
MICROSOFT("Microsoft", "https://learn.microsoft.com/en-us/java/openjdk", new String[] { "microsoft" }),
SEMERU("IBM", "https://developer.ibm.com/languages/java/semeru-runtimes", new String[] { "ibm", "semeru" }),
ORACLE("Oracle", "https://www.oracle.com/java/technologies/downloads", new String[] { "oracle" }),
DRAGONWELL("Alibaba", "https://dragonwell-jdk.io", new String[] { "alibaba", "dragonwell" }),
NITRO("Fizzed", "https://github.com/fizzed/nitro", new String[] { "fizzed", "nitro" }),
JETBRAINS("JetBrains", "https://github.com/JetBrains/JetBrainsRuntime", new String[] { "jetbrains" }),
SAPMACHINE("SAP", "https://sap.github.io/SapMachine", new String[] { "sap", "sapmachine" }),
ZULU("Zulu", "Azul Systems", "https://www.azul.com/downloads", new String[] { "zulu", "azul" }),
LIBERICA("Liberica", "BellSoft", "https://bell-sw.com/libericajdk", new String[] { "liberica", "bellsoft" }),
TEMURIN("Temurin", "Eclipse", "https://adoptium.net", new String[] { "temurin", "eclipse", "adoptium" }),
CORRETTO("Corretto", "Amazon", "https://docs.aws.amazon.com/corretto", new String[] { "corretto", "amazon" }),
MICROSOFT("Microsoft", "Microsoft", "https://learn.microsoft.com/en-us/java/openjdk", new String[] { "microsoft" }),
SEMERU("Semeru", "IBM", "https://developer.ibm.com/languages/java/semeru-runtimes", new String[] { "ibm", "semeru" }),
ORACLE("Oracle", "Oracle", "https://www.oracle.com/java/technologies/downloads", new String[] { "oracle" }),
DRAGONWELL("DragonWell", "Alibaba", "https://dragonwell-jdk.io", new String[] { "alibaba", "dragonwell" }),
NITRO("Nitro", "Fizzed", "https://github.com/fizzed/nitro", new String[] { "fizzed", "nitro" }),
JBR("JBR", "JetBrains", "https://github.com/JetBrains/JetBrainsRuntime", new String[] { "jetbrains" }),
SAPMACHINE("SapMachine", "SAP", "https://sap.github.io/SapMachine", new String[] { "sap", "sapmachine" }),

// Tencent KONA
// Trava

//
// these are provided by many package managers / os distros
//
REDHAT("RedHat", null, new String[] { "redhat" }),
DEBIAN("Debian", null, new String[] { "debian" }),
UBUNTU("Ubuntu", null, new String[] { "ubuntu" }),
HOMEBREW("HomeBrew", null, new String[] { "homebrew" }),
REDHAT("OpenJDK", "RedHat", null, new String[] { "redhat" }),
DEBIAN("OpenJDK", "Debian", null, new String[] { "debian" }),
UBUNTU("OpenJDK", "Ubuntu", null, new String[] { "ubuntu" }),
HOMEBREW("OpenJDK", "HomeBrew", null, new String[] { "homebrew" }),
;

private final String descriptor;
private final String vendor;
private final String distroUrl;
private final String[] keywords;

JavaDistribution(String vendor, String distroUrl, String[] keywords) {
JavaDistribution(String descriptor, String vendor, String distroUrl, String[] keywords) {
this.descriptor = descriptor;
this.vendor = vendor;
this.distroUrl = distroUrl;
this.keywords = keywords;
}

public String getDescriptor() {
return descriptor;
}

public String getVendor() {
return vendor;
}
Expand All @@ -67,6 +73,10 @@ public String getDistroUrl() {
return distroUrl;
}

public String[] getKeywords() {
return keywords;
}

static public JavaDistribution resolve(String value) {
if (value == null) {
return null;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/fizzed/jne/JavaHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ public Map<String, String> getReleaseProperties() {
return releaseProperties;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
if (this.distribution != null) {
sb.append(this.distribution.getDescriptor());
}
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(this.getImageType());
sb.append(" ").append(this.version);
sb.append(" (");
if (this.operatingSystem != null) {
sb.append(this.operatingSystem.getDescriptor());
if (this.hardwareArchitecture != null) {
sb.append(", ");
sb.append(this.hardwareArchitecture.getDescriptor());
}
sb.append(", ");
}
sb.append(this.directory);
sb.append(")");
return sb.toString();
}

static public JavaHome current() throws IOException {
Path javaHomeDir = Paths.get(System.getProperty("java.home"));
return current(javaHomeDir);
Expand Down
180 changes: 180 additions & 0 deletions src/main/java/com/fizzed/jne/JavaHomeFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.fizzed.jne;

/*-
* #%L
* jne
* %%
* Copyright (C) 2016 - 2023 Fizzed, Inc
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import java.util.*;
import java.util.stream.Collectors;

public class JavaHomeFinder {

static public final JavaDistribution[] DEFAULT_PREFERRED_DISTRIBUTIONS = new JavaDistribution[] {
JavaDistribution.ZULU,
JavaDistribution.LIBERICA,
JavaDistribution.NITRO,
JavaDistribution.TEMURIN,
JavaDistribution.MICROSOFT,
JavaDistribution.CORRETTO
};

private JavaImageType imageType;
private Integer minVersion;
private Integer maxVersion;
private JavaDistribution distribution;
private JavaDistribution[] preferredDistributions;

public JavaImageType getImageType() {
return imageType;
}

public JavaHomeFinder jdk() {
this.imageType = JavaImageType.JDK;
return this;
}

public JavaHomeFinder jre() {
this.imageType = JavaImageType.JRE;
return this;
}

public JavaHomeFinder nik() {
this.imageType = JavaImageType.NIK;
return this;
}

public JavaHomeFinder imageType(JavaImageType imageType) {
this.imageType = imageType;
return this;
}

public Integer getMinVersion() {
return minVersion;
}

public JavaHomeFinder minVersion(Integer minVersion) {
this.minVersion = minVersion;
return this;
}

public Integer getMaxVersion() {
return maxVersion;
}

public JavaHomeFinder maxVersion(Integer maxVersion) {
this.maxVersion = maxVersion;
return this;
}

public JavaHomeFinder version(Integer version) {
this.maxVersion = version;
this.minVersion = version;
return this;
}

public JavaDistribution getDistribution() {
return distribution;
}

public JavaHomeFinder distribution(JavaDistribution distribution) {
this.distribution = distribution;
return this;
}

public JavaDistribution[] getPreferredDistributions() {
return preferredDistributions;
}

public JavaHomeFinder preferredDistributions() {
this.preferredDistributions = DEFAULT_PREFERRED_DISTRIBUTIONS;
return this;
}

public JavaHomeFinder preferredDistributions(JavaDistribution... preferredDistributions) {
this.preferredDistributions = preferredDistributions;
return this;
}

@Override
public String toString() {
return "imageType=" + imageType +
", minVersion=" + minVersion +
", maxVersion=" + maxVersion +
", distribution=" + distribution +
", preferredDistributions=" + Arrays.toString(preferredDistributions) +
'}';
}

public JavaHome find() throws ResourceNotFoundException {
final List<JavaHome> javaHomes;
try {
javaHomes = JavaHomes.detect();
} catch (Exception e) {
throw new ResourceNotFoundException("Unable to find Java", e);
}
return this.find(javaHomes);
}

public JavaHome find(List<JavaHome> javaHomes) throws ResourceNotFoundException {
return this.tryFind(javaHomes)
.orElseThrow(() -> new ResourceNotFoundException("Unable to find Java with criteria " + this.toString()));
}

public Optional<JavaHome> tryFind() {
final List<JavaHome> javaHomes;
try {
javaHomes = JavaHomes.detect();
} catch (Exception e) {
return Optional.empty();
}
return this.tryFind(javaHomes);
}

public Optional<JavaHome> tryFind(List<JavaHome> javaHomes) {
if (javaHomes == null || javaHomes.isEmpty()) {
return Optional.empty();
}

// filter our list down by image type, version, etc. (concrete criteria)
final List<JavaHome> filteredJavaHomes = javaHomes.stream()
.filter(v -> this.minVersion == null || v.getVersion().getMajor() >= this.minVersion)
.filter(v -> this.maxVersion == null || v.getVersion().getMajor() <= this.maxVersion)
.filter(v -> this.imageType == null || v.getImageType() == this.imageType)
.filter(v -> this.distribution == null || v.getDistribution() == this.distribution)
.collect(Collectors.toList());

// now, we'll sort what's left by the most recent version (descending)
filteredJavaHomes.sort((a, b) -> b.getVersion().compareTo(a.getVersion()));

// by preferred now?
if (this.preferredDistributions != null) {
for (JavaDistribution d : this.preferredDistributions) {
for (JavaHome javaHome : filteredJavaHomes) {
if (javaHome.getDistribution() == d) {
return Optional.of(javaHome);
}
}
}
}

// otherwise, return the first
return filteredJavaHomes.stream().findFirst();
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/fizzed/jne/ResourceNotFoundException.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public class ResourceNotFoundException extends IOException {
public ResourceNotFoundException(String msg) {
super(msg);
}

public ResourceNotFoundException(String s, Throwable throwable) {
super(s, throwable);
}

}

0 comments on commit 806faa8

Please sign in to comment.