Skip to content

Commit

Permalink
Merge pull request apache#2879 from lkishalmi/NETBEANS-5398
Browse files Browse the repository at this point in the history
[NETBEANS-5398] Fix NPE on Gradle settings when no network connection
  • Loading branch information
geertjanw authored Apr 16, 2021
2 parents 473f2f4 + 2ed9438 commit 2760a68
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
12 changes: 12 additions & 0 deletions extide/gradle/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion extide/gradle/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -207,7 +214,7 @@ public GradleDistribution distributionFromWrapper(File gradleProjectRoot) throws
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
Expand Down Expand Up @@ -309,6 +316,46 @@ public List<GradleDistribution> availableDistributions(boolean releaseOnly) thro
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand All @@ -44,7 +45,6 @@
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;
Expand Down Expand Up @@ -744,19 +744,24 @@ public void setValues() {

@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();
Expand All @@ -775,7 +780,10 @@ public void applyValues() {
} 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();
Expand Down Expand Up @@ -896,10 +904,6 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i

}

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
Expand Down

0 comments on commit 2760a68

Please sign in to comment.