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 16:01:46 UTC

svn commit: r1343729 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/config/ResourceLoader.java

Author: jacopoc
Date: Tue May 29 14:01:45 2012
New Revision: 1343729

URL: http://svn.apache.org/viewvc?rev=1343729&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/base/src/org/ofbiz/base/config/ResourceLoader.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/config/ResourceLoader.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/config/ResourceLoader.java?rev=1343729&r1=1343728&r2=1343729&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/config/ResourceLoader.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/config/ResourceLoader.java Tue May 29 14:01:45 2012
@@ -35,7 +35,7 @@ import org.w3c.dom.Element;
 public abstract class ResourceLoader {
 
     public static final String module = ResourceLoader.class.getName();
-    protected static UtilCache<String, Object> loaderCache = UtilCache.createUtilCache("resource.ResourceLoaders", 0, 0);
+    private static final UtilCache<String, Object> loaderCache = UtilCache.createUtilCache("resource.ResourceLoaders", 0, 0);
 
     protected String name;
     protected String prefix;
@@ -61,19 +61,14 @@ public abstract class ResourceLoader {
         ResourceLoader loader = (ResourceLoader) loaderCache.get(xmlFilename + "::" + loaderName);
 
         if (loader == null) {
-            synchronized (ResourceLoader.class) {
-                loader = (ResourceLoader) loaderCache.get(xmlFilename + "::" + loaderName);
-                if (loader == null) {
-                    Element rootElement = getXmlRootElement(xmlFilename);
-
-                    Element loaderElement = UtilXml.firstChildElement(rootElement, "resource-loader", "name", loaderName);
-
-                    loader = makeLoader(loaderElement);
-
-                    if (loader != null) {
-                        loaderCache.put(xmlFilename + "::" + loaderName, loader);
-                    }
-                }
+            Element rootElement = getXmlRootElement(xmlFilename);
+
+            Element loaderElement = UtilXml.firstChildElement(rootElement, "resource-loader", "name", loaderName);
+
+            loader = makeLoader(loaderElement);
+
+            if (loader != null) {
+                loader = (ResourceLoader) loaderCache.putIfAbsentAndGet(xmlFilename + "::" + loaderName, loader);
             }
         }
 
@@ -98,29 +93,24 @@ public abstract class ResourceLoader {
         Document document = (Document) loaderCache.get(xmlFilename);
 
         if (document == null) {
-            synchronized (ResourceLoader.class) {
-                document = (Document) loaderCache.get(xmlFilename);
-                if (document == null) {
-                    URL confUrl = UtilURL.fromResource(xmlFilename);
-
-                    if (confUrl == null) {
-                        throw new GenericConfigException("ERROR: could not find the [" + xmlFilename + "] XML file on the classpath");
-                    }
-
-                    try {
-                        document = UtilXml.readXmlDocument(confUrl);
-                    } catch (org.xml.sax.SAXException e) {
-                        throw new GenericConfigException("Error reading " + xmlFilename + "", e);
-                    } catch (javax.xml.parsers.ParserConfigurationException e) {
-                        throw new GenericConfigException("Error reading " + xmlFilename + "", e);
-                    } catch (java.io.IOException e) {
-                        throw new GenericConfigException("Error reading " + xmlFilename + "", e);
-                    }
-
-                    if (document != null) {
-                        loaderCache.put(xmlFilename, document);
-                    }
-                }
+            URL confUrl = UtilURL.fromResource(xmlFilename);
+
+            if (confUrl == null) {
+                throw new GenericConfigException("ERROR: could not find the [" + xmlFilename + "] XML file on the classpath");
+            }
+
+            try {
+                document = UtilXml.readXmlDocument(confUrl);
+            } catch (org.xml.sax.SAXException e) {
+                throw new GenericConfigException("Error reading " + xmlFilename + "", e);
+            } catch (javax.xml.parsers.ParserConfigurationException e) {
+                throw new GenericConfigException("Error reading " + xmlFilename + "", e);
+            } catch (java.io.IOException e) {
+                throw new GenericConfigException("Error reading " + xmlFilename + "", e);
+            }
+
+            if (document != null) {
+                document = (Document) loaderCache.putIfAbsentAndGet(xmlFilename, document);
             }
         }
         return document;