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/13 10:07:12 UTC

svn commit: r1337802 - in /ofbiz/branches/release11.04: ./ framework/base/src/org/ofbiz/base/util/GroovyUtil.java

Author: jacopoc
Date: Sun May 13 08:07:12 2012
New Revision: 1337802

URL: http://svn.apache.org/viewvc?rev=1337802&view=rev
Log:
Applied fix from trunk for revision: 1337789 
===

Improved implementation of concurrent access to the cache of parsed Groovy scripts to avoid the risk of a NPE in the unlucky event that, between the execution of the cache.putIfAbsent and cache.get methods, the cache entry was removed (e.g. cache cleared/expired).


Modified:
    ofbiz/branches/release11.04/   (props changed)
    ofbiz/branches/release11.04/framework/base/src/org/ofbiz/base/util/GroovyUtil.java

Propchange: ofbiz/branches/release11.04/
------------------------------------------------------------------------------
  Merged /ofbiz/trunk:r1337789

Modified: ofbiz/branches/release11.04/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/release11.04/framework/base/src/org/ofbiz/base/util/GroovyUtil.java?rev=1337802&r1=1337801&r2=1337802&view=diff
==============================================================================
--- ofbiz/branches/release11.04/framework/base/src/org/ofbiz/base/util/GroovyUtil.java (original)
+++ ofbiz/branches/release11.04/framework/base/src/org/ofbiz/base/util/GroovyUtil.java Sun May 13 08:07:12 2012
@@ -102,19 +102,22 @@ public class GroovyUtil {
 
     public static Class<?> getScriptClassFromLocation(String location) throws GeneralException {
         try {
-            Class<?> scriptClass = null;
-            scriptClass = parsedScripts.get(location);
+            Class<?> scriptClass = parsedScripts.get(location);
             if (scriptClass == null) {
                 URL scriptUrl = FlexibleLocation.resolveLocation(location);
                 if (scriptUrl == null) {
                     throw new GeneralException("Script not found at location [" + location + "]");
                 }
                 scriptClass = parseClass(scriptUrl.openStream(), location);
-                scriptClass = parsedScripts.putIfAbsent(location, scriptClass);
-                if (scriptClass == null && Debug.verboseOn()) { // putIfAbsent returns null if the class is added
-                    Debug.logVerbose("Cached Groovy script at: " + location, module);
+                Class<?> scriptClassCached = parsedScripts.putIfAbsent(location, scriptClass);
+                if (scriptClassCached == null) { // putIfAbsent returns null if the class is added to the cache
+                    if (Debug.verboseOn()) {
+                        Debug.logVerbose("Cached Groovy script at: " + location, module);
+                    }
+                } else {
+                    // the newly parsed script is discarded and the one found in the cache (that has been created by a concurrent thread in the meantime) is used
+                    scriptClass = scriptClassCached;
                 }
-                scriptClass = parsedScripts.get(location);
             }
             return scriptClass;
         } catch (Exception e) {
@@ -147,11 +150,15 @@ public class GroovyUtil {
             Class<?> scriptClass = parsedScripts.get(script);
             if (scriptClass == null) {
                 scriptClass = loadClass(script);
-                scriptClass = parsedScripts.putIfAbsent(script, scriptClass);
-                if (scriptClass == null && Debug.verboseOn()) { // putIfAbsent returns null if the class is added
-                    Debug.logVerbose("Cached Groovy script at: " + script, module);
+                Class<?> cachedScriptClass = parsedScripts.putIfAbsent(script, scriptClass);
+                if (cachedScriptClass == null) { // putIfAbsent returns null if the class is added
+                    if (Debug.verboseOn()) {
+                        Debug.logVerbose("Cached Groovy script at: " + script, module);
+                    }
+                } else {
+                    // the newly parsed script is discarded and the one found in the cache (that has been created by a concurrent thread in the meantime) is used
+                    scriptClass = cachedScriptClass;
                 }
-                scriptClass = parsedScripts.get(script);
             }
             return InvokerHelper.createScript(scriptClass, getBinding(context)).run();
         } catch (CompilationFailedException e) {