-
Notifications
You must be signed in to change notification settings - Fork 63
Detect JVM installs at startup #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2023 Red Hat, Inc. and others. | ||
| * | ||
| * This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * which accompanies this distribution, and is available at | ||
| * https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| *******************************************************************************/ | ||
| package org.eclipse.jdt.internal.launching; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.core.runtime.IStatus; | ||
| import org.eclipse.core.runtime.Platform; | ||
| import org.eclipse.core.runtime.Status; | ||
| import org.eclipse.core.runtime.SubMonitor; | ||
| import org.eclipse.core.runtime.jobs.Job; | ||
| import org.eclipse.jdt.launching.IVMInstall; | ||
| import org.eclipse.jdt.launching.IVMInstall2; | ||
| import org.eclipse.jdt.launching.IVMInstallType; | ||
| import org.eclipse.jdt.launching.JavaRuntime; | ||
| import org.eclipse.jdt.launching.VMStandin; | ||
|
|
||
|
mickaelistria marked this conversation as resolved.
|
||
| /** | ||
| * Lookup for VMs installed in standard or usual locations; and add the existing ones that | ||
| * are not yet known by JDT to the VM registry (usually visible in the "Installed JREs" | ||
| * preference page) | ||
| */ | ||
| class DetectVMInstallationsJob extends Job { | ||
|
|
||
| private static final Object FAMILY = DetectVMInstallationsJob.class; | ||
|
|
||
| private final StandardVMType standardType; | ||
|
|
||
| DetectVMInstallationsJob() { | ||
| super(LaunchingMessages.lookupInstalledJVMs); | ||
| this.standardType = (StandardVMType)JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE); | ||
| } | ||
|
|
||
| @Override | ||
| protected IStatus run(IProgressMonitor monitor) { | ||
|
mickaelistria marked this conversation as resolved.
|
||
| Collection<File> candidates = computeCandidateVMs(); | ||
| if (monitor.isCanceled()) { | ||
| return Status.CANCEL_STATUS; | ||
| } | ||
| Set<File> knownVMs = knownVMs(); | ||
| candidates.removeIf(knownVMs::contains); | ||
| monitor.beginTask(LaunchingMessages.lookupInstalledJVMs, candidates.size()); | ||
| for (File f : candidates) { | ||
| if (monitor.isCanceled()) { | ||
| return Status.CANCEL_STATUS; | ||
| } | ||
| SubMonitor subMon = SubMonitor.convert(monitor, f.getAbsolutePath(), 1); | ||
| VMStandin workingCopy = new VMStandin(standardType, f.getAbsolutePath()); | ||
| workingCopy.setInstallLocation(f); | ||
| String name = f.getName(); | ||
| int i = 1; | ||
| while (isDuplicateName(name)) { | ||
| name = f.getName() + '(' + i++ + ')'; | ||
| } | ||
| workingCopy.setName(name); | ||
| IVMInstall install = workingCopy.convertToRealVM(); | ||
| if (!(install instanceof IVMInstall2 vm && vm.getJavaVersion() != null)) { | ||
| // worksaround: such VMs may cause issue later | ||
| // https://github.com/eclipse-jdt/eclipse.jdt.debug/issues/248 | ||
| standardType.disposeVMInstall(install.getId()); | ||
| } | ||
| subMon.done(); | ||
| } | ||
| return Status.OK_STATUS; | ||
| } | ||
|
|
||
| private boolean isDuplicateName(String name) { | ||
| return Stream.of(JavaRuntime.getVMInstallTypes()) // | ||
| .flatMap(vmType -> Arrays.stream(vmType.getVMInstalls())) // | ||
| .map(IVMInstall::getName) // | ||
| .anyMatch(name::equals); | ||
| } | ||
|
|
||
| private Collection<File> computeCandidateVMs() { | ||
| // parent directories containing a collection of VM installations | ||
| Collection<File> rootDirectories = new HashSet<>(); | ||
| if (!Platform.OS_WIN32.equals(Platform.getOS())) { | ||
|
jjohnstn marked this conversation as resolved.
|
||
| rootDirectories.add(new File("/usr/lib/jvm")); //$NON-NLS-1$ | ||
| } | ||
| if (Platform.OS_MACOSX.equals(Platform.getOS())) { | ||
| rootDirectories.add(new File("/Library/Java/JavaVirtualMachines")); //$NON-NLS-1$ | ||
| } | ||
| rootDirectories.add(new File(System.getProperty("user.home"), ".sdkman/candidates/java")); //$NON-NLS-1$ //$NON-NLS-2$ | ||
|
mickaelistria marked this conversation as resolved.
|
||
|
|
||
| Set<File> directories = rootDirectories.stream().filter(File::isDirectory) | ||
| .map(dir -> dir.listFiles(File::isDirectory)) | ||
| .flatMap(Arrays::stream) | ||
| .filter(Objects::nonNull) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| // particular VM installations | ||
| String javaHome = System.getenv("JAVA_HOME"); //$NON-NLS-1$ | ||
| if (javaHome != null) { | ||
| directories.add(new File(javaHome)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in #758 |
||
| } | ||
| String jdkHome = System.getenv("JDK_HOME"); //$NON-NLS-1$ | ||
| if (jdkHome != null) { | ||
| directories.add(new File(jdkHome)); | ||
| } | ||
| // other common/standard lookup strategies can be added here | ||
|
|
||
| return directories.stream() | ||
| .filter(Objects::nonNull) | ||
| .filter(File::isDirectory) | ||
| .map(t -> { | ||
| try { | ||
| return t.getCanonicalFile(); | ||
| } catch (IOException e) { | ||
| return null; | ||
| } | ||
| }).filter(Objects::nonNull) | ||
| .filter(location -> standardType.validateInstallLocation(location).isOK()) | ||
| .collect(Collectors.toCollection(HashSet::new)); | ||
| } | ||
|
|
||
| private static Set<File> knownVMs() { | ||
| return Stream.of(JavaRuntime.getVMInstallTypes()) | ||
| .map(IVMInstallType::getVMInstalls) | ||
| .flatMap(Arrays::stream) | ||
| .map(IVMInstall::getInstallLocation) | ||
| .filter(Objects::nonNull) | ||
| .map(t -> { | ||
| try { | ||
| return t.getCanonicalFile(); | ||
| } catch (IOException e) { | ||
| return null; | ||
| } | ||
| }).filter(Objects::nonNull) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean belongsTo(Object family) { | ||
| return family.equals(FAMILY); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.