Browse Source

Bsh/GroovyScriptFactory reset script cache in case of compilation error

Issue: SPR-14007
pull/988/merge
Juergen Hoeller 9 years ago
parent
commit
0597ff109e
  1. 17
      spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptFactory.java
  2. 16
      spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java

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

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

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

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

Loading…
Cancel
Save