Bsh/GroovyScriptFactory reset script cache in case of compilation error

Issue: SPR-14007
This commit is contained in:
Juergen Hoeller 2016-03-11 12:55:20 +01:00
parent b944283354
commit 0597ff109e
2 changed files with 49 additions and 38 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -119,9 +119,9 @@ public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware {
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
throws IOException, ScriptCompilationException { throws IOException, ScriptCompilationException {
try {
Class<?> clazz; Class<?> clazz;
try {
synchronized (this.scriptClassMonitor) { synchronized (this.scriptClassMonitor) {
boolean requiresScriptEvaluation = (this.wasModifiedForTypeCheck && this.scriptClass == null); boolean requiresScriptEvaluation = (this.wasModifiedForTypeCheck && this.scriptClass == null);
this.wasModifiedForTypeCheck = false; this.wasModifiedForTypeCheck = false;
@ -145,6 +145,11 @@ public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware {
} }
clazz = this.scriptClass; clazz = this.scriptClass;
} }
}
catch (EvalError ex) {
this.scriptClass = null;
throw new ScriptCompilationException(scriptSource, ex);
}
if (clazz != null) { if (clazz != null) {
// A Class: We need to create an instance for every call. // A Class: We need to create an instance for every call.
@ -158,21 +163,22 @@ public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware {
} }
else { else {
// Not a Class: We need to evaluate the script for every call. // Not a Class: We need to evaluate the script for every call.
try {
return BshScriptUtils.createBshObject( return BshScriptUtils.createBshObject(
scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader); scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
} }
}
catch (EvalError ex) { catch (EvalError ex) {
throw new ScriptCompilationException(scriptSource, ex); throw new ScriptCompilationException(scriptSource, ex);
} }
} }
}
@Override @Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource) public Class<?> getScriptedObjectType(ScriptSource scriptSource)
throws IOException, ScriptCompilationException { throws IOException, ScriptCompilationException {
try {
synchronized (this.scriptClassMonitor) { synchronized (this.scriptClassMonitor) {
try {
if (scriptSource.isModified()) { if (scriptSource.isModified()) {
// New script content: Let's check whether it evaluates to a Class. // New script content: Let's check whether it evaluates to a Class.
this.wasModifiedForTypeCheck = true; this.wasModifiedForTypeCheck = true;
@ -181,11 +187,12 @@ public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware {
} }
return this.scriptClass; return this.scriptClass;
} }
}
catch (EvalError ex) { catch (EvalError ex) {
this.scriptClass = null;
throw new ScriptCompilationException(scriptSource, ex); throw new ScriptCompilationException(scriptSource, ex);
} }
} }
}
@Override @Override
public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -158,10 +158,9 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces) public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
throws IOException, ScriptCompilationException { throws IOException, ScriptCompilationException {
synchronized (this.scriptClassMonitor) {
try { try {
Class<?> scriptClassToExecute; Class<?> scriptClassToExecute;
synchronized (this.scriptClassMonitor) {
this.wasModifiedForTypeCheck = false; this.wasModifiedForTypeCheck = false;
if (this.cachedResult != null) { if (this.cachedResult != null) {
@ -186,22 +185,24 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
} }
} }
scriptClassToExecute = this.scriptClass; scriptClassToExecute = this.scriptClass;
}
// Process re-execution outside of the synchronized block. // Process re-execution outside of the synchronized block.
return executeScript(scriptSource, scriptClassToExecute); return executeScript(scriptSource, scriptClassToExecute);
} }
catch (CompilationFailedException ex) { catch (CompilationFailedException ex) {
this.scriptClass = null;
this.scriptResultClass = null;
throw new ScriptCompilationException(scriptSource, ex); throw new ScriptCompilationException(scriptSource, ex);
} }
} }
}
@Override @Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource) public Class<?> getScriptedObjectType(ScriptSource scriptSource)
throws IOException, ScriptCompilationException { throws IOException, ScriptCompilationException {
try {
synchronized (this.scriptClassMonitor) { synchronized (this.scriptClassMonitor) {
try {
if (this.scriptClass == null || scriptSource.isModified()) { if (this.scriptClass == null || scriptSource.isModified()) {
// New script content... // New script content...
this.wasModifiedForTypeCheck = true; this.wasModifiedForTypeCheck = true;
@ -220,11 +221,14 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
} }
return this.scriptResultClass; return this.scriptResultClass;
} }
}
catch (CompilationFailedException ex) { catch (CompilationFailedException ex) {
this.scriptClass = null;
this.scriptResultClass = null;
this.cachedResult = null;
throw new ScriptCompilationException(scriptSource, ex); throw new ScriptCompilationException(scriptSource, ex);
} }
} }
}
@Override @Override
public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) {