Class GroovyScriptFactory

java.lang.Object
org.springframework.scripting.groovy.GroovyScriptFactory
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, ScriptFactory

public class GroovyScriptFactory extends Object implements ScriptFactory, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.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.

Since:
2.0
Author:
Juergen Hoeller, Rob Harrop, Rod Johnson
See Also:
  • Constructor Details

    • GroovyScriptFactory

      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 - a locator that points to the source of the script. Interpreted by the post-processor that actually creates the script.
    • GroovyScriptFactory

      public GroovyScriptFactory(String scriptSourceLocator, @Nullable 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 - 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)
      See Also:
    • GroovyScriptFactory

      public GroovyScriptFactory(String scriptSourceLocator, @Nullable 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.
      Parameters:
      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)
      Since:
      4.3.3
      See Also:
      • GroovyClassLoader(ClassLoader, CompilerConfiguration)
    • GroovyScriptFactory

      public 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.
      Parameters:
      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 configuration
      Since:
      4.3.3
      See Also:
      • CompilerConfiguration.addCompilationCustomizers(org.codehaus.groovy.control.customizers.CompilationCustomizer...)
      • ImportCustomizer
  • Method Details

    • setBeanFactory

      public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
    • setBeanClassLoader

      public void setBeanClassLoader(ClassLoader classLoader)
      Specified by:
      setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware
    • getGroovyClassLoader

      public groovy.lang.GroovyClassLoader getGroovyClassLoader()
      Return the GroovyClassLoader used by this script factory.
    • buildGroovyClassLoader

      protected groovy.lang.GroovyClassLoader buildGroovyClassLoader(@Nullable ClassLoader classLoader)
      Build a GroovyClassLoader for the given ClassLoader.
      Parameters:
      classLoader - the ClassLoader to build a GroovyClassLoader for
      Since:
      4.3.3
    • getScriptSourceLocator

      public String getScriptSourceLocator()
      Description copied from interface: ScriptFactory
      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...").

      Specified by:
      getScriptSourceLocator in interface ScriptFactory
      Returns:
      the script source locator
      See Also:
    • getScriptInterfaces

      @Nullable public Class<?>[] getScriptInterfaces()
      Groovy scripts determine their interfaces themselves, hence we don't need to explicitly expose interfaces here.
      Specified by:
      getScriptInterfaces in interface ScriptFactory
      Returns:
      null always
    • requiresConfigInterface

      public boolean requiresConfigInterface()
      Groovy scripts do not need a config interface, since they expose their setters as public methods.
      Specified by:
      requiresConfigInterface in interface ScriptFactory
      Returns:
      whether the script requires a generated config interface
      See Also:
    • getScriptedObject

      @Nullable public Object getScriptedObject(ScriptSource scriptSource, @Nullable Class<?>... actualInterfaces) throws IOException, ScriptCompilationException
      Loads and parses the Groovy script via the GroovyClassLoader.
      Specified by:
      getScriptedObject in interface ScriptFactory
      Parameters:
      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)
      Returns:
      the scripted Java object
      Throws:
      IOException - if script retrieval failed
      ScriptCompilationException - if script compilation failed
      See Also:
      • GroovyClassLoader
    • getScriptedObjectType

      @Nullable public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException
      Description copied from interface: ScriptFactory
      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.

      Specified by:
      getScriptedObjectType in interface ScriptFactory
      Parameters:
      scriptSource - the actual ScriptSource to retrieve the script source text from (never null)
      Returns:
      the type of the scripted Java object, or null if none could be determined
      Throws:
      IOException - if script retrieval failed
      ScriptCompilationException - if script compilation failed
    • requiresScriptedObjectRefresh

      public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource)
      Description copied from interface: ScriptFactory
      Determine whether a refresh is required (e.g. through ScriptSource's isModified() method).
      Specified by:
      requiresScriptedObjectRefresh in interface ScriptFactory
      Parameters:
      scriptSource - the actual ScriptSource to retrieve the script source text from (never null)
      Returns:
      whether a fresh ScriptFactory.getScriptedObject(org.springframework.scripting.ScriptSource, java.lang.Class<?>...) call is required
      See Also:
    • executeScript

      @Nullable protected Object executeScript(ScriptSource scriptSource, Class<?> scriptClass) throws ScriptCompilationException
      Instantiate the given Groovy script class and run it if necessary.
      Parameters:
      scriptSource - the source for the underlying script
      scriptClass - the Groovy script class
      Returns:
      the result object (either an instance of the script class or the result of running the script instance)
      Throws:
      ScriptCompilationException - in case of instantiation failure
    • toString

      public String toString()
      Overrides:
      toString in class Object