public class GroovyScriptFactory extends Object implements ScriptFactory, BeanFactoryAware, BeanClassLoaderAware
ScriptFactory
implementation
for a Groovy script.
Typically used in combination with a
ScriptFactoryPostProcessor
;
see the latter's javadoc for a configuration example.
Note: Spring 4.0 supports Groovy 1.8 and higher.
GroovyClassLoader
,
ScriptFactoryPostProcessor
Constructor and Description |
---|
GroovyScriptFactory(String scriptSourceLocator)
Create a new GroovyScriptFactory for the given script source.
|
GroovyScriptFactory(String scriptSourceLocator,
org.codehaus.groovy.control.customizers.CompilationCustomizer... compilationCustomizers)
Create a new GroovyScriptFactory for the given script source,
specifying a strategy interface that can customize Groovy's compilation
process within the underlying GroovyClassLoader.
|
GroovyScriptFactory(String scriptSourceLocator,
org.codehaus.groovy.control.CompilerConfiguration compilerConfiguration)
Create a new GroovyScriptFactory for the given script source,
specifying a strategy interface that can create a custom MetaClass
to supply missing methods and otherwise change the behavior of the object.
|
GroovyScriptFactory(String scriptSourceLocator,
GroovyObjectCustomizer groovyObjectCustomizer)
Create a new GroovyScriptFactory for the given script source,
specifying a strategy interface that can create a custom MetaClass
to supply missing methods and otherwise change the behavior of the object.
|
Modifier and Type | Method and Description |
---|---|
protected groovy.lang.GroovyClassLoader |
buildGroovyClassLoader(ClassLoader classLoader)
Build a
GroovyClassLoader for the given ClassLoader . |
protected Object |
executeScript(ScriptSource scriptSource,
Class<?> scriptClass)
Instantiate the given Groovy script class and run it if necessary.
|
groovy.lang.GroovyClassLoader |
getGroovyClassLoader()
Return the GroovyClassLoader used by this script factory.
|
Object |
getScriptedObject(ScriptSource scriptSource,
Class<?>... actualInterfaces)
Loads and parses the Groovy script via the GroovyClassLoader.
|
Class<?> |
getScriptedObjectType(ScriptSource scriptSource)
Determine the type of the scripted Java object.
|
Class<?>[] |
getScriptInterfaces()
Groovy scripts determine their interfaces themselves,
hence we don't need to explicitly expose interfaces here.
|
String |
getScriptSourceLocator()
Return a locator that points to the source of the script.
|
boolean |
requiresConfigInterface()
Groovy scripts do not need a config interface,
since they expose their setters as public methods.
|
boolean |
requiresScriptedObjectRefresh(ScriptSource scriptSource)
Determine whether a refresh is required (e.g.
|
void |
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean
class loader to
a bean instance. |
void |
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
|
String |
toString() |
public GroovyScriptFactory(String scriptSourceLocator)
We don't need to specify script interfaces here, since a Groovy script defines its Java interfaces itself.
scriptSourceLocator
- a locator that points to the source of the script.
Interpreted by the post-processor that actually creates the script.public GroovyScriptFactory(String scriptSourceLocator, GroovyObjectCustomizer groovyObjectCustomizer)
scriptSourceLocator
- a locator that points to the source of the script.
Interpreted by the post-processor that actually creates the script.groovyObjectCustomizer
- a customizer that can set a custom metaclass
or make other changes to the GroovyObject created by this factory
(may be null
)GroovyObjectCustomizer.customize(groovy.lang.GroovyObject)
public GroovyScriptFactory(String scriptSourceLocator, org.codehaus.groovy.control.CompilerConfiguration compilerConfiguration)
scriptSourceLocator
- a locator that points to the source of the script.
Interpreted by the post-processor that actually creates the script.compilerConfiguration
- a custom compiler configuration to be applied
to the GroovyClassLoader (may be null
)GroovyClassLoader.GroovyClassLoader(ClassLoader, CompilerConfiguration)
public GroovyScriptFactory(String scriptSourceLocator, org.codehaus.groovy.control.customizers.CompilationCustomizer... compilationCustomizers)
scriptSourceLocator
- a locator that points to the source of the script.
Interpreted by the post-processor that actually creates the script.compilationCustomizers
- one or more customizers to be applied to the
GroovyClassLoader compiler configurationCompilerConfiguration.addCompilationCustomizers(org.codehaus.groovy.control.customizers.CompilationCustomizer...)
,
ImportCustomizer
public void setBeanFactory(BeanFactory beanFactory)
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
public void setBeanClassLoader(ClassLoader classLoader)
BeanClassLoaderAware
class loader
to
a bean instance.
Invoked after the population of normal bean properties but
before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.
setBeanClassLoader
in interface BeanClassLoaderAware
classLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
public groovy.lang.GroovyClassLoader getGroovyClassLoader()
protected groovy.lang.GroovyClassLoader buildGroovyClassLoader(ClassLoader classLoader)
GroovyClassLoader
for the given ClassLoader
.classLoader
- the ClassLoader to build a GroovyClassLoader forpublic String getScriptSourceLocator()
ScriptFactory
Typical supported locators are Spring resource locations (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") and inline scripts ("inline:myScriptText...").
getScriptSourceLocator
in interface ScriptFactory
ScriptFactoryPostProcessor.convertToScriptSource(java.lang.String, java.lang.String, org.springframework.core.io.ResourceLoader)
,
ResourceLoader
public Class<?>[] getScriptInterfaces()
getScriptInterfaces
in interface ScriptFactory
null
alwayspublic boolean requiresConfigInterface()
requiresConfigInterface
in interface ScriptFactory
ScriptFactory.getScriptInterfaces()
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) throws IOException, ScriptCompilationException
getScriptedObject
in interface ScriptFactory
scriptSource
- the actual ScriptSource to retrieve
the script source text from (never null
)actualInterfaces
- the actual interfaces to expose,
including script interfaces as well as a generated config interface
(if applicable; may be null
)IOException
- if script retrieval failedScriptCompilationException
- if script compilation failedGroovyClassLoader
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException
ScriptFactory
Implementations are encouraged to cache script metadata such as a generated script class. Note that this method may be invoked concurrently and must be implemented in a thread-safe fashion.
getScriptedObjectType
in interface ScriptFactory
scriptSource
- the actual ScriptSource to retrieve
the script source text from (never null
)null
if none could be determinedIOException
- if script retrieval failedScriptCompilationException
- if script compilation failedpublic boolean requiresScriptedObjectRefresh(ScriptSource scriptSource)
ScriptFactory
isModified()
method).requiresScriptedObjectRefresh
in interface ScriptFactory
scriptSource
- the actual ScriptSource to retrieve
the script source text from (never null
)ScriptFactory.getScriptedObject(org.springframework.scripting.ScriptSource, java.lang.Class<?>...)
call is requiredScriptSource.isModified()
protected Object executeScript(ScriptSource scriptSource, Class<?> scriptClass) throws ScriptCompilationException
scriptSource
- the source for the underlying scriptscriptClass
- the Groovy script classScriptCompilationException
- in case of instantiation failure