geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject svn commit: r158932 - geronimo/trunk/specs/activation/src/java/javax/activation/DataHandler.java
Date Thu, 24 Mar 2005 18:20:12 GMT
Author: jboynes
Date: Thu Mar 24 10:20:12 2005
New Revision: 158932

URL: http://svn.apache.org/viewcvs?view=rev&rev=158932
Log:
track default factory to detect changes

Modified:
    geronimo/trunk/specs/activation/src/java/javax/activation/DataHandler.java

Modified: geronimo/trunk/specs/activation/src/java/javax/activation/DataHandler.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/specs/activation/src/java/javax/activation/DataHandler.java?view=diff&r1=158931&r2=158932
==============================================================================
--- geronimo/trunk/specs/activation/src/java/javax/activation/DataHandler.java (original)
+++ geronimo/trunk/specs/activation/src/java/javax/activation/DataHandler.java Thu Mar 24
10:20:12 2005
@@ -209,11 +209,9 @@
         }
     }
 
-    public void setCommandMap(CommandMap commandMap) {
-        synchronized(this) {
-            this.commandMap = commandMap;
-            this.dch = null;
-        }
+    public synchronized void setCommandMap(CommandMap commandMap) {
+        this.commandMap = commandMap;
+        this.dch = null;
     }
 
     private synchronized CommandMap getCommandMap() {
@@ -222,23 +220,36 @@
 
     /**
      * Search for a DataContentHandler for our mime type.
-     * If a commandMap has been set, then use it to create the handler.
-     * Otherwise, if a global factory has been set, use it.
-     * If neither has been set, use the default CommandMap from CommandMap.getDefaultCommandMap()
to create it.
+     * The search is performed by first checking if a global factory has been set using
+     * {@link #setDataContentHandlerFactory(DataContentHandlerFactory)};
+     * if found then it is called to attempt to create a handler.
+     * If this attempt fails, we then call the command map set using {@link #setCommandMap(CommandMap)}
+     * (or if that has not been set, the default map returned by {@link CommandMap#getDefaultCommandMap()})
+     * to create the handler.
+     *
+     * The resulting handler is cached until the global factory is changed.
+     *
      * @return
      */
     private synchronized DataContentHandler getDataContentHandler() {
+        DataContentHandlerFactory localFactory;
+        synchronized (DataHandler.class) {
+            if (factory != originalFactory) {
+                // setDCHF was called - clear our cached copy of the DCH and DCHF
+                dch = null;
+                originalFactory = factory;
+            }
+            localFactory = originalFactory;
+        }
         if (dch == null) {
             String contentType = ds.getContentType();
-            if (commandMap != null) {
-                dch = commandMap.createDataContentHandler(contentType);
-            } else {
-                synchronized(DataHandler.class) {
-                    if (factory != null) {
-                        dch = factory.createDataContentHandler(contentType);
-                    }
-                }
-                if (dch == null) {
+            if (localFactory != null) {
+                dch = localFactory.createDataContentHandler(contentType);
+            }
+            if (dch == null) {
+                if (commandMap != null) {
+                    dch = commandMap.createDataContentHandler(contentType);
+                } else {
                     dch = CommandMap.getDefaultCommandMap().createDataContentHandler(contentType);
                 }
             }
@@ -246,17 +257,30 @@
         return dch;
     }
 
+    /**
+     * This is used to check if the DataContentHandlerFactory has been changed.
+     * This is not specified behaviour but this check is required to make this work like
the RI.
+     */
+    private DataContentHandlerFactory originalFactory;
+
+    {
+        synchronized (DataHandler.class) {
+            originalFactory = factory;
+        }
+    }
+
     private static DataContentHandlerFactory factory;
 
     /**
      * Set the DataContentHandlerFactory to use.
      * If this method has already been called then an Error is raised.
+     *
      * @param newFactory the new factory
      * @throws SecurityException if the caller does not have "SetFactory" RuntimePermission
      */
     public static synchronized void setDataContentHandlerFactory(DataContentHandlerFactory
newFactory) {
         if (factory != null) {
-            throw new Error("javax.activation.DataHandler.setDataContentHandlerFactory has
already been called");
+            throw new Error("javax.activation.DataHandler.setDataContentHandlerFactory has
already been defined");
         }
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {



Mime
View raw message