Author: jacopoc
Date: Fri May 11 07:50:43 2012 New Revision: 1337046 URL: http://svn.apache.org/viewvc?rev=1337046&view=rev Log: Based on review from Adam Heath, refactored/simplified the implementation of thread safe code using the parsedScripts object. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java?rev=1337046&r1=1337045&r2=1337046&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java Fri May 11 07:50:43 2012 @@ -117,9 +117,7 @@ public class GroovyUtil { public static Class<?> getScriptClassFromLocation(String location, GroovyClassLoader groovyClassLoader) throws GeneralException { try { Class<?> scriptClass = null; - synchronized (parsedScripts) { - scriptClass = parsedScripts.get(location); - } + scriptClass = parsedScripts.get(location); if (scriptClass == null) { URL scriptUrl = FlexibleLocation.resolveLocation(location); if (scriptUrl == null) { @@ -130,17 +128,11 @@ public class GroovyUtil { } else { scriptClass = parseClass(scriptUrl.openStream(), location); } - synchronized (parsedScripts) { - Class<?> scriptClassCached = parsedScripts.get(location); - if (scriptClassCached == null) { - if (Debug.verboseOn()) { - Debug.logVerbose("Caching Groovy script at: " + location, module); - } - parsedScripts.put(location, scriptClass); - } else { - scriptClass = scriptClassCached; - } + 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); } + scriptClass = parsedScripts.get(location); } return scriptClass; } catch (Exception e) { @@ -184,23 +176,14 @@ public class GroovyUtil { public static Object runScriptFromClasspath(String script, Map<String,Object> context) throws GeneralException { try { - Class<?> scriptClass = null; - synchronized (parsedScripts) { - parsedScripts.get(script); - } + Class<?> scriptClass = parsedScripts.get(script); if (scriptClass == null) { scriptClass = loadClass(script); - synchronized (parsedScripts) { - Class<?> scriptClassCached = parsedScripts.get(script); - if (scriptClassCached == null) { - if (Debug.verboseOn()) { - Debug.logVerbose("Caching Groovy script: " + script, module); - } - parsedScripts.put(script, scriptClass); - } else { - scriptClass = scriptClassCached; - } + 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); } + scriptClass = parsedScripts.get(script); } return InvokerHelper.createScript(scriptClass, getBinding(context)).run(); } catch (CompilationFailedException e) { |
Free forum by Nabble | Edit this page |