changeset 989:e8b21e10ead6

Properly disconnect all connected http connections.
author Jiri Vanek <jvanek@redhat.com>
date Mon, 05 May 2014 15:38:16 +0200
parents 8012d1bbbe72
children 7e5db2ce19da
files ChangeLog netx/net/sourceforge/jnlp/cache/CacheEntry.java netx/net/sourceforge/jnlp/cache/CacheUtil.java netx/net/sourceforge/jnlp/cache/ResourceTracker.java netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java netx/net/sourceforge/jnlp/services/XBasicService.java plugin/icedteanp/java/sun/applet/PluginAppletViewer.java tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java
diffstat 9 files changed, 137 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri May 02 19:11:11 2014 -0400
+++ b/ChangeLog	Mon May 05 15:38:16 2014 +0200
@@ -1,3 +1,24 @@
+2014-05-05  Jiri Vanek  <jvanek@redhat.com>
+
+	Properly disconnect all connected http connections
+	* netx/net/sourceforge/jnlp/cache/CacheEntry.java: (isCurrent) is using
+	lastModified of long rather then whole url connection. Callers adapted.
+	* netx/net/sourceforge/jnlp/cache/CacheUtil.java: (getReadPermission)
+	closed http connection.	(isCurrent)  retyped to use lastModified of long
+	rather then whole connection. Removed workaround of it. Callers adapted.
+	* netx/net/sourceforge/jnlp/cache/ResourceTracker.java: (downloadResource)
+	Closed http connection.
+	* netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java: Closed http
+	connection.
+	* netx/net/sourceforge/jnlp/services/XBasicService.java: (isOffline) Closed
+	http connection.
+	* plugin/icedteanp/java/sun/applet/PluginAppletViewer.java: Closed http
+	connections.
+	* tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java:
+	Adapted to URLconnection -> long signature changes.
+	* tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java: Closed http
+	connections.
+
 2014-05-02  Omair Majid  <omajid@redhat.com>
 
 	* netx/net/sourceforge/jnlp/cache/DefaultDownloadIndicator.java: Switch to
--- a/netx/net/sourceforge/jnlp/cache/CacheEntry.java	Fri May 02 19:11:11 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/CacheEntry.java	Mon May 05 15:38:16 2014 +0200
@@ -82,6 +82,7 @@
     /**
      * Returns the time in the local system clock that the file was
      * most recently checked for an update.
+     * @return 
      */
     public long getLastUpdated() {
         try {
@@ -94,6 +95,7 @@
     /**
      * Sets the time in the local system clock that the file was
      * most recently checked for an update.
+     * @param updatedTime
      */
     public void setLastUpdated(long updatedTime) {
         properties.setProperty("last-updated", Long.toString(updatedTime));
@@ -104,17 +106,17 @@
      * the cache and it is up to date.  This method may not return
      * immediately.
      *
-     * @param connection a connection to the remote URL
+     * @param lastModified
      * @return whether the cache contains the version
      */
-    public boolean isCurrent(URLConnection connection) {
+    public boolean isCurrent(long lastModified) {
         boolean cached = isCached();
 
         if (!cached)
             return false;
 
         try {
-            long remoteModified = connection.getLastModified();
+            long remoteModified = lastModified;
             long cachedModified = Long.parseLong(properties.getProperty("last-modified"));
 
             if (remoteModified > 0 && remoteModified <= cachedModified)
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Fri May 02 19:11:11 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Mon May 05 15:38:16 2014 +0200
@@ -27,6 +27,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
@@ -149,23 +150,30 @@
     /**
      * Returns the Permission object necessary to access the
      * resource, or {@code null} if no permission is needed.
+     * @param location
+     * @param version
+     * @return 
      */
     public static Permission getReadPermission(URL location, Version version) {
+        Permission result = null;
         if (CacheUtil.isCacheable(location, version)) {
             File file = CacheUtil.getCacheFile(location, version);
-
-            return new FilePermission(file.getPath(), "read");
+            result = new FilePermission(file.getPath(), "read");
         } else {
             try {
                 // this is what URLClassLoader does
-                return location.openConnection().getPermission();
+                URLConnection conn = location.openConnection();
+                result = conn.getPermission();
+                if (conn instanceof HttpURLConnection) {
+                    ((HttpURLConnection) conn).disconnect();
+                }                
             } catch (java.io.IOException ioe) {
                 // should try to figure out the permission
                 OutputController.getLogger().log(ioe);
             }
         }
 
-        return null;
+        return result;
     }
 
     /**
@@ -242,23 +250,18 @@
      *
      * @param source the source {@link URL}
      * @param version the versions to check for
-     * @param connection a connection to the {@link URL}, or {@code null}
+     * @param lastModifed
      * @return whether the cache contains the version
      * @throws IllegalArgumentException if the source is not cacheable
      */
-    public static boolean isCurrent(URL source, Version version, URLConnection connection) {
+    public static boolean isCurrent(URL source, Version version, long lastModifed) {
 
         if (!isCacheable(source, version))
             throw new IllegalArgumentException(R("CNotCacheable", source));
 
         try {
-            if (connection == null)
-                connection = source.openConnection();
-
-            connection.connect();
-
             CacheEntry entry = new CacheEntry(source, version); // could pool this
-            boolean result = entry.isCurrent(connection);
+            boolean result = entry.isCurrent(lastModifed);
 
             OutputController.getLogger().log("isCurrent: " + source + " = " + result);
 
--- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Fri May 02 19:11:11 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Mon May 05 15:38:16 2014 +0200
@@ -647,14 +647,14 @@
      */
     private void downloadResource(Resource resource) {
         resource.fireDownloadEvent(); // fire DOWNLOADING
-
+        URLConnection con = null;
         CacheEntry origEntry = new CacheEntry(resource.location, resource.downloadVersion); // This is where the jar file will be.
         origEntry.lock();
 
         try {
             // create out second in case in does not exist
             URL realLocation = resource.getDownloadLocation();
-            URLConnection con = realLocation.openConnection();
+            con = realLocation.openConnection();
             con.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");
 
             con.connect();
@@ -689,7 +689,7 @@
             CacheEntry downloadEntry = new CacheEntry(downloadLocation, resource.downloadVersion);
             File finalFile = CacheUtil.getCacheFile(resource.location, resource.downloadVersion); // This is where extracted version will be, or downloaded file if not compressed.
 
-            if (!downloadEntry.isCurrent(con)) {
+            if (!downloadEntry.isCurrent(con.getLastModified())) {
                 // Make sure we don't re-download the file. however it will wait as if it was downloading.
                 // (This is fine because file is not ready yet anyways)
                 byte buf[] = new byte[1024];
@@ -768,6 +768,11 @@
             resource.fireDownloadEvent(); // fire ERROR
         } finally {
             origEntry.unlock();
+            if (con != null) {
+                if (con instanceof HttpURLConnection) {
+                    ((HttpURLConnection) con).disconnect();
+                }
+            }
         }
     }
 
@@ -797,7 +802,7 @@
             connection.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");
 
             int size = connection.getContentLength();
-            boolean current = CacheUtil.isCurrent(resource.location, resource.requestVersion, connection) && resource.getUpdatePolicy() != UpdatePolicy.FORCE;
+            boolean current = CacheUtil.isCurrent(resource.location, resource.requestVersion, connection.getLastModified()) && resource.getUpdatePolicy() != UpdatePolicy.FORCE;
             if (!current) {
                 if (entry.isCached()) {
                     entry.markForDelete();
@@ -835,8 +840,9 @@
             resource.fireDownloadEvent(); // fire CONNECTED
 
             // explicitly close the URLConnection.
-            if (connection instanceof HttpURLConnection)
+            if (connection instanceof HttpURLConnection) {
                 ((HttpURLConnection) connection).disconnect();
+            }
         } catch (Exception ex) {
             OutputController.getLogger().log(ex);
             resource.changeStatus(0, ERROR);
@@ -921,6 +927,9 @@
         if (possibleRedirect != null && possibleRedirect.trim().length() > 0) {
             result.URL = new URL(possibleRedirect);
         }
+        if (connection instanceof HttpURLConnection) {
+            ((HttpURLConnection) connection).disconnect();
+        }
 
         return result;
 
--- a/netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java	Fri May 02 19:11:11 2014 -0400
+++ b/netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java	Mon May 05 15:38:16 2014 +0200
@@ -43,7 +43,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLConnection;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
@@ -126,9 +128,9 @@
         java.util.jar.JarFile result = null;
 
         final int BUF_SIZE = 2048;
-
+        URLConnection conn = url.openConnection();
         /* get the stream before asserting privileges */
-        final InputStream in = url.openConnection().getInputStream();
+        final InputStream in = conn.getInputStream();
 
         try {
             result =
@@ -166,6 +168,10 @@
                     });
         } catch (PrivilegedActionException pae) {
             throw (IOException) pae.getException();
+        } finally{
+            if (conn instanceof HttpURLConnection) {
+                ((HttpURLConnection) conn).disconnect();
+            }
         }
 
         return result;
--- a/netx/net/sourceforge/jnlp/services/XBasicService.java	Fri May 02 19:11:11 2014 -0400
+++ b/netx/net/sourceforge/jnlp/services/XBasicService.java	Mon May 05 15:38:16 2014 +0200
@@ -19,8 +19,10 @@
 import static net.sourceforge.jnlp.runtime.Translator.R;
 
 import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
 
 import javax.jnlp.BasicService;
 import javax.swing.JOptionPane;
@@ -59,6 +61,7 @@
      * JAR was specified then the location of the JAR containing the
      * main class is returned.
      */
+    @Override
     public URL getCodeBase() {
         ApplicationInstance app = JNLPRuntime.getApplication();
 
@@ -88,15 +91,21 @@
     /**
      * Return true if the Environment is Offline
      */
+    @Override
     public boolean isOffline() {
 
         URL url = findFirstURLFromJNLPFile();
-
+        URLConnection conn = null;
         try {
-            url.openConnection().getInputStream().close();
+            conn = url.openConnection();
+            conn.getInputStream().close();
             return false;
         } catch (IOException exception) {
             return true;
+        } finally {
+            if (conn != null && conn instanceof HttpURLConnection) {
+                ((HttpURLConnection) conn).disconnect();
+            }
         }
     }
 
@@ -148,6 +157,7 @@
     /**
      * Return true if a Web Browser is Supported
      */
+    @Override
     public boolean isWebBrowserSupported() {
         initialize();
 
@@ -159,6 +169,7 @@
      *
      * @return whether the document was opened
      */
+    @Override
     public boolean showDocument(URL url) {
         initialize();
 
--- a/plugin/icedteanp/java/sun/applet/PluginAppletViewer.java	Fri May 02 19:11:11 2014 -0400
+++ b/plugin/icedteanp/java/sun/applet/PluginAppletViewer.java	Mon May 05 15:38:16 2014 +0200
@@ -118,6 +118,7 @@
 import sun.misc.Ref;
 
 import com.sun.jndi.toolkit.url.UrlUtil;
+import java.net.HttpURLConnection;
 import net.sourceforge.jnlp.runtime.JNLPRuntime;
 import net.sourceforge.jnlp.util.logging.OutputController;
 
@@ -478,6 +479,9 @@
          * sets it to whatever URL/codebase we ended up getting
          */
         url = conn.getURL();
+        if (conn instanceof  HttpURLConnection){
+            ((HttpURLConnection)conn).disconnect();
+        }
 
         PluginParameters params = new PluginParameterParser().parse(width, height, paramString);
 
@@ -1543,8 +1547,11 @@
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
             try {
-                java.security.Permission perm =
-                        url.openConnection().getPermission();
+                URLConnection conn = url.openConnection();
+                java.security.Permission perm = conn.getPermission();
+                if (conn instanceof HttpURLConnection) {
+                    ((HttpURLConnection) conn).disconnect();
+                }
                 if (perm != null) {
                     security.checkPermission(perm);
                 }
--- a/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java	Fri May 02 19:11:11 2014 -0400
+++ b/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java	Mon May 05 15:38:16 2014 +0200
@@ -38,7 +38,9 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLConnection;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -158,10 +160,12 @@
         String dirFileContent = ServerAccess.getContentOfStream(new FileInputStream(dirFile));
 
         URL portUrl = new URL("http", "localhost", server.getPort(), "/server.port");
+        HttpURLConnection portConn = (HttpURLConnection) portUrl.openConnection();
         URL dirUrl = new URL("http", "localhost", server.getPort(), "/server.dir");
+        HttpURLConnection dirConn = (HttpURLConnection) dirUrl.openConnection();
 
-        String portUrlContent = ServerAccess.getContentOfStream(portUrl.openConnection().getInputStream());
-        String dirUrlContent = ServerAccess.getContentOfStream(dirUrl.openConnection().getInputStream());
+        String portUrlContent = ServerAccess.getContentOfStream(portConn.getInputStream());
+        String dirUrlContent = ServerAccess.getContentOfStream(dirConn.getInputStream());
 
         Assert.assertEquals(portUrlContent.trim(), portFileContent.trim());
         Assert.assertEquals(dirUrlContent.trim(), dirFileContent.trim());
@@ -169,11 +173,18 @@
         Assert.assertEquals(new Integer(portUrlContent.trim()), server.getPort());
 
         URL fastUrl = new URL("http", "localhost", server.getPort(), "/simpletest1.jnlp");
+        HttpURLConnection fastUrlConn = (HttpURLConnection) fastUrl.openConnection();
         URL slowUrl = new URL("http", "localhost", server.getPort(), "/XslowXsimpletest1.jnlp");
+        HttpURLConnection slowUrlConn = (HttpURLConnection) slowUrl.openConnection();
 
-        String fastUrlcontent = ServerAccess.getContentOfStream(fastUrl.openConnection().getInputStream());
-        String slowUrlContent = ServerAccess.getContentOfStream(slowUrl.openConnection().getInputStream());
+        String fastUrlcontent = ServerAccess.getContentOfStream(fastUrlConn.getInputStream());
+        String slowUrlContent = ServerAccess.getContentOfStream(slowUrlConn.getInputStream());
         Assert.assertEquals(fastUrlcontent, slowUrlContent);
+        
+        portConn.disconnect();
+        dirConn.disconnect();
+        fastUrlConn.disconnect();
+        slowUrlConn.disconnect();
 
     }
 
--- a/tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java	Fri May 02 19:11:11 2014 -0400
+++ b/tests/test-extensions/net/sourceforge/jnlp/ServerAccess.java	Mon May 05 15:38:16 2014 +0200
@@ -53,6 +53,7 @@
 import java.net.MalformedURLException;
 import java.net.ServerSocket;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.List;
 import net.sourceforge.jnlp.ProcessResult;
@@ -427,7 +428,7 @@
     /**
      * utility method which can read from any stream as one long String
      *
-     * @param input stream
+     * @param is
      * @return stream as string
      * @throws IOException if connection can't be established or resource does not exist
      */
@@ -438,36 +439,53 @@
     /**
      * utility method which can read bytes of resource from any url
      * 
-     * @param resource to be located on any url
+     * @param u
      * @return individual bytes of resource
      * @throws IOException if connection can't be established or resource does not exist
      */
     public static ByteArrayOutputStream getResourceAsBytes(URL u) throws IOException {
-        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
-        connection = (HttpURLConnection) u.openConnection();
-        connection.setRequestMethod("GET");
-        connection.setDoOutput(true);
-        connection.setReadTimeout(READ_TIMEOUT);
-        connection.connect();
-        return getBytesFromStream(connection.getInputStream());
+        URLConnection connection = null;
+        try {
+            connection = u.openConnection();
+            if (connection instanceof HttpURLConnection) {
+                ((HttpURLConnection) connection).setRequestMethod("GET");
+            }
+            connection.setDoOutput(true);
+            connection.setReadTimeout(READ_TIMEOUT);
+            connection.connect();
+            return getBytesFromStream(connection.getInputStream());
+        } finally {
+            if (connection != null && connection instanceof HttpURLConnection) {
+                ((HttpURLConnection) connection).disconnect();
+            }
+        }
 
     }
 
     /**
      * utility method which can read string of resource from any url
-     * 
-     * @param resource to be located on any url
+     *
+     * @param u
      * @return resource as string
-     * @throws IOException if connection can't be established or resource does not exist
+     * @throws IOException if connection can't be established or resource does
+     * not exist
      */
     public static String getResourceAsString(URL u) throws IOException {
-        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
-        connection = (HttpURLConnection) u.openConnection();
-        connection.setRequestMethod("GET");
-        connection.setDoOutput(true);
-        connection.setReadTimeout(READ_TIMEOUT);
-        connection.connect();
-        return getContentOfStream(connection.getInputStream());
+        URLConnection connection = null;
+        try {
+            connection = (HttpURLConnection) u.openConnection();
+            if (connection instanceof HttpURLConnection) {
+                ((HttpURLConnection) connection).setRequestMethod("GET");
+            }
+            connection.setDoOutput(true);
+            connection.setReadTimeout(READ_TIMEOUT);
+            connection.connect();
+            return getContentOfStream(connection.getInputStream());
+        } finally {
+            if (connection != null && connection instanceof HttpURLConnection) {
+                ((HttpURLConnection) connection).disconnect();
+            }
+        }
     }
 
     /**