]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jsscripting] Wrapped GraalJS ScriptEngines now also Autocloseable (#12022)
authorJonathan Gilbert <jpg@trillica.com>
Sat, 15 Jan 2022 16:44:15 +0000 (16:44 +0000)
committerGitHub <noreply@github.com>
Sat, 15 Jan 2022 16:44:15 +0000 (17:44 +0100)
Signed-off-by: Jonathan Gilbert <jpg@trillica.com>
bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/DebuggingGraalScriptEngine.java
bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java
bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocable.java [deleted file]
bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java [new file with mode: 0644]
bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocable.java [deleted file]
bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java [new file with mode: 0644]

index 0a787b6369c8cff3f10c84e08819788e3c514df8..4855f02c2f7fd3309275128440e81d900108d8ca 100644 (file)
@@ -18,7 +18,7 @@ import javax.script.ScriptEngine;
 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 @@ import org.slf4j.LoggerFactory;
  *
  * @author Jonathan Gilbert - Initial contribution
  */
-class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable>
-        extends InvocationInterceptingScriptEngineWithInvocable<T> {
+class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
+        extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
 
     private static final Logger STACK_LOGGER = LoggerFactory
             .getLogger("org.openhab.automation.script.javascript.stack");
index fc123e172173555a5ec500f80df4059b9cef453e..66f3b4837c8682507f1081ba147bf0b52e79e8fd 100644 (file)
@@ -46,7 +46,7 @@ import org.openhab.automation.jsscripting.internal.fs.DelegatingFileSystem;
 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 @@ import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
  * @author Jonathan Gilbert - Initial contribution
  * @author Dan Cunningham - Script injections
  */
-public class OpenhabGraalJSScriptEngine extends InvocationInterceptingScriptEngineWithInvocable<GraalJSScriptEngine> {
+public class OpenhabGraalJSScriptEngine
+        extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<GraalJSScriptEngine> {
 
     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/DelegatingScriptEngineWithInvocable.java
deleted file mode 100644 (file)
index 317c71c..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/**
- * Copyright (c) 2010-2022 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-
-package org.openhab.automation.jsscripting.internal.scriptengine;
-
-import java.io.Reader;
-
-import javax.script.Bindings;
-import javax.script.Invocable;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineFactory;
-import javax.script.ScriptException;
-
-/**
- * {@link ScriptEngine} implementation that delegates to a supplied ScriptEngine instance. Allows overriding specific
- * methods.
- *
- * @author Jonathan Gilbert - Initial contribution
- */
-public abstract class DelegatingScriptEngineWithInvocable<T extends ScriptEngine & Invocable>
-        implements ScriptEngine, Invocable {
-    protected T delegate;
-
-    public DelegatingScriptEngineWithInvocable(T delegate) {
-        this.delegate = delegate;
-    }
-
-    @Override
-    public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
-        return delegate.eval(s, scriptContext);
-    }
-
-    @Override
-    public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
-        return delegate.eval(reader, scriptContext);
-    }
-
-    @Override
-    public Object eval(String s) throws ScriptException {
-        return delegate.eval(s);
-    }
-
-    @Override
-    public Object eval(Reader reader) throws ScriptException {
-        return delegate.eval(reader);
-    }
-
-    @Override
-    public Object eval(String s, Bindings bindings) throws ScriptException {
-        return delegate.eval(s, bindings);
-    }
-
-    @Override
-    public Object eval(Reader reader, Bindings bindings) throws ScriptException {
-        return delegate.eval(reader, bindings);
-    }
-
-    @Override
-    public void put(String s, Object o) {
-        delegate.put(s, o);
-    }
-
-    @Override
-    public Object get(String s) {
-        return delegate.get(s);
-    }
-
-    @Override
-    public Bindings getBindings(int i) {
-        return delegate.getBindings(i);
-    }
-
-    @Override
-    public void setBindings(Bindings bindings, int i) {
-        delegate.setBindings(bindings, i);
-    }
-
-    @Override
-    public Bindings createBindings() {
-        return delegate.createBindings();
-    }
-
-    @Override
-    public ScriptContext getContext() {
-        return delegate.getContext();
-    }
-
-    @Override
-    public void setContext(ScriptContext scriptContext) {
-        delegate.setContext(scriptContext);
-    }
-
-    @Override
-    public ScriptEngineFactory getFactory() {
-        return delegate.getFactory();
-    }
-
-    @Override
-    public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
-        return delegate.invokeMethod(o, s, objects);
-    }
-
-    @Override
-    public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
-        return delegate.invokeFunction(s, objects);
-    }
-
-    @Override
-    public <T> T getInterface(Class<T> aClass) {
-        return delegate.getInterface(aClass);
-    }
-
-    @Override
-    public <T> T getInterface(Object o, Class<T> aClass) {
-        return delegate.getInterface(o, aClass);
-    }
-}
diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java
new file mode 100644 (file)
index 0000000..0e09712
--- /dev/null
@@ -0,0 +1,133 @@
+/**
+ * Copyright (c) 2010-2022 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+
+package org.openhab.automation.jsscripting.internal.scriptengine;
+
+import java.io.Reader;
+
+import javax.script.Bindings;
+import javax.script.Invocable;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptException;
+
+/**
+ * {@link ScriptEngine} implementation that delegates to a supplied ScriptEngine instance. Allows overriding specific
+ * methods.
+ *
+ * @author Jonathan Gilbert - Initial contribution
+ */
+public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable>
+        implements ScriptEngine, Invocable, AutoCloseable {
+    protected T delegate;
+
+    public DelegatingScriptEngineWithInvocableAndAutocloseable(T delegate) {
+        this.delegate = delegate;
+    }
+
+    @Override
+    public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
+        return delegate.eval(s, scriptContext);
+    }
+
+    @Override
+    public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
+        return delegate.eval(reader, scriptContext);
+    }
+
+    @Override
+    public Object eval(String s) throws ScriptException {
+        return delegate.eval(s);
+    }
+
+    @Override
+    public Object eval(Reader reader) throws ScriptException {
+        return delegate.eval(reader);
+    }
+
+    @Override
+    public Object eval(String s, Bindings bindings) throws ScriptException {
+        return delegate.eval(s, bindings);
+    }
+
+    @Override
+    public Object eval(Reader reader, Bindings bindings) throws ScriptException {
+        return delegate.eval(reader, bindings);
+    }
+
+    @Override
+    public void put(String s, Object o) {
+        delegate.put(s, o);
+    }
+
+    @Override
+    public Object get(String s) {
+        return delegate.get(s);
+    }
+
+    @Override
+    public Bindings getBindings(int i) {
+        return delegate.getBindings(i);
+    }
+
+    @Override
+    public void setBindings(Bindings bindings, int i) {
+        delegate.setBindings(bindings, i);
+    }
+
+    @Override
+    public Bindings createBindings() {
+        return delegate.createBindings();
+    }
+
+    @Override
+    public ScriptContext getContext() {
+        return delegate.getContext();
+    }
+
+    @Override
+    public void setContext(ScriptContext scriptContext) {
+        delegate.setContext(scriptContext);
+    }
+
+    @Override
+    public ScriptEngineFactory getFactory() {
+        return delegate.getFactory();
+    }
+
+    @Override
+    public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
+        return delegate.invokeMethod(o, s, objects);
+    }
+
+    @Override
+    public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
+        return delegate.invokeFunction(s, objects);
+    }
+
+    @Override
+    public <T> T getInterface(Class<T> aClass) {
+        return delegate.getInterface(aClass);
+    }
+
+    @Override
+    public <T> T getInterface(Object o, Class<T> 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/InvocationInterceptingScriptEngineWithInvocable.java
deleted file mode 100644 (file)
index e019722..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-/**
- * Copyright (c) 2010-2022 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-
-package org.openhab.automation.jsscripting.internal.scriptengine;
-
-import java.io.Reader;
-
-import javax.script.Bindings;
-import javax.script.Invocable;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptException;
-
-/**
- * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
- * being thrown.
- *
- * @param <T> The delegate class
- * @author Jonathan Gilbert - Initial contribution
- */
-public abstract class InvocationInterceptingScriptEngineWithInvocable<T extends ScriptEngine & Invocable>
-        extends DelegatingScriptEngineWithInvocable<T> {
-
-    public InvocationInterceptingScriptEngineWithInvocable(T delegate) {
-        super(delegate);
-    }
-
-    protected void beforeInvocation() {
-    }
-
-    protected ScriptException afterThrowsInvocation(ScriptException se) {
-        return se;
-    }
-
-    @Override
-    public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
-        try {
-            beforeInvocation();
-            return super.eval(s, scriptContext);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-
-    @Override
-    public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
-        try {
-            beforeInvocation();
-            return super.eval(reader, scriptContext);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-
-    @Override
-    public Object eval(String s) throws ScriptException {
-        try {
-            beforeInvocation();
-            return super.eval(s);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-
-    @Override
-    public Object eval(Reader reader) throws ScriptException {
-        try {
-            beforeInvocation();
-            return super.eval(reader);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-
-    @Override
-    public Object eval(String s, Bindings bindings) throws ScriptException {
-        try {
-            beforeInvocation();
-            return super.eval(s, bindings);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-
-    @Override
-    public Object eval(Reader reader, Bindings bindings) throws ScriptException {
-        try {
-            beforeInvocation();
-            return super.eval(reader, bindings);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-
-    @Override
-    public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
-        try {
-            beforeInvocation();
-            return super.invokeMethod(o, s, objects);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-
-    @Override
-    public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
-        try {
-            beforeInvocation();
-            return super.invokeFunction(s, objects);
-        } catch (ScriptException se) {
-            throw afterThrowsInvocation(se);
-        }
-    }
-}
diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java
new file mode 100644 (file)
index 0000000..a538bea
--- /dev/null
@@ -0,0 +1,124 @@
+/**
+ * Copyright (c) 2010-2022 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+
+package org.openhab.automation.jsscripting.internal.scriptengine;
+
+import java.io.Reader;
+
+import javax.script.Bindings;
+import javax.script.Invocable;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+/**
+ * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
+ * being thrown.
+ *
+ * @param <T> The delegate class
+ * @author Jonathan Gilbert - Initial contribution
+ */
+public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable>
+        extends DelegatingScriptEngineWithInvocableAndAutocloseable<T> {
+
+    public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) {
+        super(delegate);
+    }
+
+    protected void beforeInvocation() {
+    }
+
+    protected ScriptException afterThrowsInvocation(ScriptException se) {
+        return se;
+    }
+
+    @Override
+    public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
+        try {
+            beforeInvocation();
+            return super.eval(s, scriptContext);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+
+    @Override
+    public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
+        try {
+            beforeInvocation();
+            return super.eval(reader, scriptContext);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+
+    @Override
+    public Object eval(String s) throws ScriptException {
+        try {
+            beforeInvocation();
+            return super.eval(s);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+
+    @Override
+    public Object eval(Reader reader) throws ScriptException {
+        try {
+            beforeInvocation();
+            return super.eval(reader);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+
+    @Override
+    public Object eval(String s, Bindings bindings) throws ScriptException {
+        try {
+            beforeInvocation();
+            return super.eval(s, bindings);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+
+    @Override
+    public Object eval(Reader reader, Bindings bindings) throws ScriptException {
+        try {
+            beforeInvocation();
+            return super.eval(reader, bindings);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+
+    @Override
+    public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
+        try {
+            beforeInvocation();
+            return super.invokeMethod(o, s, objects);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+
+    @Override
+    public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
+        try {
+            beforeInvocation();
+            return super.invokeFunction(s, objects);
+        } catch (ScriptException se) {
+            throw afterThrowsInvocation(se);
+        }
+    }
+}