-
-
Notifications
You must be signed in to change notification settings - Fork 3
Fixes for jdk providers #104
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f2bade
fix: this fixes default `accept()` being too restrictive
quintesse a66caca
fix: linux provider was using wrong path
quintesse e9a2972
fix: fixed scoop provider and added tests for it
quintesse 9a6d282
chore: made providers more similar
quintesse d746a6a
test: added tests for some jdk providers
quintesse 51ca7e2
feat: added windows jdk provider
quintesse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/main/java/dev/jbang/devkitman/jdkproviders/WindowsJdkProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package dev.jbang.devkitman.jdkproviders; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.InvalidPathException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.Objects; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.jspecify.annotations.NonNull; | ||
|
|
||
| import dev.jbang.devkitman.Jdk; | ||
| import dev.jbang.devkitman.JdkDiscovery; | ||
| import dev.jbang.devkitman.JdkProvider; | ||
| import dev.jbang.devkitman.util.JavaUtils; | ||
| import dev.jbang.devkitman.util.OsUtils; | ||
|
|
||
| /** | ||
| * This JDK provider detects JDKs registered under HKLM\SOFTWARE\JavaSoft. | ||
| */ | ||
| public class WindowsJdkProvider extends BaseJdkProvider { | ||
| public static final String JAVA_SOFT_KEY = "HKLM\\SOFTWARE\\JavaSoft"; | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(WindowsJdkProvider.class.getName()); | ||
| private static final Pattern KEY_LINE = Pattern.compile("^\\s*HKEY_.*$"); | ||
| private static final Pattern JAVA_HOME_LINE = Pattern.compile("^\\s*JavaHome\\s+REG_\\w+\\s+(.+?)\\s*$"); | ||
| private static final Pattern VALID_ID = Pattern.compile("^[a-zA-Z0-9._+-]+-windows$"); | ||
|
|
||
| private final RegistryReader registryReader; | ||
|
|
||
| public WindowsJdkProvider() { | ||
| this(new RegCommandRegistryReader()); | ||
| } | ||
|
|
||
| public WindowsJdkProvider(@NonNull RegistryReader registryReader) { | ||
| this.registryReader = registryReader; | ||
| } | ||
|
Comment on lines
+42
to
+44
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. Enforce non-null
Suggested fix public WindowsJdkProvider(`@NonNull` RegistryReader registryReader) {
- this.registryReader = registryReader;
+ this.registryReader = Objects.requireNonNull(registryReader, "registryReader");
}🤖 Prompt for AI Agents |
||
|
|
||
| @Override | ||
| @NonNull | ||
| public String name() { | ||
| return Discovery.PROVIDER_ID; | ||
| } | ||
|
|
||
| @Override | ||
| public @NonNull String description() { | ||
| return "The JDKs registered in the HKLM\\SOFTWARE\\JavaSoft registry key."; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canUse() { | ||
| return OsUtils.isWindows(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isValidId(@NonNull String id) { | ||
| return VALID_ID.matcher(id).matches(); | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public Stream<Jdk.InstalledJdk> listInstalled() { | ||
| return dedupeByJavaHome(registryReader.listJavaHomes(JAVA_SOFT_KEY)) | ||
| .entrySet() | ||
| .stream() | ||
| .map(this::createJdk) | ||
| .filter(Objects::nonNull); | ||
| } | ||
|
|
||
| private Map<String, Path> dedupeByJavaHome(Map<String, Path> javaHomes) { | ||
| Map<Path, Entry<String, Path>> selectedByHome = new LinkedHashMap<>(); | ||
| for (Entry<String, Path> entry : javaHomes.entrySet()) { | ||
| selectedByHome.merge(entry.getValue(), entry, this::preferLongestKey); | ||
| } | ||
| Map<String, Path> deduped = new LinkedHashMap<>(); | ||
| for (Entry<String, Path> entry : selectedByHome.values()) { | ||
| deduped.put(entry.getKey(), entry.getValue()); | ||
| } | ||
| return deduped; | ||
| } | ||
|
|
||
| private Entry<String, Path> preferLongestKey(Entry<String, Path> existing, Entry<String, Path> replacement) { | ||
| return replacement.getKey().length() > existing.getKey().length() ? replacement : existing; | ||
| } | ||
|
|
||
| private Jdk.InstalledJdk createJdk(Map.Entry<String, Path> javaHomeEntry) { | ||
| Path javaHome = javaHomeEntry.getValue(); | ||
| if (!Files.isDirectory(javaHome) || !JavaUtils.hasJavacCmd(javaHome)) { | ||
| return null; | ||
| } | ||
| String id = registryVersionSegment(javaHomeEntry.getKey()) + "-" + name(); | ||
| return createJdk(id, javaHome); | ||
| } | ||
|
|
||
| private String registryVersionSegment(String registryKey) { | ||
| int idx = registryKey.lastIndexOf('\\'); | ||
| String segment = idx >= 0 && idx + 1 < registryKey.length() ? registryKey.substring(idx + 1) : registryKey; | ||
| return segment.replaceAll("[^a-zA-Z0-9._+-]+", "_"); | ||
| } | ||
|
|
||
| public interface RegistryReader { | ||
| @NonNull | ||
| Map<String, Path> listJavaHomes(@NonNull String rootKey); | ||
| } | ||
|
|
||
| static class RegCommandRegistryReader implements RegistryReader { | ||
| @Override | ||
| public @NonNull Map<String, Path> listJavaHomes(@NonNull String rootKey) { | ||
| Map<String, Path> javaHomes = new LinkedHashMap<>(); | ||
| String output = OsUtils.runCommand("reg", "query", rootKey, "/s", "/v", "JavaHome"); | ||
| if (output == null || output.trim().isEmpty()) { | ||
| return javaHomes; | ||
| } | ||
| String currentKey = null; | ||
| for (String line : output.split("\\r?\\n")) { | ||
| Matcher keyMatcher = KEY_LINE.matcher(line); | ||
| if (keyMatcher.matches()) { | ||
| currentKey = line.trim(); | ||
| continue; | ||
| } | ||
| if (currentKey == null) { | ||
| continue; | ||
| } | ||
| Matcher javaHomeMatcher = JAVA_HOME_LINE.matcher(line); | ||
| if (javaHomeMatcher.matches()) { | ||
| String home = javaHomeMatcher.group(1).trim(); | ||
| try { | ||
| javaHomes.put(currentKey, Paths.get(home)); | ||
| } catch (InvalidPathException ex) { | ||
| LOGGER.log(Level.FINE, "Ignoring invalid registry JavaHome: " + home, ex); | ||
| } | ||
| } | ||
| } | ||
| return javaHomes; | ||
| } | ||
| } | ||
|
|
||
| public static class Discovery implements JdkDiscovery { | ||
| public static final String PROVIDER_ID = "windows"; | ||
|
|
||
| @Override | ||
| @NonNull | ||
| public String name() { | ||
| return PROVIDER_ID; | ||
| } | ||
|
|
||
| @Override | ||
| public JdkProvider create(@NonNull Config config) { | ||
| return new WindowsJdkProvider(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.