From: Jonathan Gilbert Date: Sat, 15 Jan 2022 16:44:15 +0000 (+0000) Subject: [jsscripting] Wrapped GraalJS ScriptEngines now also Autocloseable (#12022) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=4d88ad06ae0c078de9255e96f8e75b024630f68a;p=openhab-addons.git [jsscripting] Wrapped GraalJS ScriptEngines now also Autocloseable (#12022) Signed-off-by: Jonathan Gilbert --- 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 0a787b6369..4855f02c2f 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.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 - 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 fc123e1721..66f3b4837c 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.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 { +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/DelegatingScriptEngineWithInvocable.java deleted file mode 100644 index 317c71cebb..0000000000 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocable.java +++ /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 - 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 getInterface(Class aClass) { - return delegate.getInterface(aClass); - } - - @Override - public T getInterface(Object o, Class 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 index 0000000000..0e0971284b --- /dev/null +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java @@ -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 + 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 getInterface(Class aClass) { + return delegate.getInterface(aClass); + } + + @Override + 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/InvocationInterceptingScriptEngineWithInvocable.java deleted file mode 100644 index e019722dae..0000000000 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocable.java +++ /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 The delegate class - * @author Jonathan Gilbert - Initial contribution - */ -public abstract class InvocationInterceptingScriptEngineWithInvocable - extends DelegatingScriptEngineWithInvocable { - - 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 index 0000000000..a538bea09a --- /dev/null +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java @@ -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 The delegate class + * @author Jonathan Gilbert - Initial contribution + */ +public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable + extends DelegatingScriptEngineWithInvocableAndAutocloseable { + + 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); + } + } +}