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 12:54:16 UTC

svn commit: r1343662 - in /ofbiz/trunk/framework: entity/src/org/ofbiz/entity/model/ModelGroupReader.java entity/src/org/ofbiz/entity/model/ModelReader.java webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java

Author: jacopoc
Date: Tue May 29 10:54:16 2012
New Revision: 1343662

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


Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelGroupReader.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelGroupReader.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelGroupReader.java?rev=1343662&r1=1343661&r2=1343662&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelGroupReader.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelGroupReader.java Tue May 29 10:54:16 2012
@@ -53,7 +53,7 @@ import org.w3c.dom.Node;
 public class ModelGroupReader implements Serializable {
 
     public static final String module = ModelGroupReader.class.getName();
-    public static UtilCache<String, ModelGroupReader> readers = UtilCache.createUtilCache("entity.ModelGroupReader", 0, 0);
+    private static final UtilCache<String, ModelGroupReader> readers = UtilCache.createUtilCache("entity.ModelGroupReader", 0, 0);
 
     private Map<String, String> groupCache = null;
     private Set<String> groupNames = null;
@@ -71,15 +71,8 @@ public class ModelGroupReader implements
         String tempModelName = delegatorInfo.entityGroupReader;
         ModelGroupReader reader = readers.get(tempModelName);
 
-        if (reader == null) { // don't want to block here
-            synchronized (ModelGroupReader.class) {
-                // must check if null again as one of the blocked threads can still enter
-                reader = readers.get(tempModelName);
-                if (reader == null) {
-                    reader = new ModelGroupReader(tempModelName);
-                    readers.put(tempModelName, reader);
-                }
-            }
+        if (reader == null) {
+            reader = readers.putIfAbsentAndGet(tempModelName, new ModelGroupReader(tempModelName));
         }
         return reader;
     }

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java?rev=1343662&r1=1343661&r2=1343662&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java Tue May 29 10:54:16 2012
@@ -58,7 +58,7 @@ import org.w3c.dom.Node;
 public class ModelReader implements Serializable {
 
     public static final String module = ModelReader.class.getName();
-    public static UtilCache<String, ModelReader> readers = UtilCache.createUtilCache("entity.ModelReader", 0, 0);
+    private static final UtilCache<String, ModelReader> readers = UtilCache.createUtilCache("entity.ModelReader", 0, 0);
 
     protected Map<String, ModelEntity> entityCache = null;
 
@@ -89,17 +89,11 @@ public class ModelReader implements Seri
         String tempModelName = delegatorInfo.entityModelReader;
         ModelReader reader = readers.get(tempModelName);
 
-        if (reader == null) { // don't want to block here
-            synchronized (ModelReader.class) {
-                // must check if null again as one of the blocked threads can still enter
-                reader = readers.get(tempModelName);
-                if (reader == null) {
-                    reader = new ModelReader(tempModelName);
-                    // preload caches...
-                    reader.getEntityCache();
-                    readers.put(tempModelName, reader);
-                }
-            }
+        if (reader == null) {
+            reader = new ModelReader(tempModelName);
+            // preload caches...
+            reader.getEntityCache();
+            reader = readers.putIfAbsentAndGet(tempModelName, reader);
         }
         return reader;
     }

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java?rev=1343662&r1=1343661&r2=1343662&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ConfigXMLReader.java Tue May 29 10:54:16 2012
@@ -52,8 +52,8 @@ public class ConfigXMLReader {
     public static final String module = ConfigXMLReader.class.getName();
     public static final String controllerXmlFileName = "/WEB-INF/controller.xml";
 
-    public static UtilCache<URL, ControllerConfig> controllerCache = UtilCache.createUtilCache("webapp.ControllerConfig");
-    public static UtilCache<String, List<ControllerConfig>> controllerSearchResultsCache = UtilCache.createUtilCache("webapp.ControllerSearchResults");
+    private static final UtilCache<URL, ControllerConfig> controllerCache = UtilCache.createUtilCache("webapp.ControllerConfig");
+    private static final UtilCache<String, List<ControllerConfig>> controllerSearchResultsCache = UtilCache.createUtilCache("webapp.ControllerSearchResults");
 
     public static URL getControllerConfigURL(ServletContext context) {
         try {
@@ -66,15 +66,8 @@ public class ConfigXMLReader {
 
     public static ControllerConfig getControllerConfig(URL url) {
         ControllerConfig controllerConfig = controllerCache.get(url);
-        if (controllerConfig == null) { // don't want to block here
-            synchronized (ConfigXMLReader.class) {
-                // must check if null again as one of the blocked threads can still enter
-                controllerConfig = controllerCache.get(url);
-                if (controllerConfig == null) {
-                    controllerConfig = new ControllerConfig(url);
-                    controllerCache.put(url, controllerConfig);
-                }
-            }
+        if (controllerConfig == null) {
+            controllerConfig = controllerCache.putIfAbsentAndGet(url, new ControllerConfig(url));
         }
         return controllerConfig;
     }
@@ -447,7 +440,7 @@ public class ConfigXMLReader {
                     controllerConfigs.add(cc);
                 }
 
-                controllerSearchResultsCache.put(cacheId, controllerConfigs);
+                controllerConfigs = controllerSearchResultsCache.putIfAbsentAndGet(cacheId, controllerConfigs);
             } catch (IOException e) {
                 throw new GeneralException("Error finding controller XML files to lookup request references: " + e.toString(), e);
             }