This is an automated email from the ASF dual-hosted git repository.

mbien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new e181301f8c improved JDK detection startup tasks
     new ac3b904000 Merge pull request #5637 from 
mbien/jdk-detector-improvements
e181301f8c is described below

commit e181301f8cbf90766ff1b711b298e7bed80a07cc
Author: Michael Bien <[email protected]>
AuthorDate: Thu Mar 9 02:38:49 2023 +0100

    improved JDK detection startup tasks
    
     - repurposed debian task to support more distributions
     - registered JDK names will now receive the (System) or (SDKMAN)
       postfixes
     - only register a JDK if it hasn't been registered yet
---
 .../j2seplatform/DebianJavaPlatformDetector.java   | 68 ----------------
 .../java/j2seplatform/JDKDetectorUtils.java        | 91 ++++++++++++++++++++++
 .../j2seplatform/LinuxJavaPlatformDetector.java    | 74 ++++++++++++++++++
 .../j2seplatform/SdkManJavaPlatformDetector.java   | 49 +++++-------
 .../modules/java/j2seplatform/resources/layer.xml  |  2 +-
 5 files changed, 187 insertions(+), 97 deletions(-)

diff --git 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/DebianJavaPlatformDetector.java
 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/DebianJavaPlatformDetector.java
deleted file mode 100644
index f1bcb2b9b2..0000000000
--- 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/DebianJavaPlatformDetector.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-package org.netbeans.modules.java.j2seplatform;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import 
org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl;
-import org.netbeans.spi.java.platform.JavaPlatformFactory;
-import org.openide.filesystems.FileObject;
-import org.openide.filesystems.FileUtil;
-import org.openide.util.Lookup;
-
-/**
- *
- * @author lkishalmi
- */
-public class DebianJavaPlatformDetector implements Runnable {
-    static final File DEBIAN_JAVA_DIR = new File("/usr/lib/jvm"); //NOI18N
-    static final String JAVA_DIR_MATCHER = 
"java-(\\d+)-openjdk-(amd64|arm64|i386)"; //NOI18N
-
-    @Override
-    public void run() {
-        if (DEBIAN_JAVA_DIR.isDirectory()) {
-            File[] platformDirs = DEBIAN_JAVA_DIR.listFiles((File f) -> 
f.isDirectory() && f.getName().matches(JAVA_DIR_MATCHER));
-            Collection<? extends JavaPlatformFactory.Provider> providers = 
Lookup.getDefault().lookupAll(JavaPlatformFactory.Provider.class);
-            for (JavaPlatformFactory.Provider provider : providers) {
-                JavaPlatformFactory platformFactory = 
provider.forType(J2SEPlatformImpl.PLATFORM_J2SE);
-                if (platformFactory != null) {
-                    for (File platformDir : platformDirs) {
-                        try {
-                            FileObject installFolder = 
FileUtil.toFileObject(platformDir);
-                            platformFactory.create(installFolder, 
getDisplayName(installFolder), true);
-                        } catch (IOException ex) {
-                            //It seems we was not suceeding to add this
-                        } catch(IllegalArgumentException ex) {
-                            //Thrown if the platform is already persisted and 
added to the system.
-                        }
-                    }
-                    break;
-                }
-            }
-        }
-    }
-
-    private static String getDisplayName(FileObject installFolder) {
-        Matcher m = 
Pattern.compile(JAVA_DIR_MATCHER).matcher(installFolder.getNameExt());
-        return m.find() ? "JDK " + m.group(1) : installFolder.getNameExt();
-    }
-}
diff --git 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/JDKDetectorUtils.java
 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/JDKDetectorUtils.java
new file mode 100644
index 0000000000..32994a30e3
--- /dev/null
+++ 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/JDKDetectorUtils.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.netbeans.modules.java.j2seplatform;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.netbeans.api.java.platform.JavaPlatform;
+import org.netbeans.api.java.platform.JavaPlatformManager;
+import 
org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl;
+import org.netbeans.spi.java.platform.JavaPlatformFactory;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Lookup;
+
+/**
+ *
+ * @author mbien
+ */
+class JDKDetectorUtils {
+
+    /**
+     * Registers new JDKs if they aren't registered yet.
+     * @param jdks List of paths to the JDKs.
+     * @param namer Converts a Path to the display name of the JDK.
+     */
+    static void registerJDKs(List<Path> jdks, Function<Path, String> namer) {
+
+        List<JavaPlatform> platforms = 
Stream.of(JavaPlatformManager.getDefault().getInstalledPlatforms())
+                                             .filter(JavaPlatform::isValid)
+                                             .collect(Collectors.toList());
+
+        Set<String> registeredJDKNames = platforms.stream()
+                                                  
.map(JavaPlatform::getDisplayName)
+                                                  .collect(Collectors.toSet());
+
+        Set<Path> registeredPaths = platforms.stream()
+                                             .flatMap(p -> 
p.getInstallFolders().stream())
+                                             .map(f -> Paths.get(f.getPath()))
+                                             .collect(Collectors.toSet());
+
+        for (JavaPlatformFactory.Provider provider : 
Lookup.getDefault().lookupAll(JavaPlatformFactory.Provider.class)) {
+
+            JavaPlatformFactory platformFactory = 
provider.forType(J2SEPlatformImpl.PLATFORM_J2SE);
+            if (platformFactory != null) {
+                for (Path jdk : jdks) {
+                    FileObject fo = FileUtil.toFileObject(jdk);
+                    if (fo != null) {
+                        Object value = 
fo.getAttribute("J2SEPlatform.displayName"); // NOI18N
+                        if (value instanceof String) {
+                            // skip; a value here indicates that the JDK was 
added
+                            // (and potentially subsequently removed again) by 
the user
+                            break;
+                        }
+                    }
+                    try {
+                        String jdkName = namer.apply(jdk);
+                        // don't register if something with the same name or 
same path exists
+                        if (!registeredJDKNames.contains(jdkName) && 
!registeredPaths.contains(jdk)) {
+                            platformFactory.create(FileUtil.toFileObject(jdk), 
jdkName, true);
+                        }
+                    } catch (IOException | IllegalArgumentException ignore) {
+                        // not valid.. probably
+                    }
+                }
+            }
+        }
+    }
+
+}
diff --git 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/LinuxJavaPlatformDetector.java
 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/LinuxJavaPlatformDetector.java
new file mode 100644
index 0000000000..e4a07a4d29
--- /dev/null
+++ 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/LinuxJavaPlatformDetector.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.netbeans.modules.java.j2seplatform;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ *
+ * @author lkishalmi
+ */
+public class LinuxJavaPlatformDetector implements Runnable {
+
+    static final Path LINUX_JAVA_DIR = Paths.get("/usr/lib/jvm"); //NOI18N
+    
+    /*
+     * examples:
+     *  java-17-openjdk-amd64 (debian)
+     *  java-17-openjdk (arch, manjaro)
+     *  java-17-openjdk-17.0.6.0.10-1.fc37.x86_64 (fedora)
+     */
+    static final String JAVA_DIR_MATCHER = "^java-(\\d+)-openjdk(-.+)?"; 
//NOI18N
+
+    @Override
+    public void run() {
+
+        if (Files.isDirectory(LINUX_JAVA_DIR)) {
+
+            try (Stream<Path> files = Files.list(LINUX_JAVA_DIR)) {
+
+                Pattern pattern = Pattern.compile(JAVA_DIR_MATCHER);
+                List<Path> jdks = files.filter(p -> Files.isDirectory(p, 
LinkOption.NOFOLLOW_LINKS))
+                                       .filter(p -> 
pattern.matcher(p.getFileName().toString()).matches())
+                                       .collect(Collectors.toList());
+
+                JDKDetectorUtils.registerJDKs(jdks, path -> 
getDisplayName(path, pattern));
+                
+            } catch (IOException ignore) {
+                // can't list files
+            }
+            
+        }
+    }
+
+    private static String getDisplayName(Path path, Pattern pattern) {
+        String folder = path.getFileName().toString();
+        Matcher m = pattern.matcher(folder);
+        return (m.matches() ? "JDK " + m.group(1) : folder) + " (System)";
+    }
+}
diff --git 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/SdkManJavaPlatformDetector.java
 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/SdkManJavaPlatformDetector.java
index 320d27fc8b..df02aa5fa9 100644
--- 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/SdkManJavaPlatformDetector.java
+++ 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/SdkManJavaPlatformDetector.java
@@ -18,14 +18,14 @@
  */
 package org.netbeans.modules.java.j2seplatform;
 
-import java.io.File;
 import java.io.IOException;
-import java.util.Collection;
-import 
org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl;
-import org.netbeans.spi.java.platform.JavaPlatformFactory;
-import org.openide.filesystems.FileObject;
-import org.openide.filesystems.FileUtil;
-import org.openide.util.Lookup;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  *
@@ -33,34 +33,27 @@ import org.openide.util.Lookup;
  */
 public class SdkManJavaPlatformDetector implements Runnable {
 
-    static final File SDKMAN_JAVA_DIR = new 
File(System.getProperty("user.home"), ".sdkman/candidates/java"); //NOI18N
+    static final Path SDKMAN_JAVA_DIR = 
Paths.get(System.getProperty("user.home"), ".sdkman", "candidates", "java"); 
//NOI18N
 
     @Override
     public void run() {
-        if (SDKMAN_JAVA_DIR.isDirectory()) {
-            File[] platformDirs = SDKMAN_JAVA_DIR.listFiles((File f) -> 
f.isDirectory() && !"current".equals(f.getName())); //NOI18N
-            Collection<? extends JavaPlatformFactory.Provider> providers = 
Lookup.getDefault().lookupAll(JavaPlatformFactory.Provider.class);
-            for (JavaPlatformFactory.Provider provider : providers) {
-                JavaPlatformFactory platformFactory = 
provider.forType(J2SEPlatformImpl.PLATFORM_J2SE);
-                if (platformFactory != null) {
-                    for (File platformDir : platformDirs) {
-                        try {
-                            FileObject installFolder = 
FileUtil.toFileObject(platformDir);
-                            platformFactory.create(installFolder, 
getDisplayName(installFolder), true);
-                        } catch (IOException ex) {
-                            //It seems we was not suceeding to add this
-                        } catch(IllegalArgumentException ex) {
-                            //Thrown if the platform is already persisted and 
added to the system.
-                        }
 
-                    }
-                    break;
-                }
+        if (Files.isDirectory(SDKMAN_JAVA_DIR)) {
+            try (Stream<Path> files = Files.list(SDKMAN_JAVA_DIR)) {
+
+                List<Path> jdks = files.filter(p -> Files.isDirectory(p, 
LinkOption.NOFOLLOW_LINKS))
+                                       .filter(p -> 
!p.getFileName().toString().equals("current"))
+                                       .collect(Collectors.toList());
+
+                JDKDetectorUtils.registerJDKs(jdks, jdk -> 
getDisplayName(jdk));
+            } catch (IOException ignore) {
+                // can't list files
             }
         }
+
     }
 
-    private static String getDisplayName(FileObject installDir) {
-        return "JDK " + installDir.getNameExt(); //NOI18
+    private static String getDisplayName(Path path) {
+        return "JDK " + path.getFileName().toString() + " (SDKMAN)"; //NOI18
     }
 }
diff --git 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/resources/layer.xml
 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/resources/layer.xml
index 39f756b83d..9047d6a454 100644
--- 
a/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/resources/layer.xml
+++ 
b/java/java.j2seplatform/src/org/netbeans/modules/java/j2seplatform/resources/layer.xml
@@ -136,7 +136,7 @@
     
     <folder name="WarmUp">
       <file 
name="org-netbeans-modules-java-j2seplatform-J2SEPlatformWarmUp.instance"/>
-      <file 
name="org-netbeans-modules-java-j2seplatform-DebianJavaPlatformDetector.instance"/>
+      <file 
name="org-netbeans-modules-java-j2seplatform-LinuxJavaPlatformDetector.instance"/>
       <file 
name="org-netbeans-modules-java-j2seplatform-SdkManJavaPlatformDetector.instance"/>
     </folder>
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to