Create ScriptEngine per execution
This commit is contained in:
@@ -23,18 +23,23 @@ import javax.script.ScriptException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.scripting.ScriptExecutor;
|
||||
import org.springframework.scripting.ScriptSource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Executes JSR223 scripts
|
||||
*
|
||||
* @author David Turanski
|
||||
* @author Mark Fisher
|
||||
* @since 2.1
|
||||
*/
|
||||
public class DefaultScriptExecutor implements ScriptExecutor {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DefaultScriptExecutor.class);
|
||||
|
||||
static {
|
||||
if (ClassUtils.isPresent("org.jruby.embed.jsr223.JRubyEngine", System.class.getClassLoader())) {
|
||||
System.setProperty("org.jruby.embed.localvariable.behavior", "transient");
|
||||
@@ -42,50 +47,46 @@ public class DefaultScriptExecutor implements ScriptExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private static Log log = LogFactory.getLog(DefaultScriptExecutor.class);
|
||||
|
||||
private final ScriptEngine scriptEngine;
|
||||
private final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
|
||||
|
||||
private final String language;
|
||||
|
||||
|
||||
/**
|
||||
* Set the language name (JSR233 alias)
|
||||
* @param language
|
||||
* Create a DefaultScriptExceutor for the specified language name (JSR233 alias).
|
||||
*/
|
||||
public DefaultScriptExecutor(String language) {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
|
||||
scriptEngine = manager.getEngineByName(language);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("using script engine : " + scriptEngine.getFactory().getEngineName());
|
||||
Assert.hasText(language, "language must not be empty");
|
||||
this.language = language;
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("using script engine : " + scriptEngineManager.getEngineByName(language).getFactory().getEngineName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object executeScript(ScriptSource scriptSource) {
|
||||
return this.executeScript(scriptSource, null);
|
||||
}
|
||||
|
||||
public synchronized Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
|
||||
Object obj = null;
|
||||
public Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
|
||||
Object result = null;
|
||||
ScriptEngine scriptEngine = this.scriptEngineManager.getEngineByName(this.language);
|
||||
try {
|
||||
|
||||
if (variables != null) {
|
||||
for (Entry<String, Object> entry : variables.entrySet()) {
|
||||
scriptEngine.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
String script = scriptSource.getScriptAsString();
|
||||
|
||||
Date start = new Date();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("executing script: " + script);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("executing script: " + script);
|
||||
}
|
||||
|
||||
obj = scriptEngine.eval(script);
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("script executed in " + (new Date().getTime() - start.getTime()) + " ms");
|
||||
result = scriptEngine.eval(script);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("script executed in " + (new Date().getTime() - start.getTime()) + " ms");
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -93,8 +94,7 @@ public class DefaultScriptExecutor implements ScriptExecutor {
|
||||
catch (ScriptException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return obj;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user