Package groovy.util

Class GroovyScriptEngine

  • All Implemented Interfaces:
    ResourceConnector

    public class GroovyScriptEngine
    extends Object
    implements ResourceConnector
    Specific script engine able to reload modified scripts as well as dealing properly with dependent scripts.
    • Method Detail

      • main

        public static void main​(String[] urls)
                         throws Exception
        Simple testing harness for the GSE. Enter script roots as arguments and then input script names to run them.
        Parameters:
        urls - an array of URLs
        Throws:
        Exception - if something goes wrong
      • getParentClassLoader

        public ClassLoader getParentClassLoader()
        Get the ClassLoader that will serve as the parent ClassLoader of the GroovyClassLoader in which scripts will be executed. By default, this is the ClassLoader that loaded the GroovyScriptEngine class.
        Returns:
        the parent classloader used to load scripts
      • loadScriptByName

        public Class loadScriptByName​(String scriptName)
                               throws ResourceException,
                                      ScriptException
        Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading.
        Parameters:
        scriptName - resource name pointing to the script
        Returns:
        the loaded scriptName as a compiled class
        Throws:
        ResourceException - if there is a problem accessing the script
        ScriptException - if there is a problem parsing the script
      • run

        public String run​(String scriptName,
                          String argument)
                   throws ResourceException,
                          ScriptException
        Run a script identified by name with a single argument.
        Parameters:
        scriptName - name of the script to run
        argument - a single argument passed as a variable named arg in the binding
        Returns:
        a toString() representation of the result of the execution of the script
        Throws:
        ResourceException - if there is a problem accessing the script
        ScriptException - if there is a problem parsing the script
      • createScript

        public Script createScript​(String scriptName,
                                   Binding binding)
                            throws ResourceException,
                                   ScriptException
        Creates a Script with a given scriptName and binding.
        Parameters:
        scriptName - name of the script to run
        binding - the binding to pass to the script
        Returns:
        the script object
        Throws:
        ResourceException - if there is a problem accessing the script
        ScriptException - if there is a problem parsing the script
      • isSourceNewer

        protected boolean isSourceNewer​(groovy.util.GroovyScriptEngine.ScriptCacheEntry entry)
      • getGroovyClassLoader

        public GroovyClassLoader getGroovyClassLoader()
        Returns the GroovyClassLoader associated with this script engine instance. Useful if you need to pass the class loader to another library.
        Returns:
        the GroovyClassLoader
      • getCurrentTime

        protected long getCurrentTime()