Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripted beans not updated after a script compilation error in getScriptedObjectType [SPR-14007] #18579

Closed
spring-issuemaster opened this issue Mar 1, 2016 · 2 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Mar 1, 2016

Anton Rokhmistrov opened SPR-14007 and commented

This is a duplicate of #9034.
It does not work again because "ScriptFactory.requiresScriptedObjectRefresh(ScriptSource)" is not used in "GroovyScriptFactory#getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)"


Affects: 3.2.16, 4.2.2, 4.2.5

Issue Links:

  • #9034 Refreshable beans are not updated if AbstractApplicationContext.getBeansOfType() is called

Referenced from: commits c2eb5e1, 0597ff1, 05ab769

Backported to: 3.2.17

2 votes, 4 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 2, 2016

Anton Rokhmistrov commented

I think come clarification is needed because it's not exactly the duplicate.

The problem happens when groovy script is invalid (syntax error etc.). Then result of a call to the groovy bean is unpredictable, it can execute old version of the script or throw an exception depending on whether AbstractApplicationContext.getBeansOfType() was called or not.

Example scenario:
Call to getScriptedObjectType

  1. scriptSource.lastModified is updated
  2. getGroovyClassLoader().parseClass throws CompilationException [1]
  3. this.scriptClass is not updated

Call to getScriptedObject

  1. scriptSource.isModified() return false [2]
  2. returns outdated class [3]
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException {
....
if (this.scriptClass == null || scriptSource.isModified()) {
	// New script content...
	this.wasModifiedForTypeCheck = true;
	this.scriptClass = getGroovyClassLoader().parseClass( [1]
				scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

	if (Script.class.isAssignableFrom(this.scriptClass)) {
			// A Groovy script, probably creating an instance: let's execute it.
			Object result = executeScript(scriptSource, this.scriptClass);
			this.scriptResultClass = (result != null ? result.getClass() : null);
			this.cachedResult = new CachedResultHolder(result);
	}
	else {
		this.scriptResultClass = this.scriptClass;
	}
}
....
}
@Override
public Object getScriptedObject(ScriptSource scriptSource, Class<?>... actualInterfaces)
			throws IOException, ScriptCompilationException {
    if (this.scriptClass == null || scriptSource.isModified()) { [2]
       // New script content...
       this.scriptClass = getGroovyClassLoader().parseClass(
			scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

	if (Script.class.isAssignableFrom(this.scriptClass)) {
	        // A Groovy script, probably creating an instance: let's execute it.
		Object result = executeScript(scriptSource, this.scriptClass);
		this.scriptResultClass = (result != null ? result.getClass() : null);
		return result;
	}
	else {
		this.scriptResultClass = this.scriptClass;
	}
	}
	scriptClassToExecute = this.scriptClass; [3]
}
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 10, 2016

Juergen Hoeller commented

Good point: We're resetting the scriptClass, scriptResultClass and cachedResult fields in GroovyScriptFactorty when we run into a CompilationFailedException now, both in getScriptedObject and getScriptedObjectType. Analogously in BshScriptFactory as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.