You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by ge...@apache.org on 2021/04/16 10:08:24 UTC

[netbeans] branch master updated: [NETBEANS-5398] Fix NPE on Gradle settings when no network connection

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

geertjan 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 2ed9438  [NETBEANS-5398] Fix NPE on Gradle settings when no network connection
     new 2760a68  Merge pull request #2879 from lkishalmi/NETBEANS-5398
2ed9438 is described below

commit 2ed9438cf246931e26658862c6b08b238bd1a4c8
Author: Laszlo Kishalmi <la...@gmail.com>
AuthorDate: Mon Apr 12 09:45:46 2021 -0700

    [NETBEANS-5398] Fix NPE on Gradle settings when no network connection
---
 extide/gradle/apichanges.xml                       | 12 +++++
 extide/gradle/manifest.mf                          |  2 +-
 .../api/execute/GradleDistributionManager.java     | 51 +++++++++++++++++++++-
 .../modules/gradle/options/SettingsPanel.java      | 28 +++++++-----
 4 files changed, 78 insertions(+), 15 deletions(-)

diff --git a/extide/gradle/apichanges.xml b/extide/gradle/apichanges.xml
index b9fcd0f..d985064 100644
--- a/extide/gradle/apichanges.xml
+++ b/extide/gradle/apichanges.xml
@@ -83,6 +83,18 @@ is the proper place.
     <!-- ACTUAL CHANGES BEGIN HERE: -->
 
     <changes>
+        <change id="local-gradle-version-discovery">
+            <api name="general"/>
+            <summary>GradleDistributionManager can detect local Gradle Distributions</summary>
+            <version major="2" minor="10"/>
+            <date day="12" month="4" year="2021"/>
+            <author login="lkishalmi"/>
+            <compatibility semantic="compatible" addition="yes"/>
+            <description>
+                Added <code><a href="@TOP@/org/netbeans/modules/gradle/api/execute/GradleDistributionManager.html#availableLocalDistributions--">GradleDistributionManager.availableLocalDistributions()</a></code> 
+                method to help detection the already available GradleDistribution-s installed in the system.
+            </description>
+        </change>
         <change id="tooling-runJvmArgs">
             <api name="general"/>
             <summary>NetBeans Tooling plugin recognizes "runJvmArgs" property</summary>
diff --git a/extide/gradle/manifest.mf b/extide/gradle/manifest.mf
index fd992b0..5ab8310 100644
--- a/extide/gradle/manifest.mf
+++ b/extide/gradle/manifest.mf
@@ -3,4 +3,4 @@ AutoUpdate-Show-In-Client: false
 OpenIDE-Module: org.netbeans.modules.gradle/2
 OpenIDE-Module-Layer: org/netbeans/modules/gradle/layer.xml
 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/gradle/Bundle.properties
-OpenIDE-Module-Specification-Version: 2.9
+OpenIDE-Module-Specification-Version: 2.10
diff --git a/extide/gradle/src/org/netbeans/modules/gradle/api/execute/GradleDistributionManager.java b/extide/gradle/src/org/netbeans/modules/gradle/api/execute/GradleDistributionManager.java
index 7312bcc..ccb3438 100644
--- a/extide/gradle/src/org/netbeans/modules/gradle/api/execute/GradleDistributionManager.java
+++ b/extide/gradle/src/org/netbeans/modules/gradle/api/execute/GradleDistributionManager.java
@@ -31,8 +31,15 @@ import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.FileVisitOption;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -76,7 +83,7 @@ public final class GradleDistributionManager {
     private static final RequestProcessor RP = new RequestProcessor("Gradle Installer", 1); //NOI18N
 
     private static final String DOWNLOAD_URI = "https://services.gradle.org/distributions/gradle-%s-%s.zip"; //NOI18N
-    private static final Pattern DIST_VERSION_PATTERN = Pattern.compile(".*gradle-(\\d+\\.\\d+.*)-(bin|all)\\.zip"); //NOI18N
+    private static final Pattern DIST_VERSION_PATTERN = Pattern.compile(".*(gradle-(\\d+\\.\\d+.*))-(bin|all)\\.zip"); //NOI18N
     private static final Set<String> VERSION_BLACKLIST = new HashSet<>(Arrays.asList("2.3", "2.13")); //NOI18N
     private static final Map<File, GradleDistributionManager> CACHE = new WeakHashMap<>();
     private static final GradleVersion MINIMUM_SUPPORTED_VERSION = GradleVersion.version("2.0"); //NOI18N
@@ -206,7 +213,7 @@ public final class GradleDistributionManager {
         URI uri = getWrapperDistributionURI(gradleProjectRoot);
         Matcher m = DIST_VERSION_PATTERN.matcher(uri.getPath());
         if (m.matches()) {
-            String version = m.group(1);
+            String version = m.group(2);
             return new GradleDistribution(distributionBaseDir(uri, version), uri, version);
         } else {
             throw new URISyntaxException(uri.getPath(), "Cannot get the Gradle distribution version from the URI"); //NOI18N
@@ -308,6 +315,46 @@ public final class GradleDistributionManager {
         return ret;
     }
 
+    /**
+     * Lists all the {@link GradleDistribution}s available on the Gradle Home
+     * of this distribution manager. It looks for the <code>$GRADLE_HOME/wrapper/dists</code>
+     * directory for already downloaded distributions.
+     * @return the list of available Gradle distributions from the Gradle Home.
+     * @since 2.10
+     */
+    public List<GradleDistribution> availableLocalDistributions() {
+        List<GradleDistribution> ret = new ArrayList<>();
+        Path dists = gradleUserHome.toPath().resolve("wrapper").resolve("dists"); //NOI18N
+        if (Files.isDirectory(dists)) {
+            try {
+                Files.walkFileTree(dists, EnumSet.noneOf(FileVisitOption.class), 2, new SimpleFileVisitor<Path>() {
+                    @Override
+                    public FileVisitResult visitFile(Path f, BasicFileAttributes attrs) throws IOException {
+                        String fname = f.getFileName().toString();
+                        Matcher m = DIST_VERSION_PATTERN.matcher(fname);
+                        if (m.matches()) {
+                            Path dist = f.resolveSibling(m.group(1));
+                            if (Files.isDirectory(dist)) {
+                                try {
+                                    GradleDistribution d = distributionFromDir(dist.toFile());
+                                    if (GradleVersion.version(d.getVersion()).compareTo(MINIMUM_SUPPORTED_VERSION) >= 0) {
+                                        ret.add(d);
+                                    }
+                                } catch (IOException ex) {
+                                    // This might be a broken distribution
+                                }
+                            }
+                        }
+                        return FileVisitResult.CONTINUE;
+                    }
+                });
+            } catch (IOException ex) {
+                //Do nothing if we fail to scan the files
+            }
+        }
+        return ret;
+    }
+    
     File distributionBaseDir(URI downloadLocation, String version) {
         WrapperConfiguration conf = new WrapperConfiguration();
         conf.setDistribution(downloadLocation);
diff --git a/extide/gradle/src/org/netbeans/modules/gradle/options/SettingsPanel.java b/extide/gradle/src/org/netbeans/modules/gradle/options/SettingsPanel.java
index feb6652..da5ca8a 100644
--- a/extide/gradle/src/org/netbeans/modules/gradle/options/SettingsPanel.java
+++ b/extide/gradle/src/org/netbeans/modules/gradle/options/SettingsPanel.java
@@ -28,6 +28,7 @@ import java.awt.Color;
 import java.awt.Component;
 import java.awt.event.ActionEvent;
 import java.awt.event.ItemEvent;
+import java.io.IOException;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import javax.swing.ButtonModel;
@@ -44,7 +45,6 @@ import org.netbeans.modules.gradle.api.execute.GradleDistributionManager;
 import org.netbeans.modules.gradle.api.execute.GradleDistributionManager.GradleDistribution;
 import org.openide.LifecycleManager;
 import org.openide.awt.NotificationDisplayer;
-import org.openide.util.Exceptions;
 import org.openide.util.ImageUtilities;
 import org.openide.util.NbBundle;
 import org.openide.util.NbBundle.Messages;
@@ -744,19 +744,24 @@ public class SettingsPanel extends javax.swing.JPanel {
 
             @Override
             protected List<GradleDistribution> doInBackground() throws Exception {
-                return gdm.availableDistributions(true);
+                try {
+                    return gdm.availableDistributions(true);
+                } catch (IOException ex) {
+                    return gdm.availableLocalDistributions();
+                }
             }
 
             @Override
             protected void done() {
+                GradleDistribution[] items = new GradleDistribution[0];
                 try {
-                    GradleDistribution[] items = get().toArray(new GradleDistribution[0]);
-                    ComboBoxModel<GradleDistribution> model = new DefaultComboBoxModel<>(items);
-                    cbGradleVersion.setModel(model);
-                    model.setSelectedItem(gdm.distributionFromVersion(settings.getGradleVersion()));
+                    items = get().toArray(new GradleDistribution[0]);
                 } catch (InterruptedException | ExecutionException ex) {
-                    Exceptions.printStackTrace(ex);
+                    // Something happened, let's have the combo list box empty;
                 }
+                ComboBoxModel<GradleDistribution> model = new DefaultComboBoxModel<>(items);
+                cbGradleVersion.setModel(model);
+                model.setSelectedItem(gdm.distributionFromVersion(settings.getGradleVersion()));
             }
 
         }.execute();
@@ -775,7 +780,10 @@ public class SettingsPanel extends javax.swing.JPanel {
         } else {
             settings.setGradleUserHome(new File(tfGradleUserHome.getText()));
         }
-        settings.setGradleVersion(((GradleDistribution) cbGradleVersion.getSelectedItem()).getVersion());
+        GradleDistribution distVersion = (GradleDistribution) cbGradleVersion.getSelectedItem();
+        if (distVersion != null) {
+            settings.setGradleVersion(distVersion.getVersion());
+        }
         settings.setDistributionHome(tfUseCustomGradle.getText());
         settings.setWrapperPreferred(cbPreferWrapper.isSelected());
         boolean useCustomGradle = bgUsedDistribution.getSelection() == rbUseCustomGradle.getModel();
@@ -896,10 +904,6 @@ public class SettingsPanel extends javax.swing.JPanel {
 
     }
 
-    private static String getRawGradleUserHome() {
-        return GradleSettings.getDefault().getPreferences().get(GradleSettings.PROP_GRADLE_USER_HOME, null);
-    }
-
     private static String getDefaultGradleUserHome() {
         String dir = System.getenv("GRADLE_USER_HOME"); //NOI18N
         return dir != null ? dir : new File(System.getProperty("user.home"), ".gradle").getAbsolutePath(); //NOI18N

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

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