Class GroovyUtil

java.lang.Object
org.apache.ofbiz.base.util.GroovyUtil

public final class GroovyUtil extends Object
Groovy Utilities.
  • Method Details

    • eval

      public static Object eval(String expression, Map<String,Object> context) throws CompilationFailedException
      Evaluate a Groovy condition or expression
      Parameters:
      expression - The expression to evaluate
      context - The context to use in evaluation (re-written)
      Returns:
      Object The result of the evaluation
      Throws:
      CompilationFailedException
      See Also:
    • getBinding

      public static Binding getBinding(Map<String,Object> context, String expression)
      Returns a Binding instance initialized with the variables contained in context. If context is null, an empty Binding is returned.

      The expression is parsed to initiate non existing variable in Binding to null for GroovyShell evaluation.

      The context Map is added to the Binding as a variable called "context" so that variables can be passed back to the caller. Any variables that are created in the script are lost when the script ends unless they are copied to the "context" Map.

      Parameters:
      context - A Map containing initial variables
      Returns:
      A Binding instance
    • getBinding

      public static Binding getBinding(Map<String,Object> context)
    • getScriptClassFromLocation

      public static Class<?> getScriptClassFromLocation(String location) throws GeneralException
      Throws:
      GeneralException
    • parseClass

      public static Class<?> parseClass(String text) throws IOException
      Throws:
      IOException
    • runScriptAtLocation

      public static Object runScriptAtLocation(String location, String methodName, Map<String,Object> context) throws GeneralException
      Runs a Groovy script with a context argument.

      A Groovy script can be either a stand-alone script or a method embedded in a script.

      Parameters:
      location - the location of the script file
      methodName - the name of the method inside the script to be run, if it is null consider the script as stand-alone
      context - the context of execution which is in the case of a method inside a script passed as an argument
      Returns:
      the invocation result
      Throws:
      GeneralException - when the script is not properly located