Skip to content

Commit

Permalink
Do not automatically detect JVMs on CI systems
Browse files Browse the repository at this point in the history
Automatic detection on CI systems can cause a lot of trouble (e.g.
memory consumption, time to read the jvms, old jvms lingering around)
and is generally not useful as a CI has usually a dedicated and fixed
setup to run with.

This adds a check for the common 'CI' environment variable that is
usually defined by all usual CI/CD pipelines to indicate a job is
running on a CI server to skip the detection regardless of preferences
configuration. If one absolutely want the feature even for CI, the
system-property 'DetectVMInstallationsJob.disabled' can be set to false
to restore previous behavior.
  • Loading branch information
laeubi committed Dec 16, 2024
1 parent ddf0a0a commit 5b0bd37
Showing 1 changed file with 17 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
*/
public class DetectVMInstallationsJob extends Job {

/**
* CI is a common variable defined in CI/CDI servers like Jenkins, Gitlab, Github, ... to indicate it is a CI environment
*/
private static final String ENV_CI = "CI"; //$NON-NLS-1$
private static final String PROPERTY_DETECT_VM_INSTALLATIONS_JOB_DISABLED = "DetectVMInstallationsJob.disabled"; //$NON-NLS-1$
private static final Object FAMILY = DetectVMInstallationsJob.class;

public DetectVMInstallationsJob() {
Expand Down Expand Up @@ -129,14 +134,7 @@ private boolean isDuplicateName(String name) {

private Collection<File> computeCandidateVMs(StandardVMType standardType) {
// parent directories containing a collection of VM installations
Collection<File> rootDirectories = new HashSet<>();
if (Platform.OS_WIN32.equals(Platform.getOS())) {
computeWindowsCandidates(rootDirectories);
} else {
rootDirectories.add(new File("/usr/lib/jvm")); //$NON-NLS-1$
}
rootDirectories.add(new File(System.getProperty("user.home"), ".sdkman/candidates/java")); //$NON-NLS-1$ //$NON-NLS-2$
rootDirectories.add(new File(miseDataDir(), "installs/java")); //$NON-NLS-1$
Collection<File> rootDirectories = List.of(new File("/opt/java")); //$NON-NLS-1$

Set<File> directories = rootDirectories.stream().filter(File::isDirectory)
.map(dir -> dir.listFiles(File::isDirectory))
Expand Down Expand Up @@ -168,38 +166,6 @@ private Collection<File> computeCandidateVMs(StandardVMType standardType) {
.collect(Collectors.toCollection(HashSet::new));
}

private void computeWindowsCandidates(Collection<File> rootDirectories) {
List<String> progFiles = List.of("ProgramFiles", "ProgramFiles(x86)"); //$NON-NLS-1$//$NON-NLS-2$
List<String> subDirs = List.of("Eclipse Adoptium", "RedHat"); //$NON-NLS-1$//$NON-NLS-2$
rootDirectories.addAll(
progFiles.stream()
.map(name -> System.getenv(name))
.filter(Objects::nonNull)
.distinct()
.flatMap(progFilesDir -> subDirs.stream().map(subDir -> new File(progFilesDir, subDir)))
.collect(Collectors.toList()));
}

private static File miseDataDir() {
String miseDataDir = System.getenv("MISE_DATA_DIR"); //$NON-NLS-1$
return miseDataDir != null ? new File(miseDataDir) : new File(xdgDataHome(), "mise"); //$NON-NLS-1$
}

private static File xdgDataHome() {
String xdgDataHome = System.getenv("XDG_DATA_HOME"); //$NON-NLS-1$
if (Platform.OS_WIN32.equals(Platform.getOS())) {
if (xdgDataHome == null) {
xdgDataHome = System.getenv("LOCALAPPDATA"); //$NON-NLS-1$
}
if (xdgDataHome == null) {
return new File(System.getProperty("user.home"), "AppData/Local"); //$NON-NLS-1$ //$NON-NLS-2$
}
} else if (xdgDataHome == null) {
return new File(System.getProperty("user.home"), ".local/share"); //$NON-NLS-1$ //$NON-NLS-2$
}
return new File(xdgDataHome);
}

private static Set<File> knownVMs() {
return Stream.of(JavaRuntime.getVMInstallTypes())
.map(IVMInstallType::getVMInstalls)
Expand All @@ -222,11 +188,20 @@ public boolean belongsTo(Object family) {
}

public static void initialize() {
boolean forcedDisableVMDetection = Boolean.getBoolean("DetectVMInstallationsJob.disabled"); //$NON-NLS-1$
boolean forcedDisableVMDetection = Boolean.getBoolean(PROPERTY_DETECT_VM_INSTALLATIONS_JOB_DISABLED);
if (forcedDisableVMDetection) {
// early exit no need to read preferences!
return;
}
if (System.getProperty(PROPERTY_DETECT_VM_INSTALLATIONS_JOB_DISABLED) == null && Boolean.parseBoolean(System.getenv(ENV_CI))) {
// exit because no explicit value for the property was given and we are running in a CI environment
return;
}
// finally look what is defined in the preferences
IEclipsePreferences instanceNode = InstanceScope.INSTANCE.getNode(LaunchingPlugin.getDefault().getBundle().getSymbolicName());
IEclipsePreferences defaultNode = DefaultScope.INSTANCE.getNode(LaunchingPlugin.getDefault().getBundle().getSymbolicName());
boolean defaultValue = defaultNode.getBoolean(LaunchingPlugin.PREF_DETECT_VMS_AT_STARTUP, true);
if (!forcedDisableVMDetection && instanceNode.getBoolean(LaunchingPlugin.PREF_DETECT_VMS_AT_STARTUP, defaultValue)) {
if (instanceNode.getBoolean(LaunchingPlugin.PREF_DETECT_VMS_AT_STARTUP, defaultValue)) {
new DetectVMInstallationsJob().schedule();
}
}
Expand Down

0 comments on commit 5b0bd37

Please sign in to comment.