aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-10-26 12:08:04 -0700
committerTor Norbye <tnorbye@google.com>2012-10-26 12:08:04 -0700
commit0ce499b81127ab942fe4164c5e7e259520ec3882 (patch)
tree9ddd0bf85ff62f7ea7b4cf45d271b4b0e5956d41 /eclipse/plugins
parent9e01d02bf6ee2240f01042dc86a417b62711ab6f (diff)
downloadsdk-0ce499b81127ab942fe4164c5e7e259520ec3882.zip
sdk-0ce499b81127ab942fe4164c5e7e259520ec3882.tar.gz
sdk-0ce499b81127ab942fe4164c5e7e259520ec3882.tar.bz2
Attempt to set timeout for URL connection
The timeout apparently isn't respected on all implementations. Also adds a cache, since this method is called repeatedly for each container (one per project?). Change-Id: I2a818815c706bd1a1ec182e38c52543079dad9e1
Diffstat (limited to 'eclipse/plugins')
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidClasspathContainerInitializer.java32
1 files changed, 25 insertions, 7 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidClasspathContainerInitializer.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidClasspathContainerInitializer.java
index bb1e40a..f3d6371 100644
--- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidClasspathContainerInitializer.java
+++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/AndroidClasspathContainerInitializer.java
@@ -25,6 +25,8 @@ import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.sdklib.AndroidVersion;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.IAndroidTarget.IOptionalLibrary;
+import com.google.common.collect.Maps;
+import com.google.common.io.Closeables;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
@@ -51,8 +53,10 @@ import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashSet;
+import java.util.Map;
import java.util.regex.Pattern;
/**
@@ -526,22 +530,36 @@ public class AndroidClasspathContainerInitializer extends BaseClasspathContainer
return androidSourceProperty;
}
+ /**
+ * Cache results for testURL: Some are expensive to compute, and this is
+ * called repeatedly (perhaps for each open project)
+ */
+ private static final Map<String, Boolean> sRecentUrlValidCache =
+ Maps.newHashMapWithExpectedSize(4);
+
+ @SuppressWarnings("resource") // Eclipse does not handle Closeables#closeQuietly
private static boolean testURL(String androidApiURL) {
+ Boolean cached = sRecentUrlValidCache.get(androidApiURL);
+ if (cached != null) {
+ return cached.booleanValue();
+ }
boolean valid = false;
InputStream is = null;
try {
URL testURL = new URL(androidApiURL);
- is = testURL.openStream();
+ URLConnection connection = testURL.openConnection();
+ // Only try for 5 seconds (though some implementations ignore this flag)
+ connection.setConnectTimeout(5000);
+ connection.setReadTimeout(5000);
+ is = connection.getInputStream();
valid = true;
} catch (Exception ignore) {
} finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException ignore) {
- }
- }
+ Closeables.closeQuietly(is);
}
+
+ sRecentUrlValidCache.put(androidApiURL, valid);
+
return valid;
}