Skip to content

Commit

Permalink
Improved: Rewrite ‘GroovyUtil#runScriptAtLocation’ (OFBIZ-10807)
Browse files Browse the repository at this point in the history
The ‘GroovyUtil#runScriptAtLocation’ method has now a javadoc and is
rewritten as a simple ternary expression.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1854653 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Mar 2, 2019
1 parent 164849a commit 4af4441
Showing 1 changed file with 18 additions and 8 deletions.
Expand Up @@ -192,15 +192,25 @@ public static Class<?> parseClass(String text) throws IOException {
return classLoader; return classLoader;
} }


public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context) throws GeneralException { /**
* Runs a Groovy script with a context argument.
* <p>
* A Groovy script can be either a stand-alone script or a method embedded in a script.
*
* @param location the location of the script file
* @param methodName the name of the method inside the script to be run,
* if it is {@code null} consider the script as stand-alone
* @param context the context of execution which is in the case
* of a method inside a script passed as an argument
* @return the invocation result
* @throws GeneralException when the script is not properly located
*/
public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context)
throws GeneralException {
Script script = InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context)); Script script = InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context));
Object result = null; return UtilValidate.isEmpty(methodName)
if (UtilValidate.isEmpty(methodName)) { ? script.run()
result = script.run(); : script.invokeMethod(methodName, new Object[] { context });
} else {
result = script.invokeMethod(methodName, new Object[] { context });
}
return result;
} }


private GroovyUtil() {} private GroovyUtil() {}
Expand Down

0 comments on commit 4af4441

Please sign in to comment.