public class GroovyScriptFactory extends java.lang.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(java.lang.String scriptSourceLocator)
Create a new GroovyScriptFactory for the given script source.
|
GroovyScriptFactory(java.lang.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(java.lang.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(java.lang.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(java.lang.ClassLoader classLoader)
Build a
GroovyClassLoader for the given ClassLoader . |
protected java.lang.Object |
executeScript(ScriptSource scriptSource,
java.lang.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.
|
java.lang.Object |
getScriptedObject(ScriptSource scriptSource,
java.lang.Class<?>... actualInterfaces)
Loads and parses the Groovy script via the GroovyClassLoader.
|
java.lang.Class<?> |
getScriptedObjectType(ScriptSource scriptSource)
Determine the type of the scripted Java object.
|
java.lang.Class<?>[] |
getScriptInterfaces()
Groovy scripts determine their interfaces themselves,
hence we don't need to explicitly expose interfaces here.
|
java.lang.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(java.lang.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.
|
java.lang.String |
toString() |
public GroovyScriptFactory(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.ClassLoader classLoader)
GroovyClassLoader
for the given ClassLoader
.classLoader
- the ClassLoader to build a GroovyClassLoader forpublic java.lang.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 java.lang.Class<?>[] getScriptInterfaces()
getScriptInterfaces
in interface ScriptFactory
null
alwayspublic boolean requiresConfigInterface()
requiresConfigInterface
in interface ScriptFactory
ScriptFactory.getScriptInterfaces()
public java.lang.Object getScriptedObject(ScriptSource scriptSource, java.lang.Class<?>... actualInterfaces) throws java.io.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
)java.io.IOException
- if script retrieval failedScriptCompilationException
- if script compilation failedGroovyClassLoader
public java.lang.Class<?> getScriptedObjectType(ScriptSource scriptSource) throws java.io.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 determinedjava.io.IOException
- 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 java.lang.Object executeScript(ScriptSource scriptSource, java.lang.Class<?> scriptClass) throws ScriptCompilationException
scriptSource
- the source for the underlying scriptscriptClass
- the Groovy script classScriptCompilationException
- in case of instantiation failurepublic java.lang.String toString()
toString
in class java.lang.Object