public class GroovyExtendableScriptCache extends Object implements Serializable
Serializable
Groovy Script cache.
It provides automatic compilation of scripts and caches the resulting class(es) internally, and after de-serialization re-compiles the cached scripts automatically.
It also provides easy support for (and scoped) script compilation with a specific Script
base class.
Internally it uses a non-serializable and thus transient GroovyClassLoader
, CompilerConfiguration
and
the parent classloader to use.
To be able to be serializable, the GroovyClassLoader
is automatically (re)created if not defined yet, and for
the CompilerConfiguration
and parent classloader it uses serializable instances of
GroovyExtendableScriptCache.CompilerConfigurationFactory
and GroovyExtendableScriptCache.ParentClassLoaderFactory
interfaces which either can be configured
or have defaults otherwise.
The underlying GroovyClassLoader
can be accessed through getGroovyClassLoader()
, which might be needed
to de-serialize previously defined/created classes and objects through this class, from within a containing object
readObject(ObjectInputStream in) method.
For more information how this works and should be done, see:
Groovy-1627: Deserialization fails to work
One other optional feature is script pre-processing which can be configured through an instance of the
GroovyExtendableScriptCache.ScriptPreProcessor
interface (also Serializable
of course).
When configured, the script source will be passed through the GroovyExtendableScriptCache.ScriptPreProcessor.preProcess(String)
method
before being compiled.
The cache itself as well as the underlying GroovyClassLoader caches can be cleared through clearCache()
.
The GroovyExtendableScriptCache has no other external dependencies other than Groovy itself, so can be used independent of Commons SCXML.
Modifier and Type | Class and Description |
---|---|
static interface |
GroovyExtendableScriptCache.CompilerConfigurationFactory
Serializable factory interface providing the Groovy CompilerConfiguration,
needed to restore the specific CompilerConfiguration after de-serialization
|
static interface |
GroovyExtendableScriptCache.ParentClassLoaderFactory
Serializable factory interface providing the Groovy parent ClassLoader,
needed to restore the specific ClassLoader after de-serialization
|
protected static class |
GroovyExtendableScriptCache.ScriptCacheElement |
static interface |
GroovyExtendableScriptCache.ScriptPreProcessor |
Modifier and Type | Field and Description |
---|---|
static GroovyExtendableScriptCache.CompilerConfigurationFactory |
DEFAULT_COMPILER_CONFIGURATION_FACTORY
Default factory for the Groovy CompilerConfiguration, returning a new and unmodified CompilerConfiguration instance
|
static GroovyExtendableScriptCache.ParentClassLoaderFactory |
DEFAULT_PARENT_CLASS_LOADER_FACTORY
Default factory for the Groovy parent ClassLoader, returning this class its ClassLoader
|
static String |
DEFAULT_SCRIPT_CODE_BASE
Default CodeSource code base for the compiled Groovy scripts
|
Constructor and Description |
---|
GroovyExtendableScriptCache() |
public static final String DEFAULT_SCRIPT_CODE_BASE
public static final GroovyExtendableScriptCache.ParentClassLoaderFactory DEFAULT_PARENT_CLASS_LOADER_FACTORY
public static final GroovyExtendableScriptCache.CompilerConfigurationFactory DEFAULT_COMPILER_CONFIGURATION_FACTORY
public GroovyExtendableScriptCache()
public ClassLoader getGroovyClassLoader()
public groovy.lang.Script getScript(String scriptSource)
scriptSource
- The script source, which will optionally be first preprocessed through preProcessScript(String)
using the configured getScriptPreProcessor()
protected void ensureInitializedOrReloaded()
protected Class<groovy.lang.Script> compileScript(String scriptBaseClass, String scriptSource, String scriptName)
protected String preProcessScript(String scriptSource)
protected String generatedScriptName(String scriptSource, int seed)
public String getScriptCodeBase()
public void setScriptCodeBase(String scriptCodeBase)
scriptCodeBase
- The CodeSource code base to be used for the compilation of the Groovy scripts.DEFAULT_SCRIPT_CODE_BASE
will be set instead.public String getScriptBaseClass()
public void setScriptBaseClass(String scriptBaseClass)
public GroovyExtendableScriptCache.ParentClassLoaderFactory getParentClassLoaderFactory()
public void setParentClassLoaderFactory(GroovyExtendableScriptCache.ParentClassLoaderFactory parentClassLoaderFactory)
public GroovyExtendableScriptCache.CompilerConfigurationFactory getCompilerConfigurationFactory()
public void setCompilerConfigurationFactory(GroovyExtendableScriptCache.CompilerConfigurationFactory compilerConfigurationFactory)
public GroovyExtendableScriptCache.ScriptPreProcessor getScriptPreProcessor()
public void setScriptPreProcessor(GroovyExtendableScriptCache.ScriptPreProcessor scriptPreProcessor)
public boolean isEmpty()
public void clearCache()
Copyright © 2005–2015 The Apache Software Foundation. All rights reserved.