8026797: Enhance data transfers
authormalenkov
Tue, 21 Jan 2014 20:53:36 +0400
changeset 23905 51af43e32270
parent 23904 4a8ca39187ef
child 23906 8f7f9cb6fe11
8026797: Enhance data transfers Reviewed-by: art, skoivu
jdk/src/macosx/classes/sun/lwawt/macosx/CClipboard.java
jdk/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java
jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java
jdk/src/share/classes/sun/awt/datatransfer/SunClipboard.java
jdk/src/solaris/classes/sun/awt/X11/XClipboard.java
jdk/src/windows/classes/sun/awt/windows/WClipboard.java
--- a/jdk/src/macosx/classes/sun/lwawt/macosx/CClipboard.java	Tue Jan 21 10:49:19 2014 +0000
+++ b/jdk/src/macosx/classes/sun/lwawt/macosx/CClipboard.java	Tue Jan 21 20:53:36 2014 +0400
@@ -58,7 +58,7 @@
 
     @Override
     protected void setContentsNative(Transferable contents) {
-
+        FlavorTable flavorMap = getDefaultFlavorTable();
         // Don't use delayed Clipboard rendering for the Transferable's data.
         // If we did that, we would call Transferable.getTransferData on
         // the Toolkit thread, which is a security hole.
--- a/jdk/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java	Tue Jan 21 10:49:19 2014 +0000
+++ b/jdk/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java	Tue Jan 21 20:53:36 2014 +0400
@@ -46,6 +46,7 @@
 import java.util.Set;
 import java.util.WeakHashMap;
 
+import sun.awt.AppContext;
 import sun.awt.datatransfer.DataTransferer;
 
 /**
@@ -65,10 +66,7 @@
      */
     private static String JavaMIME = "JAVA_DATAFLAVOR:";
 
-    /**
-     * System singleton which maps a thread's ClassLoader to a SystemFlavorMap.
-     */
-    private static final WeakHashMap<ClassLoader, FlavorMap> flavorMaps = new WeakHashMap<>();
+    private static final Object FLAVOR_MAP_KEY = new Object();
 
     /**
      * Copied from java.util.Properties.
@@ -183,22 +181,12 @@
      * @return the default FlavorMap for this thread's ClassLoader
      */
     public static FlavorMap getDefaultFlavorMap() {
-        ClassLoader contextClassLoader =
-            Thread.currentThread().getContextClassLoader();
-        if (contextClassLoader == null) {
-            contextClassLoader = ClassLoader.getSystemClassLoader();
+        AppContext context = AppContext.getAppContext();
+        FlavorMap fm = (FlavorMap) context.get(FLAVOR_MAP_KEY);
+        if (fm == null) {
+            fm = new SystemFlavorMap();
+            context.put(FLAVOR_MAP_KEY, fm);
         }
-
-        FlavorMap fm;
-
-        synchronized(flavorMaps) {
-            fm = flavorMaps.get(contextClassLoader);
-            if (fm == null) {
-                fm = new SystemFlavorMap();
-                flavorMaps.put(contextClassLoader, fm);
-            }
-        }
-
         return fm;
     }
 
@@ -239,26 +227,11 @@
                     }
                 });
 
-        BufferedReader flavormapURL =
+        String url =
             java.security.AccessController.doPrivileged(
-                new java.security.PrivilegedAction<BufferedReader>() {
-                    public BufferedReader run() {
-                        String url = Toolkit.getProperty("AWT.DnD.flavorMapFileURL", null);
-
-                        if (url == null) {
-                            return null;
-                        }
-
-                        try {
-                            return new BufferedReader
-                                (new InputStreamReader
-                                    (new URL(url).openStream(), "ISO-8859-1"));
-                        } catch (MalformedURLException e) {
-                            System.err.println("MalformedURLException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);
-                        } catch (IOException e) {
-                            System.err.println("IOException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);
-                        }
-                        return null;
+                new java.security.PrivilegedAction<String>() {
+                    public String run() {
+                        return Toolkit.getProperty("AWT.DnD.flavorMapFileURL", null);
                     }
                 });
 
@@ -270,6 +243,19 @@
             }
         }
 
+        BufferedReader flavormapURL = null;
+        if (url != null) {
+            try {
+                flavormapURL = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "ISO-8859-1"));
+            } catch (MalformedURLException e) {
+                System.err.println("MalformedURLException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);
+            } catch (IOException e) {
+                System.err.println("IOException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);
+            } catch (SecurityException e) {
+                // ignored
+            }
+        }
+
         if (flavormapURL != null) {
             try {
                 parseAndStoreReader(flavormapURL);
--- a/jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java	Tue Jan 21 10:49:19 2014 +0000
+++ b/jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java	Tue Jan 21 20:53:36 2014 +0400
@@ -85,7 +85,7 @@
                 // read it.
                 Map<Long, Object> cached_data = new HashMap<>(formats.length, 1.0f);
                 DataTransferer.getInstance()
-                        .getFlavorsForFormats(formats, SunClipboard.flavorMap)
+                        .getFlavorsForFormats(formats, SunClipboard.getDefaultFlavorTable())
                         .entrySet()
                         .forEach(entry -> fetchOneFlavor(clipboard, entry.getKey(), entry.getValue(), cached_data));
                 flavors = DataTransferer.setToSortedDataFlavorArray(flavorsToData.keySet());
--- a/jdk/src/share/classes/sun/awt/datatransfer/SunClipboard.java	Tue Jan 21 10:49:19 2014 +0000
+++ b/jdk/src/share/classes/sun/awt/datatransfer/SunClipboard.java	Tue Jan 21 20:53:36 2014 +0400
@@ -64,9 +64,6 @@
 public abstract class SunClipboard extends Clipboard
     implements PropertyChangeListener {
 
-    public static final FlavorTable flavorMap =
-        (FlavorTable)SystemFlavorMap.getDefaultFlavorMap();
-
     private AppContext contentsContext = null;
 
     private final Object CLIPBOARD_FLAVOR_LISTENER_KEY;
@@ -172,7 +169,7 @@
         long[] formats = getClipboardFormatsOpenClose();
 
         return DataTransferer.getInstance().
-            getFlavorsForFormatsAsArray(formats, flavorMap);
+            getFlavorsForFormatsAsArray(formats, getDefaultFlavorTable());
     }
 
     /**
@@ -218,7 +215,7 @@
 
             long[] formats = getClipboardFormats();
             Long lFormat = DataTransferer.getInstance().
-                    getFlavorsForFormats(formats, flavorMap).get(flavor);
+                    getFlavorsForFormats(formats, getDefaultFlavorTable()).get(flavor);
 
             if (lFormat == null) {
                 throw new UnsupportedFlavorException(flavor);
@@ -349,7 +346,7 @@
     private static Set formatArrayAsDataFlavorSet(long[] formats) {
         return (formats == null) ? null :
                 DataTransferer.getInstance().
-                getFlavorsForFormatsAsSet(formats, flavorMap);
+                getFlavorsForFormatsAsSet(formats, getDefaultFlavorTable());
     }
 
 
@@ -469,4 +466,7 @@
         }
     }
 
+    public static FlavorTable getDefaultFlavorTable() {
+        return (FlavorTable) SystemFlavorMap.getDefaultFlavorMap();
+    }
 }
--- a/jdk/src/solaris/classes/sun/awt/X11/XClipboard.java	Tue Jan 21 10:49:19 2014 +0000
+++ b/jdk/src/solaris/classes/sun/awt/X11/XClipboard.java	Tue Jan 21 20:53:36 2014 +0400
@@ -86,7 +86,7 @@
     protected synchronized void setContentsNative(Transferable contents) {
         SortedMap<Long,DataFlavor> formatMap =
             DataTransferer.getInstance().getFormatsForTransferable
-                (contents, DataTransferer.adaptFlavorMap(flavorMap));
+                (contents, DataTransferer.adaptFlavorMap(getDefaultFlavorTable()));
         long[] formats = DataTransferer.keysToLongArray(formatMap);
 
         if (!selection.setOwner(contents, formatMap, formats,
@@ -125,7 +125,7 @@
     private void checkChangeHere(Transferable contents) {
         if (areFlavorListenersRegistered()) {
             checkChange(DataTransferer.getInstance().
-                        getFormatsForTransferableAsArray(contents, flavorMap));
+                        getFormatsForTransferableAsArray(contents, getDefaultFlavorTable()));
         }
     }
 
--- a/jdk/src/windows/classes/sun/awt/windows/WClipboard.java	Tue Jan 21 10:49:19 2014 +0000
+++ b/jdk/src/windows/classes/sun/awt/windows/WClipboard.java	Tue Jan 21 20:53:36 2014 +0400
@@ -62,7 +62,6 @@
 
     @Override
     protected void setContentsNative(Transferable contents) {
-
         // Don't use delayed Clipboard rendering for the Transferable's data.
         // If we did that, we would call Transferable.getTransferData on
         // the Toolkit thread, which is a security hole.
@@ -71,7 +70,7 @@
         // translated. Then, for each format, translate the data and post
         // it to the Clipboard.
         Map <Long, DataFlavor> formatMap = WDataTransferer.getInstance().
-            getFormatsForTransferable(contents, flavorMap);
+            getFormatsForTransferable(contents, getDefaultFlavorTable());
 
         openClipboard(this);