@ -17,12 +17,15 @@
@@ -17,12 +17,15 @@
package org.springframework.scripting.groovy ;
import java.io.IOException ;
import java.lang.reflect.InvocationTargetException ;
import groovy.lang.GroovyClassLoader ;
import groovy.lang.GroovyObject ;
import groovy.lang.MetaClass ;
import groovy.lang.Script ;
import org.codehaus.groovy.control.CompilationFailedException ;
import org.codehaus.groovy.control.CompilerConfiguration ;
import org.codehaus.groovy.control.customizers.CompilationCustomizer ;
import org.springframework.beans.factory.BeanClassLoaderAware ;
import org.springframework.beans.factory.BeanFactory ;
@ -33,6 +36,8 @@ import org.springframework.scripting.ScriptFactory;
@@ -33,6 +36,8 @@ import org.springframework.scripting.ScriptFactory;
import org.springframework.scripting.ScriptSource ;
import org.springframework.util.Assert ;
import org.springframework.util.ClassUtils ;
import org.springframework.util.ObjectUtils ;
import org.springframework.util.ReflectionUtils ;
/ * *
* { @link org . springframework . scripting . ScriptFactory } implementation
@ -55,7 +60,9 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
@@ -55,7 +60,9 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
private final String scriptSourceLocator ;
private final GroovyObjectCustomizer groovyObjectCustomizer ;
private GroovyObjectCustomizer groovyObjectCustomizer ;
private CompilerConfiguration compilerConfiguration ;
private GroovyClassLoader groovyClassLoader ;
@ -78,27 +85,62 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
@@ -78,27 +85,62 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
* Interpreted by the post - processor that actually creates the script .
* /
public GroovyScriptFactory ( String scriptSourceLocator ) {
this ( scriptSourceLocator , null ) ;
Assert . hasText ( scriptSourceLocator , "'scriptSourceLocator' must not be empty" ) ;
this . scriptSourceLocator = scriptSourceLocator ;
}
/ * *
* 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 .
* < p > We don ' t need to specify script interfaces here , since
* a Groovy script defines its Java interfaces itself .
* @param scriptSourceLocator a locator that points to the source of the script .
* Interpreted by the post - processor that actually creates the script .
* @param groovyObjectCustomizer a customizer that can set a custom metaclass
* or make other changes to the GroovyObject created by this factory
* ( may be { @code null } )
* @see GroovyObjectCustomizer # customize
* /
public GroovyScriptFactory ( String scriptSourceLocator , GroovyObjectCustomizer groovyObjectCustomizer ) {
Assert . hasText ( scriptSourceLocator , "'scriptSourceLocator' must not be empty" ) ;
this . scriptSourceLocator = scriptSourceLocator ;
this ( scriptSourceLocator ) ;
this . 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 .
* @param scriptSourceLocator a locator that points to the source of the script .
* Interpreted by the post - processor that actually creates the script .
* @param compilerConfiguration a custom compiler configuration to be applied
* to the GroovyClassLoader ( may be { @code null } )
* @since 4 . 3 . 3
* @see GroovyClassLoader # GroovyClassLoader ( ClassLoader , CompilerConfiguration )
* /
public GroovyScriptFactory ( String scriptSourceLocator , CompilerConfiguration compilerConfiguration ) {
this ( scriptSourceLocator ) ;
this . compilerConfiguration = compilerConfiguration ;
}
/ * *
* Create a new GroovyScriptFactory for the given script source ,
* specifying a strategy interface that can customize Groovy ' s compilation
* process within the underlying GroovyClassLoader .
* @param scriptSourceLocator a locator that points to the source of the script .
* Interpreted by the post - processor that actually creates the script .
* @param compilationCustomizers one or more customizers to be applied to the
* GroovyClassLoader compiler configuration
* @since 4 . 3 . 3
* @see CompilerConfiguration # addCompilationCustomizers
* @see org . codehaus . groovy . control . customizers . ImportCustomizer
* /
public GroovyScriptFactory ( String scriptSourceLocator , CompilationCustomizer . . . compilationCustomizers ) {
this ( scriptSourceLocator ) ;
if ( ! ObjectUtils . isEmpty ( compilationCustomizers ) ) {
this . compilerConfiguration = new CompilerConfiguration ( ) ;
this . compilerConfiguration . addCompilationCustomizers ( compilationCustomizers ) ;
}
}
@Override
public void setBeanFactory ( BeanFactory beanFactory ) {
@ -109,7 +151,7 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
@@ -109,7 +151,7 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
@Override
public void setBeanClassLoader ( ClassLoader classLoader ) {
this . groovyClassLoader = new GroovyClassLoader ( classLoader ) ;
this . groovyClassLoader = build GroovyClassLoader( classLoader ) ;
}
/ * *
@ -118,12 +160,22 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
@@ -118,12 +160,22 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
public GroovyClassLoader getGroovyClassLoader ( ) {
synchronized ( this . scriptClassMonitor ) {
if ( this . groovyClassLoader = = null ) {
this . groovyClassLoader = new GroovyClassLoader ( ClassUtils . getDefaultClassLoader ( ) ) ;
this . groovyClassLoader = build GroovyClassLoader( ClassUtils . getDefaultClassLoader ( ) ) ;
}
return this . groovyClassLoader ;
}
}
/ * *
* Build a { @link GroovyClassLoader } for the given { @code ClassLoader } .
* @param classLoader the ClassLoader to build a GroovyClassLoader for
* @since 4 . 3 . 3
* /
protected GroovyClassLoader buildGroovyClassLoader ( ClassLoader classLoader ) {
return ( this . compilerConfiguration ! = null ?
new GroovyClassLoader ( classLoader , this . compilerConfiguration ) : new GroovyClassLoader ( classLoader ) ) ;
}
@Override
public String getScriptSourceLocator ( ) {