Script engines now unloaded if replaced, and closed if AutoCloseable (#2681)
Signed-off-by: Jonathan Gilbert <jpg@trillica.com>pull/2668/head
parent
d693302190
commit
22c28595ab
|
@ -122,6 +122,11 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager {
|
||||||
public @Nullable ScriptEngineContainer createScriptEngine(String scriptType, String engineIdentifier) {
|
public @Nullable ScriptEngineContainer createScriptEngine(String scriptType, String engineIdentifier) {
|
||||||
ScriptEngineContainer result = null;
|
ScriptEngineContainer result = null;
|
||||||
ScriptEngineFactory engineFactory = findEngineFactory(scriptType);
|
ScriptEngineFactory engineFactory = findEngineFactory(scriptType);
|
||||||
|
|
||||||
|
if (loadedScriptEngineInstances.containsKey(engineIdentifier)) {
|
||||||
|
removeEngine(engineIdentifier);
|
||||||
|
}
|
||||||
|
|
||||||
if (engineFactory == null) {
|
if (engineFactory == null) {
|
||||||
logger.error("ScriptEngine for language '{}' could not be found for identifier: {}", scriptType,
|
logger.error("ScriptEngine for language '{}' could not be found for identifier: {}", scriptType,
|
||||||
engineIdentifier);
|
engineIdentifier);
|
||||||
|
@ -195,8 +200,9 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager {
|
||||||
public void removeEngine(String engineIdentifier) {
|
public void removeEngine(String engineIdentifier) {
|
||||||
ScriptEngineContainer container = loadedScriptEngineInstances.get(engineIdentifier);
|
ScriptEngineContainer container = loadedScriptEngineInstances.get(engineIdentifier);
|
||||||
if (container != null) {
|
if (container != null) {
|
||||||
if (container.getScriptEngine() instanceof Invocable) {
|
ScriptEngine scriptEngine = container.getScriptEngine();
|
||||||
Invocable inv = (Invocable) container.getScriptEngine();
|
if (scriptEngine instanceof Invocable) {
|
||||||
|
Invocable inv = (Invocable) scriptEngine;
|
||||||
try {
|
try {
|
||||||
inv.invokeFunction("scriptUnloaded");
|
inv.invokeFunction("scriptUnloaded");
|
||||||
} catch (NoSuchMethodException e) {
|
} catch (NoSuchMethodException e) {
|
||||||
|
@ -207,6 +213,18 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager {
|
||||||
} else {
|
} else {
|
||||||
logger.trace("ScriptEngine does not support Invocable interface");
|
logger.trace("ScriptEngine does not support Invocable interface");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (scriptEngine instanceof AutoCloseable) {
|
||||||
|
AutoCloseable closeable = (AutoCloseable) scriptEngine;
|
||||||
|
try {
|
||||||
|
closeable.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error while closing script engine", e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.trace("ScriptEngine does not support AutoCloseable interface");
|
||||||
|
}
|
||||||
|
|
||||||
removeScriptExtensions(engineIdentifier);
|
removeScriptExtensions(engineIdentifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue