Top Description Inners Fields Constructors Methods
groovy.util

public Class GroovyScriptEngine

extends Object
implements ResourceConnector
Class Inheritance
All Implemented Interfaces
groovy.util.ResourceConnector
Imports
groovy.lang.Binding, .GroovyClassLoader, .GroovyCodeSource, .GroovyResourceLoader, .Script, org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.ClassHelper, .ClassNode, org.codehaus.groovy.classgen.GeneratorContext, org.codehaus.groovy.control.ClassNodeResolver, .CompilationFailedException, .CompilationUnit, .CompilerConfiguration, .Phases, .SourceUnit, org.codehaus.groovy.runtime.IOGroovyMethods, .InvokerHelper, org.codehaus.groovy.tools.gse.DependencyTracker, .StringSetMap, java.io.BufferedReader, .File, .IOException, .InputStream, .InputStreamReader, java.lang.ref.WeakReference, java.net.MalformedURLException, .URL, .URLConnection, java.security.AccessController, .CodeSource, .PrivilegedAction, java.util.HashMap, .HashSet, .Map, .Set, java.util.concurrent.ConcurrentHashMap

Specific script engine able to reload modified scripts as well as dealing properly with dependent scripts.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private class

Field Summary

Modifier and TypeField and Description
private static final ClassLoader
private CompilerConfiguration
private static final URL[]
private GroovyClassLoader
private static WeakReference<ThreadLocal<GroovyScriptEngine.LocalData>>
private final ClassLoader
private final ResourceConnector
private final URL[]
private final Map<String, GroovyScriptEngine.ScriptCacheEntry>

Constructor Summary

AccessConstructor and Description
private
GroovyScriptEngine(URL[]
This an array of URLs where Groovy scripts will be stored. They should be laid out using their package structure like Java classes
roots
,
ClassLoader parent, ResourceConnector rc)

The groovy script engine will run groovy scripts and reload them and their dependencies when they are modified.

public
public
GroovyScriptEngine(URL[] roots, ClassLoader parentClassLoader)

public
public
GroovyScriptEngine(String[] urls, ClassLoader parentClassLoader)

public
public
GroovyScriptEngine(String url, ClassLoader parentClassLoader)

public
public

Method Summary

Modifier and TypeMethod and Description
private static URL[]
public Script

Returns:

the script object
createScript
(String
name of the script to run
scriptName
,
Binding
the binding to pass to the script
binding
)

Creates a Script with a given scriptName and binding.

private static void
forceClose(URLConnection
the URLConnection to be "closed" to close the underlying file descriptors.
urlConnection
)

This method closes a URLConnection by getting its InputStream and calling the InputStream#close() method on it.

public CompilerConfiguration

Returns:

a non null compiler configuration
getConfig
()

protected long
public GroovyClassLoader

Returns:

the GroovyClassLoader
getGroovyClassLoader
()

Returns the GroovyClassLoader associated with this script engine instance.

private long
getLastModified(String scriptName)

private static synchronized ThreadLocal<GroovyScriptEngine.LocalData>
public ClassLoader

Returns:

the parent classloader used to load scripts
getParentClassLoader
()

Get the ClassLoader that will serve as the parent ClassLoader of the GroovyClassLoader in which scripts will be executed.

public URLConnection

Returns:

a URLConnection to the resource
getResourceConnection
(String
name of the resource to be retrieved
resourceName
)

Implements groovy.util.ResourceConnector.getResourceConnection.

Get a resource connection as a URLConnection to retrieve a script from the ResourceConnector.
private GroovyClassLoader

Returns:

the parent classloader used to load scripts
initGroovyLoader
()

Initialize a new GroovyClassLoader with a default or constructor-supplied parentClassLoader.

protected boolean
public Class<T>

Returns:

the loaded scriptName as a compiled class
loadScriptByName
(String
resource name pointing to the script
scriptName
)

Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading.

public static void
main(String[]
an array of URLs
urls
)

Simple testing harness for the GSE.

private static URLConnection
openConnection(URL scriptURL)

public String

Returns:

a toString() representation of the result of the execution of the script
run
(String
name of the script to run
scriptName
,
String
a single argument passed as a variable named arg in the binding
argument
)

Run a script identified by name with a single argument.

public Object

Returns:

an object
run
(String
name of the script to run
scriptName
,
Binding
the binding to pass to the script
binding
)

Run a script identified by name with a given binding.

public void
setConfig(CompilerConfiguration
- the compiler configuration
config
)

sets a compiler configuration

private static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CL_STUBback to summary
private static final ClassLoader CL_STUB
configback to summary
private CompilerConfiguration config
EMPTY_URL_ARRAYback to summary
private static final URL[] EMPTY_URL_ARRAY
groovyLoaderback to summary
private GroovyClassLoader groovyLoader
localDataback to summary
private static WeakReference<ThreadLocal<GroovyScriptEngine.LocalData>> localData
parentLoaderback to summary
private final ClassLoader parentLoader
rcback to summary
private final ResourceConnector rc
rootsback to summary
private final URL[] roots
scriptCacheback to summary
private final Map<String, GroovyScriptEngine.ScriptCacheEntry> scriptCache

Constructor Detail

GroovyScriptEngineback to summary
private GroovyScriptEngine(URL[] roots, ClassLoader parent, ResourceConnector rc)

The groovy script engine will run groovy scripts and reload them and their dependencies when they are modified. This is useful for embedding groovy in other containers like games and application servers.

Parameters
roots:URL[]

This an array of URLs where Groovy scripts will be stored. They should be laid out using their package structure like Java classes

GroovyScriptEngineback to summary
public GroovyScriptEngine(URL[] roots)
GroovyScriptEngineback to summary
public GroovyScriptEngine(URL[] roots, ClassLoader parentClassLoader)
GroovyScriptEngineback to summary
public GroovyScriptEngine(String[] urls) throws IOException
GroovyScriptEngineback to summary
public GroovyScriptEngine(String[] urls, ClassLoader parentClassLoader) throws IOException
GroovyScriptEngineback to summary
public GroovyScriptEngine(String url) throws IOException
GroovyScriptEngineback to summary
public GroovyScriptEngine(String url, ClassLoader parentClassLoader) throws IOException
GroovyScriptEngineback to summary
public GroovyScriptEngine(ResourceConnector rc)
GroovyScriptEngineback to summary
public GroovyScriptEngine(ResourceConnector rc, ClassLoader parentClassLoader)

Method Detail

createRootsback to summary
private static URL[] createRoots(String[] urls) throws MalformedURLException
createScriptback to summary
public Script createScript(String scriptName, Binding binding) throws ResourceException, ScriptException

Creates a Script with a given scriptName and binding.

Parameters
scriptName:String

name of the script to run

binding:Binding

the binding to pass to the script

Returns:Script

the script object

Exceptions
ResourceException:
if there is a problem accessing the script
ScriptException:
if there is a problem parsing the script
forceCloseback to summary
private static void forceClose(URLConnection urlConnection)

This method closes a URLConnection by getting its InputStream and calling the InputStream#close() method on it. The URLConnection doesn't have a close() method and relies on garbage collection to close the underlying connection to the file. Relying on garbage collection could lead to the application exhausting the number of files the user is allowed to have open at any one point in time and cause the application to crash (java.io.FileNotFoundException (Too many open files)). Hence the need for this method to explicitly close the underlying connection to the file.

Parameters
urlConnection:URLConnection

the URLConnection to be "closed" to close the underlying file descriptors.

getConfigback to summary
public CompilerConfiguration getConfig()
Returns:CompilerConfiguration

a non null compiler configuration

getCurrentTimeback to summary
protected long getCurrentTime()
getGroovyClassLoaderback to summary
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:GroovyClassLoader

the GroovyClassLoader

getLastModifiedback to summary
private long getLastModified(String scriptName) throws ResourceException
getLocalDataback to summary
private static synchronized ThreadLocal<GroovyScriptEngine.LocalData> getLocalData()
getParentClassLoaderback to summary
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:ClassLoader

the parent classloader used to load scripts

getResourceConnectionback to summary
public URLConnection getResourceConnection(String resourceName) throws ResourceException

Implements groovy.util.ResourceConnector.getResourceConnection.

Get a resource connection as a URLConnection to retrieve a script from the ResourceConnector.

Parameters
resourceName:String

name of the resource to be retrieved

Returns:URLConnection

a URLConnection to the resource

initGroovyLoaderback to summary
private GroovyClassLoader initGroovyLoader()

Initialize a new GroovyClassLoader with a default or constructor-supplied parentClassLoader.

Returns:GroovyClassLoader

the parent classloader used to load scripts

isSourceNewerback to summary
protected boolean isSourceNewer(GroovyScriptEngine.ScriptCacheEntry entry)
loadScriptByNameback to summary
public Class<T> 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:String

resource name pointing to the script

Returns:Class<T>

the loaded scriptName as a compiled class

Exceptions
ResourceException:
if there is a problem accessing the script
ScriptException:
if there is a problem parsing the script
mainback to summary
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:String[]

an array of URLs

Exceptions
Exception:
if something goes wrong
openConnectionback to summary
private static URLConnection openConnection(URL scriptURL) throws IOException
runback to summary
public String run(String scriptName, String argument) throws ResourceException, ScriptException

Run a script identified by name with a single argument.

Parameters
scriptName:String

name of the script to run

argument:String

a single argument passed as a variable named arg in the binding

Returns:String

a toString() representation of the result of the execution of the script

Exceptions
ResourceException:
if there is a problem accessing the script
ScriptException:
if there is a problem parsing the script
runback to summary
public Object run(String scriptName, Binding binding) throws ResourceException, ScriptException

Run a script identified by name with a given binding.

Parameters
scriptName:String

name of the script to run

binding:Binding

the binding to pass to the script

Returns:Object

an object

Exceptions
ResourceException:
if there is a problem accessing the script
ScriptException:
if there is a problem parsing the script
setConfigback to summary
public void setConfig(CompilerConfiguration config)

sets a compiler configuration

Parameters
config:CompilerConfiguration

- the compiler configuration

Exceptions
NullPointerException:
if config is null
verifyInputStreamback to summary
private static void verifyInputStream(URLConnection urlConnection) throws IOException
groovy.util back to summary

private Class GroovyScriptEngine.LocalData

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv CompilationUnit
pack-priv final StringSetMap
pack-priv final Map<String, String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cuback to summary
pack-priv CompilationUnit cu
dependencyCacheback to summary
pack-priv final StringSetMap dependencyCache
precompiledEntriesback to summary
pack-priv final Map<String, String> precompiledEntries

Constructor Detail

LocalDataback to summary
private LocalData()
groovy.util back to summary

private Class GroovyScriptEngine.ScriptCacheEntry

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final Set<String>
private final long
private final long
private final Class<T>
private final boolean

Constructor Summary

AccessConstructor and Description
public
ScriptCacheEntry(Class<T> clazz, long modified, long lastCheck, Set<String> depend, boolean sourceNewer)

public
ScriptCacheEntry(GroovyScriptEngine.ScriptCacheEntry old, long lastCheck, boolean sourceNewer)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dependenciesback to summary
private final Set<String> dependencies
lastCheckback to summary
private final long lastCheck
lastModifiedback to summary
private final long lastModified
scriptClassback to summary
private final Class<T> scriptClass
sourceNewerback to summary
private final boolean sourceNewer

Constructor Detail

ScriptCacheEntryback to summary
public ScriptCacheEntry(Class<T> clazz, long modified, long lastCheck, Set<String> depend, boolean sourceNewer)
ScriptCacheEntryback to summary
public ScriptCacheEntry(GroovyScriptEngine.ScriptCacheEntry old, long lastCheck, boolean sourceNewer)
groovy.util back to summary

private Class GroovyScriptEngine.ScriptClassLoader

extends GroovyClassLoader
Class Inheritance

Field Summary

Inherited from groovy.lang.GroovyClassLoader:
classCachesourceCache

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
private Set<String>
convertToPaths(Set<String> orig, Map<String, String> precompiledEntries)

protected CompilationUnit
createCompilationUnit(CompilerConfiguration
the compiler configuration, usually the same as for this class loader
configuration
,
CodeSource
the source containing the initial file to compile, more files may follow during compilation
source
)

Overrides groovy.lang.GroovyClassLoader.createCompilationUnit.

creates a new CompilationUnit.
private Class<?>
private String
getPath(Class<T> clazz, Map<String, String> precompiledEntries)

public Class<T>
parseClass(GroovyCodeSource codeSource, boolean
if true then the generated class will be stored in the source cache
shouldCacheSource
)

Overrides groovy.lang.GroovyClassLoader.parseClass.

Parses the given code source into a Java class.
private void
private void
private void
Inherited from groovy.lang.GroovyClassLoader:
addClasspathaddURLclearCacheclosecreateCollectordefineClassdefineClassgenerateScriptNamegetClassCacheEntrygetClassPathgetLoadedClassesgetPermissionsgetResourceLoadergetTimeStamphasCompatibleConfigurationisRecompilableisShouldRecompileisSourceNewerloadClassloadClassloadClassloadClassparseClassparseClassparseClassparseClassparseClassparseClassrecompileremoveClassCacheEntrysetClassCacheEntrysetResourceLoadersetShouldRecompile

Constructor Detail

ScriptClassLoaderback to summary
public ScriptClassLoader(GroovyClassLoader loader)
ScriptClassLoaderback to summary
public ScriptClassLoader(ClassLoader loader, CompilerConfiguration config)

Method Detail

convertToPathsback to summary
private Set<String> convertToPaths(Set<String> orig, Map<String, String> precompiledEntries)
createCompilationUnitback to summary
protected CompilationUnit createCompilationUnit(CompilerConfiguration configuration, CodeSource source)

Overrides groovy.lang.GroovyClassLoader.createCompilationUnit.

Doc from groovy.lang.GroovyClassLoader.createCompilationUnit.

creates a new CompilationUnit. If you want to add additional phase operations to the CompilationUnit (for example to inject additional methods, variables, fields), then you should overwrite this method.

Parameters
configuration:CompilerConfiguration

the compiler configuration, usually the same as for this class loader

source:CodeSource

the source containing the initial file to compile, more files may follow during compilation

Returns:CompilationUnit

the CompilationUnit

Annotations
@Override
doParseClassback to summary
private Class<?> doParseClass(GroovyCodeSource codeSource)

Hides groovy.lang.GroovyClassLoader.doParseClass.

getPathback to summary
private String getPath(Class<T> clazz, Map<String, String> precompiledEntries)
parseClassback to summary
public Class<T> parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource) throws CompilationFailedException

Overrides groovy.lang.GroovyClassLoader.parseClass.

Doc from groovy.lang.GroovyClassLoader.parseClass.

Parses the given code source into a Java class. If there is a class file for the given code source, then no parsing is done, instead the cached class is returned.

Parameters
shouldCacheSource:boolean

if true then the generated class will be stored in the source cache

Returns:Class<T>

the main class defined in the given script

Annotations
@Override
setResLoaderback to summary
private void setResLoader()
updateLocalDependencyCacheback to summary
private void updateLocalDependencyCache(GroovyCodeSource codeSource, GroovyScriptEngine.LocalData localData)
updateScriptCacheback to summary
private void updateScriptCache(GroovyScriptEngine.LocalData localData)