Author: jacopoc
Date: Tue Apr 17 08:44:43 2012 New Revision: 1326996 URL: http://svn.apache.org/viewvc?rev=1326996&view=rev Log: Enhanced GroovyUtil to support the execution of data preparation methods defined in a Groovy script file (similarly to what is already supported by the ScriptUtil class for JSR223). Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java ofbiz/trunk/framework/security/src/org/ofbiz/security/authz/da/GroovyDaHandler.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=1326996&r1=1326995&r2=1326996&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 Tue Apr 17 08:44:43 2012 @@ -25,6 +25,7 @@ import java.util.Map; import groovy.lang.Binding; import groovy.lang.GroovyClassLoader; +import groovy.lang.Script; import groovy.lang.GroovyShell; import javolution.util.FastMap; @@ -99,10 +100,12 @@ public class GroovyUtil { if (context != null) { vars.putAll(context); vars.put("context", context); - ScriptContext scriptContext = ScriptUtil.createScriptContext(context); - ScriptHelper scriptHelper = (ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY); - if (scriptHelper != null) { - vars.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper); + if (vars.get(ScriptUtil.SCRIPT_HELPER_KEY) == null) { + ScriptContext scriptContext = ScriptUtil.createScriptContext(context); + ScriptHelper scriptHelper = (ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY); + if (scriptHelper != null) { + vars.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper); + } } } return new Binding(vars); @@ -154,12 +157,19 @@ public class GroovyUtil { return new GroovyClassLoader().parseClass(text, location); } - public static Object runScriptAtLocation(String location, Map<String, Object> context) throws GeneralException { - return runScriptAtLocation(location, context, null); + public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context) throws GeneralException { + return runScriptAtLocation(location, methodName, context, null); } - public static Object runScriptAtLocation(String location, Map<String, Object> context, GroovyClassLoader groovyClassLoader) throws GeneralException { - return InvokerHelper.createScript(getScriptClassFromLocation(location, groovyClassLoader), getBinding(context)).run(); + public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context, GroovyClassLoader groovyClassLoader) throws GeneralException { + Script script = InvokerHelper.createScript(getScriptClassFromLocation(location, groovyClassLoader), getBinding(context)); + Object result = null; + if (UtilValidate.isEmpty(methodName)) { + result = script.run(); + } else { + result = script.invokeMethod(methodName, new Object[] { context }); + } + return result; } public static Object runScriptFromClasspath(String script, Map<String,Object> context) throws GeneralException { Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java?rev=1326996&r1=1326995&r2=1326996&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java Tue Apr 17 08:44:43 2012 @@ -336,6 +336,11 @@ public final class ScriptUtil { */ public static Object executeScript(String filePath, String functionName, Map<String, Object> context, Object[] args) { try { + /* Enable this to run Groovy data preparation scripts using GroovyUtil rather than the generic JSR223 that doesn't support debug mode + if (filePath.endsWith(".groovy")) { + return GroovyUtil.runScriptAtLocation(filePath, functionName, context); + } + */ return executeScript(filePath, functionName, createScriptContext(context), args); } catch (Exception e) { String errMsg = "Error running script at location [" + filePath + "]: " + e.toString(); Modified: ofbiz/trunk/framework/security/src/org/ofbiz/security/authz/da/GroovyDaHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/security/src/org/ofbiz/security/authz/da/GroovyDaHandler.java?rev=1326996&r1=1326995&r2=1326996&view=diff ============================================================================== --- ofbiz/trunk/framework/security/src/org/ofbiz/security/authz/da/GroovyDaHandler.java (original) +++ ofbiz/trunk/framework/security/src/org/ofbiz/security/authz/da/GroovyDaHandler.java Tue Apr 17 08:44:43 2012 @@ -50,7 +50,7 @@ public class GroovyDaHandler implements if (accessString.startsWith("component://")) { // loaded using the OFBiz location API try { - result = GroovyUtil.runScriptAtLocation(accessString, bindings); + result = GroovyUtil.runScriptAtLocation(accessString, null, bindings); } catch (GeneralException e) { Debug.logWarning(e, module); } |
Free forum by Nabble | Edit this page |