changeset 781:a41e0d5e8068

8025152: Enhance activation set up Summary: fix also reviewed by Alexander Fomin Reviewed-by: dfuchs, hawtin Contributed-by: bill.shannon@oracle.com
author mkos
date Tue, 12 Nov 2013 11:22:53 +0100
parents da128632f015
children 056a132d0223
files src/share/jaf_classes/javax/activation/CommandMap.java src/share/jaf_classes/javax/activation/DataHandler.java src/share/jaf_classes/javax/activation/FileTypeMap.java src/share/jaf_classes/javax/activation/MailcapCommandMap.java src/share/jaf_classes/javax/activation/MimetypesFileTypeMap.java
diffstat 5 files changed, 64 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/jaf_classes/javax/activation/CommandMap.java	Sat Nov 09 10:19:31 2013 +0100
+++ b/src/share/jaf_classes/javax/activation/CommandMap.java	Tue Nov 12 11:22:53 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, 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
@@ -25,6 +25,9 @@
 
 package javax.activation;
 
+import java.util.Map;
+import java.util.WeakHashMap;
+
 
 /**
  * The CommandMap class provides an interface to a registry of
@@ -38,6 +41,8 @@
  */
 public abstract class CommandMap {
     private static CommandMap defaultCommandMap = null;
+    private static Map<ClassLoader,CommandMap> map =
+                                new WeakHashMap<ClassLoader,CommandMap>();
 
     /**
      * Get the default CommandMap.
@@ -56,11 +61,18 @@
      *
      * @return the CommandMap
      */
-    public static CommandMap getDefaultCommandMap() {
-        if (defaultCommandMap == null)
-            defaultCommandMap = new MailcapCommandMap();
+    public static synchronized CommandMap getDefaultCommandMap() {
+        if (defaultCommandMap != null)
+            return defaultCommandMap;
 
-        return defaultCommandMap;
+        // fetch per-thread-context-class-loader default
+        ClassLoader tccl = SecuritySupport.getContextClassLoader();
+        CommandMap def = map.get(tccl);
+        if (def == null) {
+            def = new MailcapCommandMap();
+            map.put(tccl, def);
+        }
+        return def;
     }
 
     /**
@@ -71,7 +83,7 @@
      * @exception SecurityException if the caller doesn't have permission
      *                                  to change the default
      */
-    public static void setDefaultCommandMap(CommandMap commandMap) {
+    public static synchronized void setDefaultCommandMap(CommandMap commandMap) {
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
             try {
@@ -79,13 +91,16 @@
                 security.checkSetFactory();
             } catch (SecurityException ex) {
                 // otherwise, we also allow it if this code and the
-                // factory come from the same class loader (e.g.,
+                // factory come from the same (non-system) class loader (e.g.,
                 // the JAF classes were loaded with the applet classes).
-                if (CommandMap.class.getClassLoader() !=
+                if (CommandMap.class.getClassLoader() == null ||
+                    CommandMap.class.getClassLoader() !=
                             commandMap.getClass().getClassLoader())
                     throw ex;
             }
         }
+        // remove any per-thread-context-class-loader CommandMap
+        map.remove(SecuritySupport.getContextClassLoader());
         defaultCommandMap = commandMap;
     }
 
--- a/src/share/jaf_classes/javax/activation/DataHandler.java	Sat Nov 09 10:19:31 2013 +0100
+++ b/src/share/jaf_classes/javax/activation/DataHandler.java	Tue Nov 12 11:22:53 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, 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
@@ -368,7 +368,12 @@
         // if it's not set, set it...
         if (transferFlavors == emptyFlavors)
             transferFlavors = getDataContentHandler().getTransferDataFlavors();
-        return transferFlavors;
+
+        if (transferFlavors == emptyFlavors)
+            return transferFlavors;
+        else
+            return transferFlavors.clone();
+
     }
 
     /**
--- a/src/share/jaf_classes/javax/activation/FileTypeMap.java	Sat Nov 09 10:19:31 2013 +0100
+++ b/src/share/jaf_classes/javax/activation/FileTypeMap.java	Tue Nov 12 11:22:53 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, 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
@@ -26,6 +26,8 @@
 package javax.activation;
 
 import java.io.File;
+import java.util.Map;
+import java.util.WeakHashMap;
 
 /**
  * The FileTypeMap is an abstract class that provides a data typing
@@ -48,6 +50,8 @@
 public abstract class FileTypeMap {
 
     private static FileTypeMap defaultMap = null;
+    private static Map<ClassLoader,FileTypeMap> map =
+                                new WeakHashMap<ClassLoader,FileTypeMap>();
 
     /**
      * The default constructor.
@@ -78,11 +82,11 @@
      * Sets the default FileTypeMap for the system. This instance
      * will be returned to callers of getDefaultFileTypeMap.
      *
-     * @param map The FileTypeMap.
+     * @param fileTypeMap The FileTypeMap.
      * @exception SecurityException if the caller doesn't have permission
      *                                  to change the default
      */
-    public static void setDefaultFileTypeMap(FileTypeMap map) {
+    public static synchronized void setDefaultFileTypeMap(FileTypeMap fileTypeMap) {
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
             try {
@@ -90,14 +94,17 @@
                 security.checkSetFactory();
             } catch (SecurityException ex) {
                 // otherwise, we also allow it if this code and the
-                // factory come from the same class loader (e.g.,
+                // factory come from the same (non-system) class loader (e.g.,
                 // the JAF classes were loaded with the applet classes).
-                if (FileTypeMap.class.getClassLoader() !=
-                        map.getClass().getClassLoader())
+                if (FileTypeMap.class.getClassLoader() == null ||
+                    FileTypeMap.class.getClassLoader() !=
+                        fileTypeMap.getClass().getClassLoader())
                     throw ex;
             }
         }
-        defaultMap = map;
+        // remove any per-thread-context-class-loader FileTypeMap
+        map.remove(SecuritySupport.getContextClassLoader());
+        defaultMap = fileTypeMap;
     }
 
     /**
@@ -109,10 +116,17 @@
      * @return The default FileTypeMap
      * @see javax.activation.FileTypeMap#setDefaultFileTypeMap
      */
-    public static FileTypeMap getDefaultFileTypeMap() {
-        // XXX - probably should be synchronized
-        if (defaultMap == null)
-            defaultMap = new MimetypesFileTypeMap();
-        return defaultMap;
+    public static synchronized FileTypeMap getDefaultFileTypeMap() {
+        if (defaultMap != null)
+            return defaultMap;
+
+        // fetch per-thread-context-class-loader default
+        ClassLoader tccl = SecuritySupport.getContextClassLoader();
+        FileTypeMap def = map.get(tccl);
+        if (def == null) {
+            def = new MimetypesFileTypeMap();
+            map.put(tccl, def);
+        }
+        return def;
     }
 }
--- a/src/share/jaf_classes/javax/activation/MailcapCommandMap.java	Sat Nov 09 10:19:31 2013 +0100
+++ b/src/share/jaf_classes/javax/activation/MailcapCommandMap.java	Tue Nov 12 11:22:53 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, 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
@@ -120,11 +120,7 @@
 public class MailcapCommandMap extends CommandMap {
     /*
      * We manage a collection of databases, searched in order.
-     * The default database is shared between all instances
-     * of this class.
-     * XXX - Can we safely share more databases between instances?
      */
-    private static MailcapFile defDB = null;
     private MailcapFile[] DB;
     private static final int PROG = 0;  // programmatically added entries
 
@@ -164,14 +160,10 @@
         loadAllResources(dbv, "META-INF/mailcap");
 
         LogSupport.log("MailcapCommandMap: load DEF");
-        synchronized (MailcapCommandMap.class) {
-            // see if another instance has created this yet.
-            if (defDB == null)
-                defDB = loadResource("/META-INF/mailcap.default");
-        }
+        mf = loadResource("/META-INF/mailcap.default");
 
-        if (defDB != null)
-            dbv.add(defDB);
+        if (mf != null)
+            dbv.add(mf);
 
         DB = new MailcapFile[dbv.size()];
         DB = (MailcapFile[])dbv.toArray(DB);
--- a/src/share/jaf_classes/javax/activation/MimetypesFileTypeMap.java	Sat Nov 09 10:19:31 2013 +0100
+++ b/src/share/jaf_classes/javax/activation/MimetypesFileTypeMap.java	Tue Nov 12 11:22:53 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, 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
@@ -69,11 +69,7 @@
 public class MimetypesFileTypeMap extends FileTypeMap {
     /*
      * We manage a collection of databases, searched in order.
-     * The default database is shared between all instances
-     * of this class.
-     * XXX - Can we safely share more databases between instances?
      */
-    private static MimeTypeFile defDB = null;
     private MimeTypeFile[] DB;
     private static final int PROG = 0;  // programmatically added entries
 
@@ -114,14 +110,10 @@
         loadAllResources(dbv, "META-INF/mime.types");
 
         LogSupport.log("MimetypesFileTypeMap: load DEF");
-        synchronized (MimetypesFileTypeMap.class) {
-            // see if another instance has created this yet.
-            if (defDB == null)
-                defDB = loadResource("/META-INF/mimetypes.default");
-        }
+        mf = loadResource("/META-INF/mimetypes.default");
 
-        if (defDB != null)
-            dbv.addElement(defDB);
+        if (mf != null)
+            dbv.addElement(mf);
 
         DB = new MimeTypeFile[dbv.size()];
         dbv.copyInto(DB);