Skip to content

Commit c2eb5e1

Browse files
committed
Bsh/GroovyScriptFactory reset script cache in case of compilation error
Issue: SPR-14007 (cherry picked from commit 05ab769)
1 parent 4fe386d commit c2eb5e1

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptFactory.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -114,9 +114,9 @@ public boolean requiresConfigInterface() {
114114
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
115115
throws IOException, ScriptCompilationException {
116116

117-
try {
118-
Class<?> clazz;
117+
Class<?> clazz;
119118

119+
try {
120120
synchronized (this.scriptClassMonitor) {
121121
boolean requiresScriptEvaluation = (this.wasModifiedForTypeCheck && this.scriptClass == null);
122122
this.wasModifiedForTypeCheck = false;
@@ -140,43 +140,50 @@ public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInt
140140
}
141141
clazz = this.scriptClass;
142142
}
143+
}
144+
catch (EvalError ex) {
145+
this.scriptClass = null;
146+
throw new ScriptCompilationException(scriptSource, ex);
147+
}
143148

144-
if (clazz != null) {
145-
// A Class: We need to create an instance for every call.
146-
try {
147-
return clazz.newInstance();
148-
}
149-
catch (Throwable ex) {
150-
throw new ScriptCompilationException(
151-
scriptSource, "Could not instantiate script class: " + clazz.getName(), ex);
152-
}
149+
if (clazz != null) {
150+
// A Class: We need to create an instance for every call.
151+
try {
152+
return clazz.newInstance();
153+
}
154+
catch (Throwable ex) {
155+
throw new ScriptCompilationException(
156+
scriptSource, "Could not instantiate script class: " + clazz.getName(), ex);
153157
}
154-
else {
155-
// Not a Class: We need to evaluate the script for every call.
158+
}
159+
else {
160+
// Not a Class: We need to evaluate the script for every call.
161+
try {
156162
return BshScriptUtils.createBshObject(
157163
scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
158164
}
159-
}
160-
catch (EvalError ex) {
161-
throw new ScriptCompilationException(scriptSource, ex);
165+
catch (EvalError ex) {
166+
throw new ScriptCompilationException(scriptSource, ex);
167+
}
162168
}
163169
}
164170

165171
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
166172
throws IOException, ScriptCompilationException {
167173

168-
try {
169-
synchronized (this.scriptClassMonitor) {
174+
synchronized (this.scriptClassMonitor) {
175+
try {
170176
if (scriptSource.isModified()) {
171177
// New script content: Let's check whether it evaluates to a Class.
172178
this.wasModifiedForTypeCheck = true;
173179
this.scriptClass = BshScriptUtils.determineBshObjectType(scriptSource.getScriptAsString());
174180
}
175181
return this.scriptClass;
176182
}
177-
}
178-
catch (EvalError ex) {
179-
throw new ScriptCompilationException(scriptSource, ex);
183+
catch (EvalError ex) {
184+
this.scriptClass = null;
185+
throw new ScriptCompilationException(scriptSource, ex);
186+
}
180187
}
181188
}
182189

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -150,10 +150,9 @@ public boolean requiresConfigInterface() {
150150
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
151151
throws IOException, ScriptCompilationException {
152152

153-
try {
154-
Class<?> scriptClassToExecute;
155-
156-
synchronized (this.scriptClassMonitor) {
153+
synchronized (this.scriptClassMonitor) {
154+
try {
155+
Class<?> scriptClassToExecute;
157156
this.wasModifiedForTypeCheck = false;
158157

159158
if (this.cachedResult != null) {
@@ -178,21 +177,23 @@ public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInt
178177
}
179178
}
180179
scriptClassToExecute = this.scriptClass;
181-
}
182180

183-
// Process re-execution outside of the synchronized block.
184-
return executeScript(scriptSource, scriptClassToExecute);
185-
}
186-
catch (CompilationFailedException ex) {
187-
throw new ScriptCompilationException(scriptSource, ex);
181+
// Process re-execution outside of the synchronized block.
182+
return executeScript(scriptSource, scriptClassToExecute);
183+
}
184+
catch (CompilationFailedException ex) {
185+
this.scriptClass = null;
186+
this.scriptResultClass = null;
187+
throw new ScriptCompilationException(scriptSource, ex);
188+
}
188189
}
189190
}
190191

191192
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
192193
throws IOException, ScriptCompilationException {
193194

194-
try {
195-
synchronized (this.scriptClassMonitor) {
195+
synchronized (this.scriptClassMonitor) {
196+
try {
196197
if (this.scriptClass == null || scriptSource.isModified()) {
197198
// New script content...
198199
this.wasModifiedForTypeCheck = true;
@@ -211,9 +212,12 @@ public Class<?> getScriptedObjectType(ScriptSource scriptSource)
211212
}
212213
return this.scriptResultClass;
213214
}
214-
}
215-
catch (CompilationFailedException ex) {
216-
throw new ScriptCompilationException(scriptSource, ex);
215+
catch (CompilationFailedException ex) {
216+
this.scriptClass = null;
217+
this.scriptResultClass = null;
218+
this.cachedResult = null;
219+
throw new ScriptCompilationException(scriptSource, ex);
220+
}
217221
}
218222
}
219223

0 commit comments

Comments
 (0)