Author: jacopoc
Date: Fri May 11 10:06:48 2012 New Revision: 1337097 URL: http://svn.apache.org/viewvc?rev=1337097&view=rev Log: Applied fix from trunk for revision: 1337046 === Based on review from Adam Heath, refactored/simplified the implementation of thread safe code using the parsedScripts object. 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:r1337046 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=1337097&r1=1337096&r2=1337097&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 Fri May 11 10:06:48 2012 @@ -103,26 +103,18 @@ public class GroovyUtil { public static Class<?> getScriptClassFromLocation(String location) 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) { throw new GeneralException("Script not found at location [" + location + "]"); } 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) { @@ -152,23 +144,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 |