abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r532373 - /incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/ExtensionFactoryMap.java
Date Wed, 25 Apr 2007 14:46:18 GMT
Author: jmsnell
Date: Wed Apr 25 07:46:17 2007
New Revision: 532373

URL: http://svn.apache.org/viewvc?view=rev&rev=532373
Log:
Second attempt at improving thread safety on ExtensionFactoryMap.  Wrap the weakhashmap with
a synchronized map and synchronize the 
iterations over the factories list

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/ExtensionFactoryMap.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/ExtensionFactoryMap.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/ExtensionFactoryMap.java?view=diff&rev=532373&r1=532372&r2=532373
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/ExtensionFactoryMap.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/factory/ExtensionFactoryMap.java
Wed Apr 25 07:46:17 2007
@@ -18,6 +18,7 @@
 package org.apache.abdera.factory;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
@@ -34,8 +35,9 @@
   private final Map<Element,Element> wrappers;
   
   public ExtensionFactoryMap(List<ExtensionFactory> factories) {
-    this.factories = factories;
-    this.wrappers = new WeakHashMap<Element,Element>();
+    this.factories = factories;;
+    this.wrappers = Collections.synchronizedMap(
+      new WeakHashMap<Element,Element>());
   }
 
   @SuppressWarnings("unchecked")
@@ -43,11 +45,13 @@
     if (internal == null) return null;
     T t = (T)wrappers.get(internal);
     if (t == null) {
-      for (ExtensionFactory factory : factories) {
-        t = (T) factory.getElementWrapper(internal);
-        if (t != null && t != internal) {
-          setElementWrapper(internal,t);
-          return t;
+      synchronized(factories) {
+        for (ExtensionFactory factory : factories) {
+          t = (T) factory.getElementWrapper(internal);
+          if (t != null && t != internal) {
+            setElementWrapper(internal,t);
+            return t;
+          }
         }
       }
       t = (T) internal;
@@ -55,21 +59,25 @@
     return (t != null) ? t : (T)internal;
   }
   
-  public synchronized void setElementWrapper(Element internal, Element wrapper) {
+  public void setElementWrapper(Element internal, Element wrapper) {
     wrappers.put(internal, wrapper);
   }
 
   public List<String> getNamespaces() {
     List<String> ns = new ArrayList<String>();
-    for (ExtensionFactory factory : factories) {
-      ns.addAll(factory.getNamespaces());
+    synchronized(factories) {
+      for (ExtensionFactory factory : factories) {
+        ns.addAll(factory.getNamespaces());
+      }
     }
     return ns;
   }
 
   public boolean handlesNamespace(String namespace) {
-    for (ExtensionFactory factory : factories) {
-      if (factory.handlesNamespace(namespace)) return true;
+    synchronized(factories) {
+      for (ExtensionFactory factory : factories) {
+        if (factory.handlesNamespace(namespace)) return true;
+      }
     }
     return false;
   }



Mime
View raw message