8163449: Allow per protocol setting for URLConnection defaultUseCaches
authormichaelm
Thu, 12 Jan 2017 18:02:48 +0000
changeset 43070 2c327aa46246
parent 43069 6653ea6dad6e
child 43071 acc32a3bcb73
8163449: Allow per protocol setting for URLConnection defaultUseCaches Reviewed-by: chegar, dfuchs
jdk/src/java.base/share/classes/java/net/URLConnection.java
jdk/test/java/net/URLConnection/SetDefaultUseCaches.java
--- a/jdk/src/java.base/share/classes/java/net/URLConnection.java	Thu Jan 12 17:07:49 2017 +0100
+++ b/jdk/src/java.base/share/classes/java/net/URLConnection.java	Thu Jan 12 18:02:48 2017 +0000
@@ -30,8 +30,10 @@
 import java.io.OutputStream;
 import java.security.PrivilegedAction;
 import java.util.Hashtable;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.Date;
 import java.util.Iterator;
+import java.util.Locale;
 import java.util.Objects;
 import java.util.ServiceConfigurationError;
 import java.util.ServiceLoader;
@@ -231,7 +233,7 @@
      */
     protected boolean allowUserInteraction = defaultAllowUserInteraction;
 
-    private static boolean defaultUseCaches = true;
+    private static volatile boolean defaultUseCaches = true;
 
    /**
      * If {@code true}, the protocol is allowed to use caching
@@ -243,12 +245,18 @@
      * <p>
      * Its default value is the value given in the last invocation of the
      * {@code setDefaultUseCaches} method.
+     * <p>
+     * The default setting may be overridden per protocol with
+     * {@link #setDefaultUseCaches(String,boolean)}.
      *
      * @see     java.net.URLConnection#setUseCaches(boolean)
      * @see     java.net.URLConnection#getUseCaches()
      * @see     java.net.URLConnection#setDefaultUseCaches(boolean)
      */
-    protected boolean useCaches = defaultUseCaches;
+    protected boolean useCaches;
+
+    private static final ConcurrentHashMap<String,Boolean> defaultCaching =
+        new ConcurrentHashMap<>();
 
    /**
      * Some protocols support skipping the fetching of the object unless
@@ -460,6 +468,11 @@
      */
     protected URLConnection(URL url) {
         this.url = url;
+        if (url == null) {
+            this.useCaches = defaultUseCaches;
+        } else {
+            this.useCaches = getDefaultUseCaches(url.getProtocol());
+        }
     }
 
     /**
@@ -981,7 +994,8 @@
      * is true, the connection is allowed to use whatever caches it can.
      *  If false, caches are to be ignored.
      *  The default value comes from DefaultUseCaches, which defaults to
-     * true.
+     * true. A default value can also be set per-protocol using
+     * {@link #setDefaultUseCaches(String,boolean)}.
      *
      * @param usecaches a {@code boolean} indicating whether
      * or not to allow caching
@@ -1032,9 +1046,10 @@
      * Returns the default value of a {@code URLConnection}'s
      * {@code useCaches} flag.
      * <p>
-     * Ths default is "sticky", being a part of the static state of all
+     * This default is "sticky", being a part of the static state of all
      * URLConnections.  This flag applies to the next, and all following
-     * URLConnections that are created.
+     * URLConnections that are created. This default value can be over-ridden
+     * per protocol using {@link #setDefaultUseCaches(String,boolean)}
      *
      * @return  the default value of a {@code URLConnection}'s
      *          {@code useCaches} flag.
@@ -1046,7 +1061,8 @@
 
    /**
      * Sets the default value of the {@code useCaches} field to the
-     * specified value.
+     * specified value. This default value can be over-ridden
+     * per protocol using {@link #setDefaultUseCaches(String,boolean)}
      *
      * @param   defaultusecaches   the new value.
      * @see     #getDefaultUseCaches()
@@ -1055,6 +1071,43 @@
         defaultUseCaches = defaultusecaches;
     }
 
+   /**
+     * Sets the default value of the {@code useCaches} field for the named
+     * protocol to the given value. This value overrides any default setting
+     * set by {@link #setDefaultUseCaches(boolean)} for the given protocol.
+     * Successive calls to this method change the setting and affect the
+     * default value for all future connections of that protocol. The protocol
+     * name is case insensitive.
+     *
+     * @param   protocol the protocol to set the default for
+     * @param   defaultVal whether caching is enabled by default for the given protocol
+     * @since 9
+     */
+    public static void setDefaultUseCaches(String protocol, boolean defaultVal) {
+        protocol = protocol.toLowerCase(Locale.US);
+        defaultCaching.put(protocol, defaultVal);
+    }
+
+   /**
+     * Returns the default value of the {@code useCaches} flag for the given protocol. If
+     * {@link #setDefaultUseCaches(String,boolean)} was called for the given protocol,
+     * then that value is returned. Otherwise, if {@link #setDefaultUseCaches(boolean)}
+     * was called, then that value is returned. If neither method was called,
+     * the return value is {@code true}. The protocol name is case insensitive.
+     *
+     * @param protocol the protocol whose defaultUseCaches setting is required
+     * @return  the default value of the {@code useCaches} flag for the given protocol.
+     * @since 9
+     */
+    public static boolean getDefaultUseCaches(String protocol) {
+        Boolean protoDefault = defaultCaching.get(protocol.toLowerCase(Locale.US));
+        if (protoDefault != null) {
+            return protoDefault.booleanValue();
+        } else {
+            return defaultUseCaches;
+        }
+    }
+
     /**
      * Sets the general request property. If a property with the key already
      * exists, overwrite its value with the new value.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/net/URLConnection/SetDefaultUseCaches.java	Thu Jan 12 18:02:48 2017 +0000
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8163449
+ * @summary Allow per protocol setting for URLConnection defaultUseCaches
+ * @run main/othervm SetDefaultUseCaches
+ */
+
+import java.net.*;
+import java.io.*;
+
+public class SetDefaultUseCaches {
+    static void testAssert(boolean value, boolean comparator) {
+        if (value != comparator) {
+            System.err.println("Expected " + comparator + " Got " + value);
+            throw new RuntimeException("Test failed:");
+        } else
+            System.err.println("OK");
+    }
+
+    public static void main(String s[]) throws Exception {
+        URL url = new URL("http://www.foo.com/");
+        URL url1 = new URL("file:///a/b.txt");
+
+        // check default default is true
+        URLConnection urlc = url.openConnection();
+        testAssert(urlc.getDefaultUseCaches(), true);
+
+        // set default for http to false and check
+        URLConnection.setDefaultUseCaches("HTTP", false);
+
+        urlc = url.openConnection();
+        testAssert(urlc.getDefaultUseCaches(), true);
+        testAssert(urlc.getUseCaches(), false);
+        testAssert(URLConnection.getDefaultUseCaches("http"), false);
+
+        URLConnection urlc1 = url1.openConnection();
+        testAssert(urlc1.getDefaultUseCaches(), true);
+
+        // set default default to false and check other values the same
+        urlc.setDefaultUseCaches(false);
+        urlc1.setDefaultUseCaches("fiLe", true);
+        testAssert(urlc1.getDefaultUseCaches(), false);
+        testAssert(URLConnection.getDefaultUseCaches("fiLE"), true);
+    }
+}