changeset 7050:ff7327ac386e

8026797: Enhance data transfers Reviewed-by: art, skoivu
author malenkov
date Thu, 24 Oct 2013 13:10:36 +0400
parents 07004bb53c3c
children f58ff2903a96
files src/macosx/classes/sun/lwawt/macosx/CClipboard.java src/share/classes/java/awt/datatransfer/SystemFlavorMap.java src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java src/share/classes/sun/awt/datatransfer/SunClipboard.java src/solaris/classes/sun/awt/X11/XClipboard.java src/windows/classes/sun/awt/windows/WClipboard.java
diffstat 6 files changed, 35 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/sun/lwawt/macosx/CClipboard.java	Wed Oct 23 21:32:52 2013 -0700
+++ b/src/macosx/classes/sun/lwawt/macosx/CClipboard.java	Thu Oct 24 13:10:36 2013 +0400
@@ -53,7 +53,7 @@
     }
 
     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/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java	Wed Oct 23 21:32:52 2013 -0700
+++ b/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java	Thu Oct 24 13:10:36 2013 +0400
@@ -45,8 +45,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.WeakHashMap;
 
+import sun.awt.AppContext;
 import sun.awt.datatransfer.DataTransferer;
 
 /**
@@ -72,10 +72,7 @@
      */
     private static String JavaMIME = "JAVA_DATAFLAVOR:";
 
-    /**
-     * System singleton which maps a thread's ClassLoader to a SystemFlavorMap.
-     */
-    private static final WeakHashMap flavorMaps = new WeakHashMap();
+    private static final Object FLAVOR_MAP_KEY = new Object();
 
     /**
      * Copied from java.util.Properties.
@@ -184,22 +181,12 @@
      * Returns 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 = (FlavorMap)flavorMaps.get(contextClassLoader);
-            if (fm == null) {
-                fm = new SystemFlavorMap();
-                flavorMaps.put(contextClassLoader, fm);
-            }
-        }
-
         return fm;
     }
 
@@ -240,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);
                     }
                 });
 
@@ -271,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/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java	Wed Oct 23 21:32:52 2013 -0700
+++ b/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java	Thu Oct 24 13:10:36 2013 +0400
@@ -87,7 +87,7 @@
                 HashMap cached_data = new HashMap(formats.length, 1.0f);
 
                 Map flavorsForFormats = DataTransferer.getInstance().
-                    getFlavorsForFormats(formats, SunClipboard.flavorMap);
+                    getFlavorsForFormats(formats, SunClipboard.getDefaultFlavorTable());
                 for (Iterator iter = flavorsForFormats.keySet().iterator();
                      iter.hasNext(); )
                 {
--- a/src/share/classes/sun/awt/datatransfer/SunClipboard.java	Wed Oct 23 21:32:52 2013 -0700
+++ b/src/share/classes/sun/awt/datatransfer/SunClipboard.java	Thu Oct 24 13:10:36 2013 +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 = (Long)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/src/solaris/classes/sun/awt/X11/XClipboard.java	Wed Oct 23 21:32:52 2013 -0700
+++ b/src/solaris/classes/sun/awt/X11/XClipboard.java	Thu Oct 24 13:10:36 2013 +0400
@@ -84,7 +84,7 @@
 
     protected synchronized void setContentsNative(Transferable contents) {
         SortedMap formatMap = DataTransferer.getInstance().getFormatsForTransferable
-                (contents, DataTransferer.adaptFlavorMap(flavorMap));
+                (contents, DataTransferer.adaptFlavorMap(getDefaultFlavorTable()));
         long[] formats = DataTransferer.keysToLongArray(formatMap);
 
         if (!selection.setOwner(contents, formatMap, formats,
@@ -123,7 +123,7 @@
     private void checkChangeHere(Transferable contents) {
         if (areFlavorListenersRegistered()) {
             checkChange(DataTransferer.getInstance().
-                        getFormatsForTransferableAsArray(contents, flavorMap));
+                        getFormatsForTransferableAsArray(contents, getDefaultFlavorTable()));
         }
     }
 
--- a/src/windows/classes/sun/awt/windows/WClipboard.java	Wed Oct 23 21:32:52 2013 -0700
+++ b/src/windows/classes/sun/awt/windows/WClipboard.java	Thu Oct 24 13:10:36 2013 +0400
@@ -63,7 +63,6 @@
     }
 
     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.
@@ -72,7 +71,7 @@
         // translated. Then, for each format, translate the data and post
         // it to the Clipboard.
         Map formatMap = WDataTransferer.getInstance().
-            getFormatsForTransferable(contents, flavorMap);
+            getFormatsForTransferable(contents, getDefaultFlavorTable());
 
         openClipboard(this);