Top Description Inners Fields Constructors Methods
org.springframework.scripting.groovy

public Class GroovyScriptFactory

extends Object
implements ScriptFactory, BeanFactoryAware, BeanClassLoaderAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.scripting.ScriptFactory
Imports
java.io.IOException, java.lang.reflect.InvocationTargetException, groovy.lang.GroovyClassLoader, .GroovyObject, .MetaClass, .Script, org.codehaus.groovy.control.CompilationFailedException, .CompilerConfiguration, org.codehaus.groovy.control.customizers.CompilationCustomizer, org.springframework.beans.factory.BeanClassLoaderAware, .BeanFactory, .BeanFactoryAware, org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.lang.Nullable, org.springframework.scripting.ScriptCompilationException, .ScriptFactory, .ScriptSource, org.springframework.util.Assert, .ClassUtils, .ObjectUtils, .ReflectionUtils

org.springframework.scripting.ScriptFactory implementation for a Groovy script.

Typically used in combination with a org.springframework.scripting.support.ScriptFactoryPostProcessor; see the latter's javadoc for a configuration example.

Note

Spring 4.0 supports Groovy 1.8 and higher.

Authors
Juergen Hoeller, Rob Harrop, Rod Johnson
Since
2.0
See Also
groovy.lang.GroovyClassLoader, org.springframework.scripting.support.ScriptFactoryPostProcessor

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
GroovyScriptFactory.CachedResultHolder

Wrapper that holds a temporarily cached result object.

Field Summary

Modifier and TypeField and Description
private GroovyScriptFactory.CachedResultHolder
private CompilerConfiguration
private GroovyClassLoader
private GroovyObjectCustomizer
private Class<?>
private final Object
private Class<?>
private final String
private boolean

Constructor Summary

AccessConstructor and Description
public
GroovyScriptFactory(String
a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.
scriptSourceLocator
)

Create a new GroovyScriptFactory for the given script source.

public
GroovyScriptFactory(String
a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.
scriptSourceLocator
,
GroovyObjectCustomizer
a customizer that can set a custom metaclass or make other changes to the GroovyObject created by this factory (may be null)
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.

public
GroovyScriptFactory(String
a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.
scriptSourceLocator
,
CompilerConfiguration
a custom compiler configuration to be applied to the GroovyClassLoader (may be null)
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.

public
GroovyScriptFactory(String
a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.
scriptSourceLocator
,
CompilationCustomizer...
one or more customizers to be applied to the GroovyClassLoader compiler configuration
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.

Method Summary

Modifier and TypeMethod and Description
protected GroovyClassLoader
buildGroovyClassLoader(ClassLoader
the ClassLoader to build a GroovyClassLoader for
classLoader
)

Build a GroovyClassLoader for the given ClassLoader.

protected Object

Returns:

the result object (either an instance of the script class or the result of running the script instance)
executeScript
(ScriptSource
the source for the underlying script
scriptSource
,
Class<?>
the Groovy script class
scriptClass
)

Instantiate the given Groovy script class and run it if necessary.

public GroovyClassLoader
getGroovyClassLoader()

Return the GroovyClassLoader used by this script factory.

public Object
getScriptedObject(ScriptSource
the actual ScriptSource to retrieve the script source text from (never null)
scriptSource
,
Class<?>...
the actual interfaces to expose, including script interfaces as well as a generated config interface (if applicable; may be null)
actualInterfaces
)

Implements org.springframework.scripting.ScriptFactory.getScriptedObject.

Loads and parses the Groovy script via the GroovyClassLoader.

public Class<?>
getScriptedObjectType(ScriptSource
the actual ScriptSource to retrieve the script source text from (never null)
scriptSource
)

Implements org.springframework.scripting.ScriptFactory.getScriptedObjectType.

Determine the type of the scripted Java object.

public Class<?>[]

Returns:

null always
getScriptInterfaces
()

Implements org.springframework.scripting.ScriptFactory.getScriptInterfaces.

Groovy scripts determine their interfaces themselves, hence we don't need to explicitly expose interfaces here.

public String
getScriptSourceLocator()

Implements org.springframework.scripting.ScriptFactory.getScriptSourceLocator.

Return a locator that points to the source of the script.

public boolean
requiresConfigInterface()

Implements org.springframework.scripting.ScriptFactory.requiresConfigInterface.

Groovy scripts do not need a config interface, since they expose their setters as public methods.

public boolean
requiresScriptedObjectRefresh(ScriptSource
the actual ScriptSource to retrieve the script source text from (never null)
scriptSource
)

Implements org.springframework.scripting.ScriptFactory.requiresScriptedObjectRefresh.

Determine whether a refresh is required (e.g. through ScriptSource's isModified() method).

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cachedResultback to summary
private GroovyScriptFactory.CachedResultHolder cachedResult
Annotations
@Nullable
compilerConfigurationback to summary
private CompilerConfiguration compilerConfiguration
Annotations
@Nullable
groovyClassLoaderback to summary
private GroovyClassLoader groovyClassLoader
Annotations
@Nullable
groovyObjectCustomizerback to summary
private GroovyObjectCustomizer groovyObjectCustomizer
Annotations
@Nullable
scriptClassback to summary
private Class<?> scriptClass
Annotations
@Nullable
scriptClassMonitorback to summary
private final Object scriptClassMonitor
scriptResultClassback to summary
private Class<?> scriptResultClass
Annotations
@Nullable
scriptSourceLocatorback to summary
private final String scriptSourceLocator
wasModifiedForTypeCheckback to summary
private boolean wasModifiedForTypeCheck

Constructor Detail

GroovyScriptFactoryback to summary
public GroovyScriptFactory(String scriptSourceLocator)

Create a new GroovyScriptFactory for the given script source.

We don't need to specify script interfaces here, since a Groovy script defines its Java interfaces itself.

Parameters
scriptSourceLocator:String

a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.

GroovyScriptFactoryback to summary
public 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.

Parameters
scriptSourceLocator:String

a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.

groovyObjectCustomizer:GroovyObjectCustomizer

a customizer that can set a custom metaclass or make other changes to the GroovyObject created by this factory (may be null)

See Also
GroovyObjectCustomizer#customize
GroovyScriptFactoryback to summary
public GroovyScriptFactory(String scriptSourceLocator, 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.

Parameters
scriptSourceLocator:String

a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.

compilerConfiguration:CompilerConfiguration

a custom compiler configuration to be applied to the GroovyClassLoader (may be null)

Since
4.3.3
See Also
GroovyClassLoader#GroovyClassLoader(ClassLoader, CompilerConfiguration)
GroovyScriptFactoryback to summary
public GroovyScriptFactory(String scriptSourceLocator, 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.

Parameters
scriptSourceLocator:String

a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.

compilationCustomizers:CompilationCustomizer[]

one or more customizers to be applied to the GroovyClassLoader compiler configuration

Since
4.3.3
See Also
CompilerConfiguration#addCompilationCustomizers, org.codehaus.groovy.control.customizers.ImportCustomizer

Method Detail

buildGroovyClassLoaderback to summary
protected GroovyClassLoader buildGroovyClassLoader(ClassLoader classLoader)

Build a GroovyClassLoader for the given ClassLoader.

Parameters
classLoader:ClassLoader

the ClassLoader to build a GroovyClassLoader for

Since
4.3.3
executeScriptback to summary
protected Object executeScript(ScriptSource scriptSource, Class<?> scriptClass) throws ScriptCompilationException

Instantiate the given Groovy script class and run it if necessary.

Parameters
scriptSource:ScriptSource

the source for the underlying script

scriptClass:Class<?>

the Groovy script class

Returns:Object

the result object (either an instance of the script class or the result of running the script instance)

Annotations
@Nullable
Exceptions
ScriptCompilationException:
in case of instantiation failure
getGroovyClassLoaderback to summary
public GroovyClassLoader getGroovyClassLoader()

Return the GroovyClassLoader used by this script factory.

getScriptedObjectback to summary
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) throws IOException, ScriptCompilationException

Implements org.springframework.scripting.ScriptFactory.getScriptedObject.

Loads and parses the Groovy script via the GroovyClassLoader.

Parameters
scriptSource:ScriptSource

Doc from org.springframework.scripting.ScriptFactory.getScriptedObject.

the actual ScriptSource to retrieve the script source text from (never null)

actualInterfaces:Class<?>[]

Doc from org.springframework.scripting.ScriptFactory.getScriptedObject.

the actual interfaces to expose, including script interfaces as well as a generated config interface (if applicable; may be null)

Returns:Object

Doc from org.springframework.scripting.ScriptFactory.getScriptedObject.

the scripted Java object

Annotations
@Override
@Nullable
Exceptions
IOException:

Doc from org.springframework.scripting.ScriptFactory.getScriptedObject.

if script retrieval failed

ScriptCompilationException:

Doc from org.springframework.scripting.ScriptFactory.getScriptedObject.

if script compilation failed

See Also
groovy.lang.GroovyClassLoader
getScriptedObjectTypeback to summary
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException

Implements org.springframework.scripting.ScriptFactory.getScriptedObjectType.

Doc from org.springframework.scripting.ScriptFactory.getScriptedObjectType.

Determine the type of the scripted Java object.

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.

Parameters
scriptSource:ScriptSource

the actual ScriptSource to retrieve the script source text from (never null)

Returns:Class<?>

the type of the scripted Java object, or null if none could be determined

Annotations
@Override
@Nullable
Exceptions
IOException:
if script retrieval failed
ScriptCompilationException:
if script compilation failed
getScriptInterfacesback to summary
public Class<?>[] getScriptInterfaces()

Implements org.springframework.scripting.ScriptFactory.getScriptInterfaces.

Groovy scripts determine their interfaces themselves, hence we don't need to explicitly expose interfaces here.

Returns:Class<?>[]

null always

Annotations
@Override
@Nullable
getScriptSourceLocatorback to summary
public String getScriptSourceLocator()

Implements org.springframework.scripting.ScriptFactory.getScriptSourceLocator.

Doc from org.springframework.scripting.ScriptFactory.getScriptSourceLocator.

Return a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.

Typical supported locators are Spring resource locations (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") and inline scripts ("inline:myScriptText...").

Returns:String

the script source locator

Annotations
@Override
requiresConfigInterfaceback to summary
public boolean requiresConfigInterface()

Implements org.springframework.scripting.ScriptFactory.requiresConfigInterface.

Groovy scripts do not need a config interface, since they expose their setters as public methods.

Returns:boolean

Doc from org.springframework.scripting.ScriptFactory.requiresConfigInterface.

whether the script requires a generated config interface

Annotations
@Override
requiresScriptedObjectRefreshback to summary
public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource)

Implements org.springframework.scripting.ScriptFactory.requiresScriptedObjectRefresh.

Doc from org.springframework.scripting.ScriptFactory.requiresScriptedObjectRefresh.

Determine whether a refresh is required (e.g. through ScriptSource's isModified() method).

Parameters
scriptSource:ScriptSource

the actual ScriptSource to retrieve the script source text from (never null)

Returns:boolean

whether a fresh getScriptedObject call is required

Annotations
@Override
setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean 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.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.scripting.groovy back to summary

private Class GroovyScriptFactory.CachedResultHolder

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.scripting.groovy.GroovyScriptFactory.CachedResultHolder

Wrapper that holds a temporarily cached result object.

Field Summary

Modifier and TypeField and Description
public final Object

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

objectback to summary
public final Object object
Annotations
@Nullable

Constructor Detail

CachedResultHolderback to summary
public CachedResultHolder(Object object)