/hg/icedtea-web: Introduced connection factory

jvanek at icedtea.classpath.org jvanek at icedtea.classpath.org
Fri Oct 24 12:50:00 UTC 2014


changeset a3ee07da4b63 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=a3ee07da4b63
author: Jiri Vanek <jvanek at redhat.com>
date: Fri Oct 24 14:49:21 2014 +0200

	Introduced connection factory


diffstat:

 ChangeLog                                                    |   15 +
 netx/net/sourceforge/jnlp/cache/CacheUtil.java               |   12 +-
 netx/net/sourceforge/jnlp/cache/ResourceTracker.java         |   18 +-
 netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java |    8 +-
 netx/net/sourceforge/jnlp/security/ConnectionFactory.java    |  125 +++++++++++
 plugin/icedteanp/java/sun/applet/PluginAppletViewer.java     |   27 +-
 6 files changed, 165 insertions(+), 40 deletions(-)

diffs (381 lines):

diff -r 99d5407fab4a -r a3ee07da4b63 ChangeLog
--- a/ChangeLog	Thu Oct 23 13:12:39 2014 +0200
+++ b/ChangeLog	Fri Oct 24 14:49:21 2014 +0200
@@ -1,3 +1,18 @@
+2014-10-24  Jiri Vanek  <jvanek at redhat.com>
+
+	Opening of connections moved into factory
+	* netx/net/sourceforge/jnlp/cache/CacheUtil.java:
+	* netx/net/sourceforge/jnlp/cache/ResourceTracker.java:
+	* netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java:
+	* plugin/icedteanp/java/sun/applet/PluginAppletViewer.java:
+	all calls to url.openConnection(), replaced by
+	ConnectionFactory.getConnectionFactory().openConnection(url) and all calls
+	to if (conn instanceof  HttpURLConnection){((HttpURLConnection)conn).disconnect();}
+	by ConnectionFactory.getConnectionFactory().disconnect(conn);
+	* netx/net/sourceforge/jnlp/security/ConnectionFactory.java: new class
+	singleton responsible for opening and closing of connections. Have debugging
+	feature to download https connections one by one (needs tweeking)
+ 
 2014-10-21  Jiri Vanek  <jvanek at redhat.com>
 
 	Fixed case when already decoded file is wonted from cache (RH1154177)
diff -r 99d5407fab4a -r a3ee07da4b63 netx/net/sourceforge/jnlp/cache/CacheUtil.java
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Thu Oct 23 13:12:39 2014 +0200
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Fri Oct 24 14:49:21 2014 +0200
@@ -16,8 +16,6 @@
 
 package net.sourceforge.jnlp.cache;
 
-import static net.sourceforge.jnlp.runtime.Translator.R;
-
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
@@ -26,7 +24,6 @@
 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;
@@ -45,6 +42,9 @@
 import net.sourceforge.jnlp.config.DeploymentConfiguration;
 import net.sourceforge.jnlp.runtime.ApplicationInstance;
 import net.sourceforge.jnlp.runtime.JNLPRuntime;
+import static net.sourceforge.jnlp.runtime.Translator.R;
+
+import net.sourceforge.jnlp.security.ConnectionFactory;
 import net.sourceforge.jnlp.util.FileUtils;
 import net.sourceforge.jnlp.util.PropertiesFile;
 import net.sourceforge.jnlp.util.logging.OutputController;
@@ -99,11 +99,9 @@
         } else {
             try {
                 // this is what URLClassLoader does
-                URLConnection conn = location.openConnection();
+                URLConnection conn = ConnectionFactory.getConnectionFactory().openConnection(location);
                 result = conn.getPermission();
-                if (conn instanceof HttpURLConnection) {
-                    ((HttpURLConnection) conn).disconnect();
-                }                
+                 ConnectionFactory.getConnectionFactory().disconnect(conn);                
             } catch (java.io.IOException ioe) {
                 // should try to figure out the permission
                 OutputController.getLogger().log(ioe);
diff -r 99d5407fab4a -r a3ee07da4b63 netx/net/sourceforge/jnlp/cache/ResourceTracker.java
--- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Thu Oct 23 13:12:39 2014 +0200
+++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Fri Oct 24 14:49:21 2014 +0200
@@ -58,6 +58,7 @@
 import net.sourceforge.jnlp.event.DownloadEvent;
 import net.sourceforge.jnlp.event.DownloadListener;
 import net.sourceforge.jnlp.runtime.JNLPRuntime;
+import net.sourceforge.jnlp.security.ConnectionFactory;
 import net.sourceforge.jnlp.util.HttpUtils;
 import net.sourceforge.jnlp.util.UrlUtils;
 import net.sourceforge.jnlp.util.WeakList;
@@ -622,7 +623,7 @@
         try {
             // create out second in case in does not exist
             URL realLocation = resource.getDownloadLocation();
-            con = realLocation.openConnection();
+            con = ConnectionFactory.getConnectionFactory().openConnection(realLocation);
             con.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");
 
             con.connect();
@@ -679,8 +680,7 @@
                 out.close();
 
                 // explicitly close the URLConnection.
-                if (con instanceof HttpURLConnection)
-                    ((HttpURLConnection) con).disconnect();
+                ConnectionFactory.getConnectionFactory().disconnect(con);
 
                 if (packgz || gzip) {
                     // TODO why not set this otherwise?
@@ -792,7 +792,7 @@
                 }
 
                 resource.setDownloadLocation(finalLocation);
-                connection = finalLocation.openConnection(); // this won't change so should be okay unsynchronized
+                connection = ConnectionFactory.getConnectionFactory().openConnection(finalLocation); // this won't change so should be okay not-synchronized
                 connection.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");
 
                 size = connection.getContentLength();
@@ -836,9 +836,7 @@
             resource.fireDownloadEvent(); // fire CONNECTED
 
             // explicitly close the URLConnection.
-            if (connection instanceof HttpURLConnection) {
-                ((HttpURLConnection) connection).disconnect();
-            }
+           ConnectionFactory.getConnectionFactory().disconnect(connection);           
         } catch (Exception ex) {
             OutputController.getLogger().log(ex);
             resource.changeStatus(EnumSet.noneOf(Resource.Status.class), EnumSet.of(ERROR));
@@ -896,7 +894,7 @@
      */
     static CodeWithRedirect getUrlResponseCodeWithRedirectonResult(URL url, Map<String, String> requestProperties, RequestMethods requestMethod) throws IOException {
         CodeWithRedirect result = new CodeWithRedirect();
-        URLConnection connection = url.openConnection();
+        URLConnection connection = ConnectionFactory.getConnectionFactory().openConnection(url);
 
         for (Map.Entry<String, String> property : requestProperties.entrySet()) {
             connection.addRequestProperty(property.getKey(), property.getValue());
@@ -927,9 +925,7 @@
         if (possibleRedirect != null && possibleRedirect.trim().length() > 0) {
             result.URL = new URL(possibleRedirect);
         }
-        if (connection instanceof HttpURLConnection) {
-            ((HttpURLConnection) connection).disconnect();
-        }
+        ConnectionFactory.getConnectionFactory().disconnect(connection);
 
         return result;
 
diff -r 99d5407fab4a -r a3ee07da4b63 netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java
--- a/netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java	Thu Oct 23 13:12:39 2014 +0200
+++ b/netx/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java	Fri Oct 24 14:49:21 2014 +0200
@@ -43,7 +43,6 @@
 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;
@@ -51,6 +50,7 @@
 import java.security.PrivilegedExceptionAction;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import net.sourceforge.jnlp.security.ConnectionFactory;
 import net.sourceforge.jnlp.util.logging.OutputController;
 import net.sourceforge.jnlp.util.JarFile;
 
@@ -128,7 +128,7 @@
         java.util.jar.JarFile result = null;
 
         final int BUF_SIZE = 2048;
-        URLConnection conn = url.openConnection();
+        URLConnection conn = ConnectionFactory.getConnectionFactory().openConnection(url);
         /* get the stream before asserting privileges */
         final InputStream in = conn.getInputStream();
 
@@ -169,9 +169,7 @@
         } catch (PrivilegedActionException pae) {
             throw (IOException) pae.getException();
         } finally{
-            if (conn instanceof HttpURLConnection) {
-                ((HttpURLConnection) conn).disconnect();
-            }
+           ConnectionFactory.getConnectionFactory().disconnect(conn);
         }
 
         return result;
diff -r 99d5407fab4a -r a3ee07da4b63 netx/net/sourceforge/jnlp/security/ConnectionFactory.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netx/net/sourceforge/jnlp/security/ConnectionFactory.java	Fri Oct 24 14:49:21 2014 +0200
@@ -0,0 +1,125 @@
+/* 
+   Copyright (C) 2014  Red Hat
+
+This file is part of IcedTea.
+
+IcedTea is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+IcedTea is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with IcedTea; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package net.sourceforge.jnlp.security;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.List;
+import javax.net.ssl.HttpsURLConnection;
+import net.sourceforge.jnlp.util.logging.OutputController;
+
+
+public class ConnectionFactory {
+
+    private final List<URLConnection> httpsConnections = new ArrayList<>();
+
+    private boolean isSyncForced() {
+        return false;
+    }
+
+    public static ConnectionFactory getConnectionFactory() {
+        return ConnectionFactoryHolder.INSTANCE;
+    }
+
+    private static class ConnectionFactoryHolder {
+
+        //https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
+        //https://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom
+        private static volatile ConnectionFactory INSTANCE = new ConnectionFactory();
+    }
+
+    public URLConnection openConnection(URL url) throws IOException {
+        OutputController.getLogger().log("Connecting " + url.toExternalForm());
+        if (url.getProtocol().equalsIgnoreCase("https")) {
+            if (isSyncForced()) {
+                OutputController.getLogger().log("Waiting for " + httpsConnections.size() + " connections to finish");
+                while (!httpsConnections.isEmpty()) {
+                    try {
+                        Thread.sleep(100);
+                    } catch (InterruptedException ex) {
+                        throw new IOException(ex);
+                    }
+                }
+            }
+            return openHttpsConnection(url);
+        } else {
+            URLConnection conn = url.openConnection();
+            OutputController.getLogger().log("done " + url.toExternalForm());
+            return conn;
+        }
+    }
+
+    private synchronized URLConnection openHttpsConnection(URL url) throws IOException {
+        URLConnection conn = null;
+        conn = url.openConnection();
+        OutputController.getLogger().log("Adding " + conn.toString());
+        httpsConnections.add(conn);
+        OutputController.getLogger().log("done " + url.toExternalForm());
+        return conn;
+    }
+
+    public void disconnect(URLConnection conn) {
+        OutputController.getLogger().log("Disconnecting " + conn.toString());
+        if (conn instanceof HttpsURLConnection) {
+            closeHttpsConnection((HttpsURLConnection) conn);
+        } else {
+            if (conn instanceof HttpURLConnection) {
+                ((HttpURLConnection) conn).disconnect();
+            }
+        }
+    }
+
+    private synchronized void closeHttpsConnection(HttpsURLConnection conn) {
+        conn.disconnect();
+        //this s intentional search by object value. equals do not work
+        for (int i = 0; i < httpsConnections.size(); i++) {
+            URLConnection urlConnection = httpsConnections.get(i);
+            if (urlConnection == conn) {
+                httpsConnections.remove(i);
+                OutputController.getLogger().log("Removed " + urlConnection.toString());
+                i--;
+
+            }
+
+        }
+    }
+  
+}
diff -r 99d5407fab4a -r a3ee07da4b63 plugin/icedteanp/java/sun/applet/PluginAppletViewer.java
--- a/plugin/icedteanp/java/sun/applet/PluginAppletViewer.java	Thu Oct 23 13:12:39 2014 +0200
+++ b/plugin/icedteanp/java/sun/applet/PluginAppletViewer.java	Fri Oct 24 14:49:21 2014 +0200
@@ -62,8 +62,7 @@
 
 package sun.applet;
 
-import static net.sourceforge.jnlp.runtime.Translator.R;
-
+import com.sun.jndi.toolkit.url.UrlUtil;
 import java.applet.Applet;
 import java.applet.AppletContext;
 import java.applet.AudioClip;
@@ -100,27 +99,25 @@
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.ReentrantLock;
-
 import javax.swing.SwingUtilities;
-
 import net.sourceforge.jnlp.LaunchException;
 import net.sourceforge.jnlp.NetxPanel;
 import net.sourceforge.jnlp.PluginParameters;
 import net.sourceforge.jnlp.runtime.JNLPClassLoader;
+import net.sourceforge.jnlp.runtime.JNLPRuntime;
+import static net.sourceforge.jnlp.runtime.Translator.R;
+
+import net.sourceforge.jnlp.security.ConnectionFactory;
 import net.sourceforge.jnlp.security.appletextendedsecurity.AppletSecurityLevel;
 import net.sourceforge.jnlp.security.appletextendedsecurity.AppletStartupSecuritySettings;
 import net.sourceforge.jnlp.splashscreen.SplashController;
 import net.sourceforge.jnlp.splashscreen.SplashPanel;
 import net.sourceforge.jnlp.splashscreen.SplashUtils;
+import net.sourceforge.jnlp.util.logging.OutputController;
 import sun.awt.AppContext;
 import sun.awt.SunToolkit;
 import sun.awt.X11.XEmbeddedFrame;
 
-import com.sun.jndi.toolkit.url.UrlUtil;
-import java.net.HttpURLConnection;
-import net.sourceforge.jnlp.runtime.JNLPRuntime;
-import net.sourceforge.jnlp.util.logging.OutputController;
-
 /*
  */
 // FIXME: declare JSProxy implementation
@@ -473,14 +470,12 @@
         PluginAppletPanelFactory factory = new PluginAppletPanelFactory();
         AppletMessageHandler amh = new AppletMessageHandler("appletviewer");
         URL url = new URL(documentBase);
-        URLConnection conn = url.openConnection();
+        URLConnection conn = ConnectionFactory.getConnectionFactory().openConnection(url);
         /* The original URL may have been redirected - this
          * sets it to whatever URL/codebase we ended up getting
          */
         url = conn.getURL();
-        if (conn instanceof  HttpURLConnection){
-            ((HttpURLConnection)conn).disconnect();
-        }
+        ConnectionFactory.getConnectionFactory().disconnect(conn);
 
         PluginParameters params = new PluginParameterParser().parse(width, height, paramString);
 
@@ -1550,11 +1545,9 @@
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
             try {
-                URLConnection conn = url.openConnection();
+                URLConnection conn = ConnectionFactory.getConnectionFactory().openConnection(url);
                 java.security.Permission perm = conn.getPermission();
-                if (conn instanceof HttpURLConnection) {
-                    ((HttpURLConnection) conn).disconnect();
-                }
+                ConnectionFactory.getConnectionFactory().disconnect(conn);
                 if (perm != null) {
                     security.checkPermission(perm);
                 }


More information about the distro-pkg-dev mailing list