Skip to content

Commit 433764d

Browse files
committed
GroovyScriptFactory supports CompilationCustomizer next to GroovyObjectCustomizer
Issue: SPR-14585
1 parent 214c919 commit 433764d

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import groovy.lang.MetaClass;
2525
import groovy.lang.Script;
2626
import org.codehaus.groovy.control.CompilationFailedException;
27+
import org.codehaus.groovy.control.CompilerConfiguration;
28+
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
2729

2830
import org.springframework.beans.factory.BeanClassLoaderAware;
2931
import org.springframework.beans.factory.BeanFactory;
@@ -57,7 +59,9 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
5759

5860
private final String scriptSourceLocator;
5961

60-
private final GroovyObjectCustomizer groovyObjectCustomizer;
62+
private GroovyObjectCustomizer groovyObjectCustomizer;
63+
64+
private CompilerConfiguration compilerConfiguration;
6165

6266
private GroovyClassLoader groovyClassLoader;
6367

@@ -80,27 +84,46 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
8084
* Interpreted by the post-processor that actually creates the script.
8185
*/
8286
public GroovyScriptFactory(String scriptSourceLocator) {
83-
this(scriptSourceLocator, null);
87+
Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
88+
this.scriptSourceLocator = scriptSourceLocator;
8489
}
8590

8691
/**
8792
* Create a new GroovyScriptFactory for the given script source,
8893
* specifying a strategy interface that can create a custom MetaClass
8994
* to supply missing methods and otherwise change the behavior of the object.
90-
* <p>We don't need to specify script interfaces here, since
91-
* a Groovy script defines its Java interfaces itself.
9295
* @param scriptSourceLocator a locator that points to the source of the script.
9396
* Interpreted by the post-processor that actually creates the script.
9497
* @param groovyObjectCustomizer a customizer that can set a custom metaclass
9598
* or make other changes to the GroovyObject created by this factory
9699
* (may be {@code null})
100+
* @see GroovyObjectCustomizer#customize
97101
*/
98102
public GroovyScriptFactory(String scriptSourceLocator, GroovyObjectCustomizer groovyObjectCustomizer) {
99-
Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
100-
this.scriptSourceLocator = scriptSourceLocator;
103+
this(scriptSourceLocator);
101104
this.groovyObjectCustomizer = groovyObjectCustomizer;
102105
}
103106

107+
/**
108+
* Create a new GroovyScriptFactory for the given script source,
109+
* specifying a strategy interface that can customize Groovy's compilation
110+
* process within the underlying GroovyClassLoader.
111+
* @param scriptSourceLocator a locator that points to the source of the script.
112+
* Interpreted by the post-processor that actually creates the script.
113+
* @param compilationCustomizer a customizer to be applied to the GroovyClassLoader
114+
* compiler configuration (may be {@code null})
115+
* @since 4.3.3
116+
* @see CompilerConfiguration#addCompilationCustomizers
117+
* @see org.codehaus.groovy.control.customizers.ImportCustomizer
118+
*/
119+
public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer compilationCustomizer) {
120+
this(scriptSourceLocator);
121+
if (compilationCustomizer != null) {
122+
this.compilerConfiguration = new CompilerConfiguration();
123+
this.compilerConfiguration.addCompilationCustomizers(compilationCustomizer);
124+
}
125+
}
126+
104127

105128
@Override
106129
public void setBeanFactory(BeanFactory beanFactory) {
@@ -111,7 +134,7 @@ public void setBeanFactory(BeanFactory beanFactory) {
111134

112135
@Override
113136
public void setBeanClassLoader(ClassLoader classLoader) {
114-
this.groovyClassLoader = new GroovyClassLoader(classLoader);
137+
this.groovyClassLoader = buildGroovyClassLoader(classLoader);
115138
}
116139

117140
/**
@@ -120,12 +143,22 @@ public void setBeanClassLoader(ClassLoader classLoader) {
120143
public GroovyClassLoader getGroovyClassLoader() {
121144
synchronized (this.scriptClassMonitor) {
122145
if (this.groovyClassLoader == null) {
123-
this.groovyClassLoader = new GroovyClassLoader(ClassUtils.getDefaultClassLoader());
146+
this.groovyClassLoader = buildGroovyClassLoader(ClassUtils.getDefaultClassLoader());
124147
}
125148
return this.groovyClassLoader;
126149
}
127150
}
128151

152+
/**
153+
* Build a {@link GroovyClassLoader} for the given {@code ClassLoader}.
154+
* @param classLoader the ClassLoader to build a GroovyClassLoader for
155+
* @since 4.3.3
156+
*/
157+
protected GroovyClassLoader buildGroovyClassLoader(ClassLoader classLoader) {
158+
return (this.compilerConfiguration != null ?
159+
new GroovyClassLoader(classLoader, this.compilerConfiguration) : new GroovyClassLoader(classLoader));
160+
}
161+
129162

130163
@Override
131164
public String getScriptSourceLocator() {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scripting.groovy;
18+
19+
import org.codehaus.groovy.control.customizers.ImportCustomizer;
20+
21+
/**
22+
* @author Juergen Hoeller
23+
*/
24+
public class MyImportCustomizer extends ImportCustomizer {
25+
26+
public MyImportCustomizer() {
27+
addStarImports("org.springframework.scripting.groovy");
28+
}
29+
30+
}

spring-context/src/test/resources/org/springframework/scripting/groovy/groovy-with-xsd.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xmlns:aop="http://www.springframework.org/schema/aop"
44
xmlns:lang="http://www.springframework.org/schema/lang"
5-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
66
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
77
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
88

@@ -24,7 +24,7 @@
2424
<lang:property name="message" value="Hello World!"/>
2525
</lang:groovy>
2626

27-
<lang:groovy id="calculator" depends-on="messenger" customizer-ref="customizer">
27+
<lang:groovy id="calculator" depends-on="messenger" customizer-ref="groovyObjectCustomizer">
2828
<lang:inline-script>
2929
package org.springframework.scripting.groovy;
3030
import org.springframework.scripting.Calculator
@@ -36,10 +36,8 @@ class GroovyCalculator implements Calculator {
3636
</lang:inline-script>
3737
</lang:groovy>
3838

39-
<lang:groovy id="customizer">
39+
<lang:groovy id="groovyObjectCustomizer" customizer-ref="importCustomizer">
4040
<lang:inline-script><![CDATA[
41-
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
42-
4341
public class TestCustomizer implements GroovyObjectCustomizer {
4442
public void customize(GroovyObject o) {
4543
println "customizing ${o}.."
@@ -48,13 +46,15 @@ public class TestCustomizer implements GroovyObjectCustomizer {
4846
</lang:inline-script>
4947
</lang:groovy>
5048

49+
<bean id="importCustomizer" class="org.springframework.scripting.groovy.MyImportCustomizer"/>
50+
5151
<lang:groovy id="refreshableMessenger" refresh-check-delay="5000"
5252
script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
5353
<lang:property name="message" value="Hello World!"/>
5454
</lang:groovy>
5555

56-
<lang:groovy script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
57-
<lang:property name="message" value="Hello World!"/>
58-
</lang:groovy>
56+
<lang:groovy script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
57+
<lang:property name="message" value="Hello World!"/>
58+
</lang:groovy>
5959

6060
</beans>

src/asciidoc/integration.adoc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7639,7 +7639,6 @@ set some default property values, or specify a custom `MetaClass`.
76397639
public interface GroovyObjectCustomizer {
76407640
76417641
void customize(GroovyObject goo);
7642-
76437642
}
76447643
----
76457644

@@ -7678,12 +7677,12 @@ of a `GroovyObjectCustomizer` is easy if you are using the Spring namespace supp
76787677
[subs="verbatim,quotes"]
76797678
----
76807679
<!-- define the GroovyObjectCustomizer just like any other bean -->
7681-
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer" />
7680+
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer"/>
76827681
76837682
<!-- ... and plug it into the desired Groovy bean via the 'customizer-ref' attribute -->
76847683
<lang:groovy id="calculator"
76857684
script-source="classpath:org/springframework/scripting/groovy/Calculator.groovy"
7686-
customizer-ref="tracingCustomizer" />
7685+
customizer-ref="tracingCustomizer"/>
76877686
----
76887687

76897688
If you are not using the Spring namespace support, you can still use the
@@ -7696,13 +7695,19 @@ If you are not using the Spring namespace support, you can still use the
76967695
<constructor-arg value="classpath:org/springframework/scripting/groovy/Calculator.groovy"/>
76977696
<!-- define the GroovyObjectCustomizer (as an inner bean) -->
76987697
<constructor-arg>
7699-
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer" />
7698+
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer"/>
77007699
</constructor-arg>
77017700
</bean>
77027701
77037702
<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/>
77047703
----
77057704

7705+
[NOTE]
7706+
====
7707+
As of Spring Framework 4.3.3, you may also specify a Groovy `CompilationCustomizer` type
7708+
such as an `ImportCustomizer` in the same place as Spring's `GroovyObjectCustomizer`.
7709+
====
7710+
77067711

77077712

77087713
[[dynamic-language-beans-bsh]]

0 commit comments

Comments
 (0)