Package groovy.util
Class GroovyScriptEngine
java.lang.Object
groovy.util.GroovyScriptEngine
- All Implemented Interfaces:
ResourceConnector
public class GroovyScriptEngine extends java.lang.Object implements ResourceConnector
Specific script engine able to reload modified scripts as well as dealing properly
with dependent scripts.
-
Constructor Summary
Constructors Constructor Description GroovyScriptEngine(ResourceConnector rc)GroovyScriptEngine(ResourceConnector rc, java.lang.ClassLoader parentClassLoader)GroovyScriptEngine(java.lang.String url)GroovyScriptEngine(java.lang.String[] urls)GroovyScriptEngine(java.lang.String[] urls, java.lang.ClassLoader parentClassLoader)GroovyScriptEngine(java.lang.String url, java.lang.ClassLoader parentClassLoader)GroovyScriptEngine(java.net.URL[] roots)GroovyScriptEngine(java.net.URL[] roots, java.lang.ClassLoader parentClassLoader) -
Method Summary
Modifier and Type Method Description ScriptcreateScript(java.lang.String scriptName, Binding binding)Creates a Script with a given scriptName and binding.CompilerConfigurationgetConfig()protected longgetCurrentTime()GroovyClassLoadergetGroovyClassLoader()Returns the GroovyClassLoader associated with this script engine instance.java.lang.ClassLoadergetParentClassLoader()Get theClassLoaderthat will serve as the parent ClassLoader of theGroovyClassLoaderin which scripts will be executed.java.net.URLConnectiongetResourceConnection(java.lang.String resourceName)Get a resource connection as aURLConnectionto retrieve a script from theResourceConnector.protected booleanisSourceNewer(groovy.util.GroovyScriptEngine.ScriptCacheEntry entry)java.lang.ClassloadScriptByName(java.lang.String scriptName)Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading.static voidmain(java.lang.String[] urls)Simple testing harness for the GSE.java.lang.Objectrun(java.lang.String scriptName, Binding binding)Run a script identified by name with a given binding.java.lang.Stringrun(java.lang.String scriptName, java.lang.String argument)Run a script identified by name with a single argument.voidsetConfig(CompilerConfiguration config)sets a compiler configurationMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
GroovyScriptEngine
public GroovyScriptEngine(java.net.URL[] roots) -
GroovyScriptEngine
public GroovyScriptEngine(java.net.URL[] roots, java.lang.ClassLoader parentClassLoader) -
GroovyScriptEngine
public GroovyScriptEngine(java.lang.String[] urls) throws java.io.IOException- Throws:
java.io.IOException
-
GroovyScriptEngine
public GroovyScriptEngine(java.lang.String[] urls, java.lang.ClassLoader parentClassLoader) throws java.io.IOException- Throws:
java.io.IOException
-
GroovyScriptEngine
public GroovyScriptEngine(java.lang.String url) throws java.io.IOException- Throws:
java.io.IOException
-
GroovyScriptEngine
public GroovyScriptEngine(java.lang.String url, java.lang.ClassLoader parentClassLoader) throws java.io.IOException- Throws:
java.io.IOException
-
GroovyScriptEngine
-
GroovyScriptEngine
-
-
Method Details
-
main
public static void main(java.lang.String[] urls) throws java.lang.ExceptionSimple 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:
java.lang.Exception- if something goes wrong
-
getResourceConnection
public java.net.URLConnection getResourceConnection(java.lang.String resourceName) throws ResourceExceptionGet 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
public java.lang.ClassLoader 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
public java.lang.Class loadScriptByName(java.lang.String scriptName) throws ResourceException, ScriptExceptionGet 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
public java.lang.String run(java.lang.String scriptName, java.lang.String argument) throws ResourceException, ScriptExceptionRun 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
public java.lang.Object run(java.lang.String scriptName, Binding binding) throws ResourceException, ScriptExceptionRun 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(java.lang.String scriptName, Binding binding) throws ResourceException, ScriptExceptionCreates 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) -
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:
java.lang.NullPointerException- if config is null
-
getCurrentTime
protected long getCurrentTime()
-