You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2012/05/29 17:40:17 UTC

svn commit: r1343785 - /ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java

Author: jacopoc
Date: Tue May 29 15:40:17 2012
New Revision: 1343785

URL: http://svn.apache.org/viewvc?rev=1343785&view=rev
Log:
Improved code that manages the cache:
* protected the UtilCache object (static field) by making it private and final
* removed unnecessary synchronization


Modified:
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java?rev=1343785&r1=1343784&r2=1343785&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java Tue May 29 15:40:17 2012
@@ -38,8 +38,8 @@ import org.w3c.dom.Element;
  */
 public class SimpleMapProcessor {
 
-    protected static UtilCache<String, Map<String, MapProcessor>> simpleMapProcessorsResourceCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsResource", 0, 0);
-    protected static UtilCache<URL, Map<String, MapProcessor>> simpleMapProcessorsURLCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsURL", 0, 0);
+    private static final UtilCache<String, Map<String, MapProcessor>> simpleMapProcessorsResourceCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsResource", 0, 0);
+    private static final UtilCache<URL, Map<String, MapProcessor>> simpleMapProcessorsURLCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsURL", 0, 0);
 
     protected static Map<String, MapProcessor> getAllProcessors(URL xmlURL) throws MiniLangException {
         Map<String, MapProcessor> mapProcessors = FastMap.newInstance();
@@ -68,22 +68,16 @@ public class SimpleMapProcessor {
     protected static Map<String, MapProcessor> getProcessors(String xmlResource, String name, ClassLoader loader) throws MiniLangException {
         Map<String, MapProcessor> simpleMapProcessors = simpleMapProcessorsResourceCache.get(xmlResource);
         if (simpleMapProcessors == null) {
-            synchronized (SimpleMapProcessor.class) {
-                simpleMapProcessors = simpleMapProcessorsResourceCache.get(xmlResource);
-                if (simpleMapProcessors == null) {
-                    URL xmlURL = null;
-                    try {
-                        xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader);
-                    } catch (MalformedURLException e) {
-                        throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource + "; error was: " + e.toString(), e);
-                    }
-                    if (xmlURL == null) {
-                        throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource);
-                    }
-                    simpleMapProcessors = getAllProcessors(xmlURL);
-                    simpleMapProcessorsResourceCache.put(xmlResource, simpleMapProcessors);
-                }
+            URL xmlURL = null;
+            try {
+                xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader);
+            } catch (MalformedURLException e) {
+                throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource + "; error was: " + e.toString(), e);
             }
+            if (xmlURL == null) {
+                throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource);
+            }
+            simpleMapProcessors = simpleMapProcessorsResourceCache.putIfAbsentAndGet(xmlResource, getAllProcessors(xmlURL));
         }
         return simpleMapProcessors;
     }
@@ -91,13 +85,7 @@ public class SimpleMapProcessor {
     protected static Map<String, MapProcessor> getProcessors(URL xmlURL, String name) throws MiniLangException {
         Map<String, MapProcessor> simpleMapProcessors = simpleMapProcessorsURLCache.get(xmlURL);
         if (simpleMapProcessors == null) {
-            synchronized (SimpleMapProcessor.class) {
-                simpleMapProcessors = simpleMapProcessorsURLCache.get(xmlURL);
-                if (simpleMapProcessors == null) {
-                    simpleMapProcessors = getAllProcessors(xmlURL);
-                    simpleMapProcessorsURLCache.put(xmlURL, simpleMapProcessors);
-                }
-            }
+            simpleMapProcessors = simpleMapProcessorsURLCache.putIfAbsentAndGet(xmlURL, getAllProcessors(xmlURL));
         }
         return simpleMapProcessors;
     }