Skip to content

Commit

Permalink
remove serviceloader from runtime finder
Browse files Browse the repository at this point in the history
  • Loading branch information
janakmulani committed Oct 12, 2020
1 parent 06712a1 commit c9d1e65
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import java.util.Arrays;
import java.util.List;

public class LinuxRuntimeFinder implements RuntimeFinder {
class LinuxRuntimeFinder {
private static final Logger LOG = LoggerFactory.getLogger(LinuxRuntimeFinder.class);

private static final String JVM_BASEFOLDER = "/usr/lib/jvm/";

@Override
public List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
static List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
LOG.debug("Searching for local runtimes");

final Path systemPath = Paths.get(JVM_BASEFOLDER);
Expand All @@ -28,8 +27,7 @@ public List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
return JdkFinder.findLocalJdks(systemPath, sdkmanPath);
}

@Override
public List<OperationSystem> getSupportedOperationSystems() {
static List<OperationSystem> getSupportedOperationSystems() {
return Arrays.asList(OperationSystem.LINUX32, OperationSystem.LINUX64);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import java.util.Collections;
import java.util.List;

public class MacRuntimeFinder implements RuntimeFinder {
class MacRuntimeFinder {
private static final Logger LOG = LoggerFactory.getLogger(MacRuntimeFinder.class);

private static final String MAC_JVM_BASEFOLDER = "/Library/Java/JavaVirtualMachines";
private static final String MAC_HOMEBREW_JVM_BASEFOLDER = "/usr/local/Cellar/openjdk/";


@Override
public List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
static List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
LOG.debug("Searching for local runtimes");

final Path systemPath = Paths.get(MAC_JVM_BASEFOLDER);
Expand All @@ -31,8 +29,7 @@ public List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
return JdkFinder.findLocalJdks(systemPath, sdkmanPath, homebrewPath);
}

@Override
public List<OperationSystem> getSupportedOperationSystems() {
static List<OperationSystem> getSupportedOperationSystems() {
return Collections.singletonList(OperationSystem.MAC64);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;

public interface RuntimeFinder {

List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() throws Exception;

List<OperationSystem> getSupportedOperationSystems();

static List<ResultWithInput<Path, LocalJavaRuntime>> find() {
final OperationSystem currentOs = OperationSystem.getLocalSystem();

final List<ResultWithInput<Path, LocalJavaRuntime>> foundRuntimes = new ArrayList<>();
ServiceLoader.load(RuntimeFinder.class).iterator().forEachRemaining(f -> {
if (f.getSupportedOperationSystems().contains(currentOs)) {
try {
foundRuntimes.addAll(f.findLocalRuntimes());
} catch (final Exception e) {
throw new RuntimeException("Error while searching for local JVMs", e);
}
}
});

if (WindowsRuntimeFinder.getSupportedOperationSystems().contains(currentOs)) {
foundRuntimes.addAll(WindowsRuntimeFinder.findLocalRuntimes());
} else if (LinuxRuntimeFinder.getSupportedOperationSystems().contains(currentOs)) {
foundRuntimes.addAll(LinuxRuntimeFinder.findLocalRuntimes());
} else if (MacRuntimeFinder.getSupportedOperationSystems().contains(currentOs)) {
foundRuntimes.addAll(MacRuntimeFinder.findLocalRuntimes());
}
return Collections.unmodifiableList(foundRuntimes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Arrays;
import java.util.List;

public class WindowsRuntimeFinder implements RuntimeFinder {
class WindowsRuntimeFinder {
private static final Logger LOG = LoggerFactory.getLogger(WindowsRuntimeFinder.class);

private static final String PROGRAMS_32 = System.getenv("ProgramFiles(X86)") + File.separatorChar;
Expand All @@ -40,8 +40,7 @@ public class WindowsRuntimeFinder implements RuntimeFinder {
private static final String CYGWIN_USER_HOME = JavaSystemProperties.getUserHome().replace("Users", CYGWIN_HOME);
private static final String SDK_MAN_FOLDER = CYGWIN_USER_HOME + File.separatorChar + ".sdkman";

@Override
public List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
static List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
LOG.debug("Searching for local runtimes");

return JdkFinder.findLocalJdks(
Expand All @@ -54,8 +53,7 @@ public List<ResultWithInput<Path, LocalJavaRuntime>> findLocalRuntimes() {
);
}

@Override
public List<OperationSystem> getSupportedOperationSystems() {
static List<OperationSystem> getSupportedOperationSystems() {
return Arrays.asList(OperationSystem.WIN32, OperationSystem.WIN64);
}
}

This file was deleted.

0 comments on commit c9d1e65

Please sign in to comment.