From 4d88ad06ae0c078de9255e96f8e75b024630f68a Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Sat, 15 Jan 2022 16:44:15 +0000 Subject: [PATCH] [jsscripting] Wrapped GraalJS ScriptEngines now also Autocloseable (#12022) Signed-off-by: Jonathan Gilbert --- .../internal/DebuggingGraalScriptEngine.java | 6 +++--- .../internal/OpenhabGraalJSScriptEngine.java | 5 +++-- ...ingScriptEngineWithInvocableAndAutocloseable.java} | 11 ++++++++--- ...ingScriptEngineWithInvocableAndAutoCloseable.java} | 6 +++--- 4 files changed, 17 insertions(+), 11 deletions(-) rename bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/{DelegatingScriptEngineWithInvocable.java => DelegatingScriptEngineWithInvocableAndAutocloseable.java} (90%) rename bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/{InvocationInterceptingScriptEngineWithInvocable.java => InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java} (93%) diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/DebuggingGraalScriptEngine.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/DebuggingGraalScriptEngine.java index 0a787b6369c8c..4855f02c2f7fd 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/DebuggingGraalScriptEngine.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/DebuggingGraalScriptEngine.java @@ -18,7 +18,7 @@ import javax.script.ScriptException; import org.graalvm.polyglot.PolyglotException; -import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocable; +import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,8 +27,8 @@ * * @author Jonathan Gilbert - Initial contribution */ -class DebuggingGraalScriptEngine - extends InvocationInterceptingScriptEngineWithInvocable { +class DebuggingGraalScriptEngine + extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { private static final Logger STACK_LOGGER = LoggerFactory .getLogger("org.openhab.automation.script.javascript.stack"); diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java index fc123e1721735..66f3b4837c868 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java @@ -46,7 +46,7 @@ import org.openhab.automation.jsscripting.internal.fs.PrefixedSeekableByteChannel; import org.openhab.automation.jsscripting.internal.fs.ReadOnlySeekableByteArrayChannel; import org.openhab.automation.jsscripting.internal.fs.watch.JSDependencyTracker; -import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocable; +import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable; import org.openhab.core.automation.module.script.ScriptExtensionAccessor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +59,8 @@ * @author Jonathan Gilbert - Initial contribution * @author Dan Cunningham - Script injections */ -public class OpenhabGraalJSScriptEngine extends InvocationInterceptingScriptEngineWithInvocable { +public class OpenhabGraalJSScriptEngine + extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(OpenhabGraalJSScriptEngine.class); private static final String GLOBAL_REQUIRE = "require(\"@jsscripting-globals\");"; diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocable.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java similarity index 90% rename from bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocable.java rename to bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java index 317c71cebb3f6..0e0971284b142 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocable.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java @@ -28,11 +28,11 @@ * * @author Jonathan Gilbert - Initial contribution */ -public abstract class DelegatingScriptEngineWithInvocable - implements ScriptEngine, Invocable { +public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable + implements ScriptEngine, Invocable, AutoCloseable { protected T delegate; - public DelegatingScriptEngineWithInvocable(T delegate) { + public DelegatingScriptEngineWithInvocableAndAutocloseable(T delegate) { this.delegate = delegate; } @@ -125,4 +125,9 @@ public T getInterface(Class aClass) { public T getInterface(Object o, Class aClass) { return delegate.getInterface(o, aClass); } + + @Override + public void close() throws Exception { + delegate.close(); + } } diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocable.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java similarity index 93% rename from bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocable.java rename to bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java index e019722dae773..a538bea09a36a 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocable.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java @@ -28,10 +28,10 @@ * @param The delegate class * @author Jonathan Gilbert - Initial contribution */ -public abstract class InvocationInterceptingScriptEngineWithInvocable - extends DelegatingScriptEngineWithInvocable { +public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable + extends DelegatingScriptEngineWithInvocableAndAutocloseable { - public InvocationInterceptingScriptEngineWithInvocable(T delegate) { + public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) { super(delegate); }