Package groovy.util
Class GroovyScriptEngine
java.lang.Object
groovy.util.GroovyScriptEngine
- All Implemented Interfaces:
ResourceConnector
Specific script engine able to reload modified scripts as well as dealing properly
with dependent scripts.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a script engine that delegates resource lookup to the supplied connector.GroovyScriptEngine(ResourceConnector rc, ClassLoader parentClassLoader) Creates a script engine that delegates resource lookup to the supplied connector.GroovyScriptEngine(String url) Creates a script engine for a single root location.GroovyScriptEngine(String[] urls) Creates a script engine from string root locations.GroovyScriptEngine(String[] urls, ClassLoader parentClassLoader) Creates a script engine from string root locations.GroovyScriptEngine(String url, ClassLoader parentClassLoader) Creates a script engine for a single root location.GroovyScriptEngine(URL[] roots) Creates a script engine rooted at the supplied URLs.GroovyScriptEngine(URL[] roots, ClassLoader parentClassLoader) Creates a script engine rooted at the supplied URLs. -
Method Summary
Modifier and TypeMethodDescriptioncreateScript(String scriptName, Binding binding) Creates a Script with a given scriptName and binding.protected longReturns the current time used for recompilation checks.Returns the GroovyClassLoader associated with this script engine instance.Get theClassLoaderthat will serve as the parent ClassLoader of theGroovyClassLoaderin which scripts will be executed.getResourceConnection(String resourceName) Get a resource connection as aURLConnectionto retrieve a script from theResourceConnector.protected booleanisSourceNewer(groovy.util.GroovyScriptEngine.ScriptCacheEntry entry) Determines whether a cached script or one of its dependencies should be recompiled.loadScriptByName(String scriptName) Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading.static voidSimple testing harness for the GSE.Run a script identified by name with a given binding.Run a script identified by name with a single argument.voidsetConfig(CompilerConfiguration config) sets a compiler configuration
-
Constructor Details
-
GroovyScriptEngine
Creates a script engine rooted at the supplied URLs.- Parameters:
roots- script root URLs
-
GroovyScriptEngine
Creates a script engine rooted at the supplied URLs.- Parameters:
roots- script root URLsparentClassLoader- parent class loader for compiled scripts
-
GroovyScriptEngine
Creates a script engine from string root locations.- Parameters:
urls- script root URLs or file paths- Throws:
IOException- if a root cannot be converted to a URL
-
GroovyScriptEngine
Creates a script engine from string root locations.- Parameters:
urls- script root URLs or file pathsparentClassLoader- parent class loader for compiled scripts- Throws:
IOException- if a root cannot be converted to a URL
-
GroovyScriptEngine
Creates a script engine for a single root location.- Parameters:
url- script root URL or file path- Throws:
IOException- if the root cannot be converted to a URL
-
GroovyScriptEngine
Creates a script engine for a single root location.- Parameters:
url- script root URL or file pathparentClassLoader- parent class loader for compiled scripts- Throws:
IOException- if the root cannot be converted to a URL
-
GroovyScriptEngine
Creates a script engine that delegates resource lookup to the supplied connector.- Parameters:
rc- resource connector used to resolve scripts
-
GroovyScriptEngine
Creates a script engine that delegates resource lookup to the supplied connector.- Parameters:
rc- resource connector used to resolve scriptsparentClassLoader- parent class loader for compiled scripts
-
-
Method Details
-
main
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
-
getResourceConnection
Get a resource connection as aURLConnectionto retrieve a script from theResourceConnector.- Specified by:
getResourceConnectionin interfaceResourceConnector- Parameters:
resourceName- name of the resource to be retrieved- Returns:
- a URLConnection to the resource
- Throws:
ResourceException
-
getParentClassLoader
Get theClassLoaderthat will serve as the parent ClassLoader of theGroovyClassLoaderin which scripts will be executed. By default, this is the ClassLoader that loaded theGroovyScriptEngineclass.- Returns:
- the parent classloader used to load scripts
-
loadScriptByName
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 scriptScriptException- if there is a problem parsing the script
-
run
Run a script identified by name with a single argument.- Parameters:
scriptName- name of the script to runargument- a single argument passed as a variable namedargin the binding- Returns:
- a
toString()representation of the result of the execution of the script - Throws:
ResourceException- if there is a problem accessing the scriptScriptException- if there is a problem parsing the script
-
run
Run a script identified by name with a given binding.- Parameters:
scriptName- name of the script to runbinding- the binding to pass to the script- Returns:
- an object
- Throws:
ResourceException- if there is a problem accessing the scriptScriptException- 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 runbinding- the binding to pass to the script- Returns:
- the script object
- Throws:
ResourceException- if there is a problem accessing the scriptScriptException- if there is a problem parsing the script
-
isSourceNewer
protected boolean isSourceNewer(groovy.util.GroovyScriptEngine.ScriptCacheEntry entry) Determines whether a cached script or one of its dependencies should be recompiled.- Parameters:
entry- the cached script entry- Returns:
trueif recompilation is required
-
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
-
getConfig
- Returns:
- a non null compiler configuration
-
setConfig
sets a compiler configuration- Parameters:
config- - the compiler configuration- Throws:
NullPointerException- if config is null
-
getCurrentTime
protected long getCurrentTime()Returns the current time used for recompilation checks.- Returns:
- the current time in milliseconds
-