svn commit: r1343692 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/ common/src/org/ofbiz/common/ entityext/src/org/ofbiz/entityext/eca/ minilang/src/org/ofbiz/minilang/ service/src/org/ofbiz/service/engine/ webtools/src/org/ofbiz/webtools/a...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1343692 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/ common/src/org/ofbiz/common/ entityext/src/org/ofbiz/entityext/eca/ minilang/src/org/ofbiz/minilang/ service/src/org/ofbiz/service/engine/ webtools/src/org/ofbiz/webtools/a...

jacopoc
Author: jacopoc
Date: Tue May 29 12:10:54 2012
New Revision: 1343692

URL: http://svn.apache.org/viewvc?rev=1343692&view=rev
Log:
Improved code that manages the cache:
* removed unnecessary synchronization
* protected the UtilCache object (static field) by making it private and final
* replaced non atomic containsKey+put+get with get + putIfAbsentAndGet
* replaced containsKey + null check + get with get + null check


Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/BshUtil.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/OfbizBshBsfEngine.java
    ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java
    ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/eca/EntityEcaUtil.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BSFEngine.java
    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/BshUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/BshUtil.java?rev=1343692&r1=1343691&r2=1343692&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/BshUtil.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/BshUtil.java Tue May 29 12:10:54 2012
@@ -46,7 +46,7 @@ public final class BshUtil {
     public static final String module = BshUtil.class.getName();
 
     protected static FastMap<ClassLoader, BshClassManager> masterClassManagers = FastMap.newInstance();
-    public static UtilCache<String, Interpreter.ParsedScript> parsedScripts = UtilCache.createUtilCache("script.BshLocationParsedCache", 0, 0, false);
+    private static final UtilCache<String, Interpreter.ParsedScript> parsedScripts = UtilCache.createUtilCache("script.BshLocationParsedCache", 0, 0, false);
 
     /**
      * Evaluate a BSH condition or expression
@@ -134,19 +134,14 @@ public final class BshUtil {
             Interpreter.ParsedScript script = null;
             script = parsedScripts.get(location);
             if (script == null) {
-                synchronized (OfbizBshBsfEngine.class) {
-                    script = parsedScripts.get(location);
-                    if (script == null) {
-                        URL scriptUrl = FlexibleLocation.resolveLocation(location);
-                        if (scriptUrl == null) {
-                            throw new GeneralException("Could not find bsh script at [" + location + "]");
-                        }
-                        Reader scriptReader = new InputStreamReader(scriptUrl.openStream());
-                        script = interpreter.parseScript(location, scriptReader);
-                        if (Debug.verboseOn()) Debug.logVerbose("Caching BSH script at: " + location, module);
-                        parsedScripts.put(location, script);
-                    }
+                URL scriptUrl = FlexibleLocation.resolveLocation(location);
+                if (scriptUrl == null) {
+                    throw new GeneralException("Could not find bsh script at [" + location + "]");
                 }
+                Reader scriptReader = new InputStreamReader(scriptUrl.openStream());
+                script = interpreter.parseScript(location, scriptReader);
+                if (Debug.verboseOn()) Debug.logVerbose("Caching BSH script at: " + location, module);
+                script = parsedScripts.putIfAbsentAndGet(location, script);
             }
 
             return interpreter.evalParsedScript(script);

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/OfbizBshBsfEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/OfbizBshBsfEngine.java?rev=1343692&r1=1343691&r2=1343692&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/OfbizBshBsfEngine.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/OfbizBshBsfEngine.java Tue May 29 12:10:54 2012
@@ -61,7 +61,7 @@ public class OfbizBshBsfEngine extends B
     protected Interpreter interpreter;
     protected boolean installedApplyMethod;
 
-    public static UtilCache<String, Interpreter.ParsedScript> parsedScripts = UtilCache.createUtilCache("script.BshBsfParsedCache", 0, 0, false);
+    private static final UtilCache<String, Interpreter.ParsedScript> parsedScripts = UtilCache.createUtilCache("script.BshBsfParsedCache", 0, 0, false);
 
     @SuppressWarnings("unchecked")
     @Override
@@ -177,14 +177,9 @@ public class OfbizBshBsfEngine extends B
             if (UtilValidate.isNotEmpty(source)) {
                 script = parsedScripts.get(source);
                 if (script == null) {
-                    synchronized (OfbizBshBsfEngine.class) {
-                        script = parsedScripts.get(source);
-                        if (script == null) {
-                            script = interpreter.parseScript(source, new StringReader((String) expr));
-                            Debug.logVerbose("Caching BSH script at: " + source, module);
-                            parsedScripts.put(source, script);
-                        }
-                    }
+                    script = interpreter.parseScript(source, new StringReader((String) expr));
+                    Debug.logVerbose("Caching BSH script at: " + source, module);
+                    script = parsedScripts.putIfAbsentAndGet(source, script);
                 }
             } else {
                 script = interpreter.parseScript(source, new StringReader((String) expr));

Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java?rev=1343692&r1=1343691&r2=1343692&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java (original)
+++ ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java Tue May 29 12:10:54 2012
@@ -68,7 +68,7 @@ public class CommonEvents {
 
     public static final String module = CommonEvents.class.getName();
 
-    public static UtilCache<String, Map<String, String>> appletSessions = UtilCache.createUtilCache("AppletSessions", 0, 600000, true);
+    private static final UtilCache<String, Map<String, String>> appletSessions = UtilCache.createUtilCache("AppletSessions", 0, 600000, true);
 
     public static String checkAppletRequest(HttpServletRequest request, HttpServletResponse response) {
         Delegator delegator = (Delegator) request.getAttribute("delegator");
@@ -122,8 +122,8 @@ public class CommonEvents {
 
         if (visit.getString("sessionId").equals(sessionId)) {
             String currentPage = request.getParameter("currentPage");
-            if (appletSessions.containsKey(sessionId)) {
-                Map<String, String> sessionMap = appletSessions.get(sessionId);
+            Map<String, String> sessionMap = appletSessions.get(sessionId);
+            if (sessionMap != null) {
                 String followers = sessionMap.get("followers");
                 List<String> folList = StringUtil.split(followers, ",");
                 for (String follower: folList) {

Modified: ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/eca/EntityEcaUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/eca/EntityEcaUtil.java?rev=1343692&r1=1343691&r2=1343692&view=diff
==============================================================================
--- ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/eca/EntityEcaUtil.java (original)
+++ ofbiz/trunk/framework/entityext/src/org/ofbiz/entityext/eca/EntityEcaUtil.java Tue May 29 12:10:54 2012
@@ -49,19 +49,14 @@ public class EntityEcaUtil {
 
     public static final String module = EntityEcaUtil.class.getName();
 
-    public static UtilCache<String, Map<String, Map<String, List<EntityEcaRule>>>> entityEcaReaders = UtilCache.createUtilCache("entity.EcaReaders", 0, 0, false);
+    private static final UtilCache<String, Map<String, Map<String, List<EntityEcaRule>>>> entityEcaReaders = UtilCache.createUtilCache("entity.EcaReaders", 0, 0, false);
 
     public static Map<String, Map<String, List<EntityEcaRule>>> getEntityEcaCache(String entityEcaReaderName) {
         Map<String, Map<String, List<EntityEcaRule>>> ecaCache = entityEcaReaders.get(entityEcaReaderName);
         if (ecaCache == null) {
-            synchronized (EntityEcaUtil.class) {
-                ecaCache = entityEcaReaders.get(entityEcaReaderName);
-                if (ecaCache == null) {
-                    ecaCache = FastMap.newInstance();
-                    readConfig(entityEcaReaderName, ecaCache);
-                    entityEcaReaders.put(entityEcaReaderName, ecaCache);
-                }
-            }
+            ecaCache = FastMap.newInstance();
+            readConfig(entityEcaReaderName, ecaCache);
+            ecaCache = entityEcaReaders.putIfAbsentAndGet(entityEcaReaderName, ecaCache);
         }
         return ecaCache;
     }

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java?rev=1343692&r1=1343691&r2=1343692&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java Tue May 29 12:10:54 2012
@@ -154,8 +154,7 @@ public final class SimpleMethod extends
         Map<String, SimpleMethod> simpleMethods = simpleMethodsDirectCache.get(name);
         if (simpleMethods == null) {
             simpleMethods = getAllDirectSimpleMethods(name, content, fromLocation);
-            simpleMethodsDirectCache.putIfAbsent(name, simpleMethods);
-            simpleMethods = simpleMethodsDirectCache.get(name);
+            simpleMethods = simpleMethodsDirectCache.putIfAbsentAndGet(name, simpleMethods);
         }
         return simpleMethods;
     }
@@ -192,8 +191,7 @@ public final class SimpleMethod extends
         Map<String, SimpleMethod> simpleMethods = simpleMethodsResourceCache.get(cacheKey);
         if (simpleMethods == null) {
             simpleMethods = getAllSimpleMethods(xmlURL);
-            simpleMethodsResourceCache.putIfAbsent(cacheKey, simpleMethods);
-            simpleMethods = simpleMethodsResourceCache.get(cacheKey);
+            simpleMethods = simpleMethodsResourceCache.putIfAbsentAndGet(cacheKey, simpleMethods);
         }
         return simpleMethods;
     }

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BSFEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BSFEngine.java?rev=1343692&r1=1343691&r2=1343692&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BSFEngine.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/BSFEngine.java Tue May 29 12:10:54 2012
@@ -40,7 +40,7 @@ import org.apache.bsf.BSFManager;
 public class BSFEngine extends GenericAsyncEngine {
 
     public static final String module = BSFEngine.class.getName();
-    public static UtilCache<String, String> scriptCache = UtilCache.createUtilCache("BSFScripts", 0, 0);
+    private static final UtilCache<String, String> scriptCache = UtilCache.createUtilCache("BSFScripts", 0, 0);
 
     public BSFEngine(ServiceDispatcher dispatcher) {
         super(dispatcher);
@@ -104,27 +104,22 @@ public class BSFEngine extends GenericAs
         String script = scriptCache.get(localName + "_" + location);
 
         if (script == null) {
-            synchronized (this) {
-                script = scriptCache.get(localName + "_" + location);
-                if (script == null) {
-                    URL scriptUrl = UtilURL.fromResource(location, cl);
-
-                    if (scriptUrl != null) {
-                        try {
-                            HttpClient http = new HttpClient(scriptUrl);
-                            script = http.get();
-                        } catch (HttpClientException e) {
-                            throw new GenericServiceException("Cannot read script from resource", e);
-                        }
-                    } else {
-                        throw new GenericServiceException("Cannot read script, resource [" + location + "] not found");
-                    }
-                    if (script == null || script.length() < 2) {
-                        throw new GenericServiceException("Null or empty script");
-                    }
-                    scriptCache.put(localName + "_" + location, script);
+            URL scriptUrl = UtilURL.fromResource(location, cl);
+
+            if (scriptUrl != null) {
+                try {
+                    HttpClient http = new HttpClient(scriptUrl);
+                    script = http.get();
+                } catch (HttpClientException e) {
+                    throw new GenericServiceException("Cannot read script from resource", e);
                 }
+            } else {
+                throw new GenericServiceException("Cannot read script, resource [" + location + "] not found");
+            }
+            if (script == null || script.length() < 2) {
+                throw new GenericServiceException("Null or empty script");
             }
+            script = scriptCache.putIfAbsentAndGet(localName + "_" + location, script);
         }
 
         // now invoke the script

Modified: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java?rev=1343692&r1=1343691&r2=1343692&view=diff
==============================================================================
--- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java (original)
+++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java Tue May 29 12:10:54 2012
@@ -63,7 +63,7 @@ public class ArtifactInfoFactory {
 
     public static final String module = ArtifactInfoFactory.class.getName();
 
-    protected static UtilCache<String, ArtifactInfoFactory> artifactInfoFactoryCache = UtilCache.createUtilCache("ArtifactInfoFactory");
+    private static final UtilCache<String, ArtifactInfoFactory> artifactInfoFactoryCache = UtilCache.createUtilCache("ArtifactInfoFactory");
 
     public static final String EntityInfoTypeId = "entity";
     public static final String ServiceInfoTypeId = "service";
@@ -121,8 +121,7 @@ public class ArtifactInfoFactory {
 
         ArtifactInfoFactory aif = artifactInfoFactoryCache.get(delegatorName);
         if (aif == null) {
-            aif = new ArtifactInfoFactory(delegatorName);
-            artifactInfoFactoryCache.put(delegatorName, aif);
+            aif = artifactInfoFactoryCache.putIfAbsentAndGet(delegatorName, new ArtifactInfoFactory(delegatorName));
         }
         return aif;
     }