From becbd86e9ea1bf63bb5e02ed5d6893745c671e3d Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Sun, 30 Jun 2024 19:45:26 +0200 Subject: [PATCH 1/6] [jsscripting] Restructure & Comment POM Signed-off-by: Florian Hotze --- .../pom.xml | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.automation.jsscripting/pom.xml b/bundles/org.openhab.automation.jsscripting/pom.xml index 824527ded031c..ffb931be9ff4f 100644 --- a/bundles/org.openhab.automation.jsscripting/pom.xml +++ b/bundles/org.openhab.automation.jsscripting/pom.xml @@ -44,6 +44,7 @@ + com.github.eirslett frontend-maven-plugin @@ -114,6 +115,7 @@ + org.openhab.tools.sat sat-plugin @@ -125,32 +127,33 @@ + + org.graalvm.sdk + graal-sdk + ${graal.version} + org.graalvm.truffle truffle-api ${graal.version} + org.graalvm.js js-scriptengine ${graal.version} - - org.graalvm.sdk - graal-sdk - ${graal.version} - + org.graalvm.regex regex ${graal.version} - + + org.graalvm.js js ${graal.version} - From 8e790cc497be3b294fe84a05da6fe8be26d567f9 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Sun, 30 Jun 2024 20:27:44 +0200 Subject: [PATCH 2/6] [jsscripting] Use OPENHAB_TRANSFORMATION_SCRIPT constant from core Signed-off-by: Florian Hotze --- .../jsscripting/internal/DebuggingGraalScriptEngine.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 508e9cf26aa7f..47f1795877982 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 @@ -25,6 +25,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT; + /** * Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts. * @@ -34,7 +36,6 @@ class DebuggingGraalScriptEngine extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { - private static final String SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER = "openhab-transformation-script-"; private static final int STACK_TRACE_LENGTH = 5; private @Nullable Logger logger; @@ -91,8 +92,8 @@ private void initializeLogger() { } else if (ruleUID != null) { identifier = ruleUID.toString(); } else if (ohEngineIdentifier != null) { - if (ohEngineIdentifier.toString().startsWith(SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER)) { - identifier = ohEngineIdentifier.toString().replaceAll(SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER, + if (ohEngineIdentifier.toString().startsWith(OPENHAB_TRANSFORMATION_SCRIPT)) { + identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT, "transformation."); } } From c5cfed5f856bdef12a286dd52307490972579273 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Sun, 30 Jun 2024 20:31:17 +0200 Subject: [PATCH 3/6] [jsscripting] Implement javax.script.Compilable Signed-off-by: Florian Hotze --- .../internal/DebuggingGraalScriptEngine.java | 10 +++---- ...ptEngineWithInvocableAndAutocloseable.java | 16 +++++++++-- ...ptEngineWithInvocableAndAutoCloseable.java | 28 ++++++++++++++++++- 3 files changed, 46 insertions(+), 8 deletions(-) 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 47f1795877982..fbcb514fc2bff 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 @@ -12,9 +12,12 @@ */ package org.openhab.automation.jsscripting.internal; +import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT; + import java.util.Arrays; import java.util.stream.Collectors; +import javax.script.Compilable; import javax.script.Invocable; import javax.script.ScriptContext; import javax.script.ScriptEngine; @@ -25,15 +28,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT; - /** * Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts. * * @author Jonathan Gilbert - Initial contribution * @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging */ -class DebuggingGraalScriptEngine +class DebuggingGraalScriptEngine extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { private static final int STACK_TRACE_LENGTH = 5; @@ -93,8 +94,7 @@ private void initializeLogger() { identifier = ruleUID.toString(); } else if (ohEngineIdentifier != null) { if (ohEngineIdentifier.toString().startsWith(OPENHAB_TRANSFORMATION_SCRIPT)) { - identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT, - "transformation."); + identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT, "transformation."); } } 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 index 7e00c6af11797..d0225c72d896e 100644 --- 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 @@ -15,6 +15,8 @@ import java.io.Reader; import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; import javax.script.Invocable; import javax.script.ScriptContext; import javax.script.ScriptEngine; @@ -29,8 +31,8 @@ * * @author Jonathan Gilbert - Initial contribution */ -public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable - implements ScriptEngine, Invocable, AutoCloseable { +public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable + implements ScriptEngine, Invocable, AutoCloseable, Compilable { protected @NonNull T delegate; public DelegatingScriptEngineWithInvocableAndAutocloseable(@NonNull T delegate) { @@ -132,4 +134,14 @@ public T getInterface(Object o, Class aClass) { public void close() throws Exception { delegate.close(); } + + @Override + public CompiledScript compile(String s) throws ScriptException { + return delegate.compile(s); + } + + @Override + public CompiledScript compile(Reader reader) throws ScriptException { + return delegate.compile(reader); + } } 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 index 71501f3973665..ccb28bc19f498 100644 --- 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 @@ -16,6 +16,8 @@ import java.lang.reflect.UndeclaredThrowableException; import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; import javax.script.Invocable; import javax.script.ScriptContext; import javax.script.ScriptEngine; @@ -28,7 +30,7 @@ * @param The delegate class * @author Jonathan Gilbert - Initial contribution */ -public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable +public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable extends DelegatingScriptEngineWithInvocableAndAutocloseable { public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) { @@ -155,4 +157,28 @@ public Object invokeFunction(String s, Object... objects) throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions } } + + @Override + public CompiledScript compile(String s) throws ScriptException { + try { + beforeInvocation(); + return (CompiledScript) afterInvocation(super.compile(s)); + } catch (ScriptException se) { + throw (ScriptException) afterThrowsInvocation(se); + } catch (Exception e) { + throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions + } + } + + @Override + public CompiledScript compile(Reader reader) throws ScriptException { + try { + beforeInvocation(); + return (CompiledScript) afterInvocation(super.compile(reader)); + } catch (ScriptException se) { + throw (ScriptException) afterThrowsInvocation(se); + } catch (Exception e) { + throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions + } + } } From d18aedb4c26964c8581a4213a2126982e1db191b Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Tue, 9 Jul 2024 19:06:55 +0200 Subject: [PATCH 4/6] [jsscripting] Minor POM comment improvements Signed-off-by: Florian Hotze --- bundles/org.openhab.automation.jsscripting/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.automation.jsscripting/pom.xml b/bundles/org.openhab.automation.jsscripting/pom.xml index ffb931be9ff4f..9041a6a8c5b84 100644 --- a/bundles/org.openhab.automation.jsscripting/pom.xml +++ b/bundles/org.openhab.automation.jsscripting/pom.xml @@ -29,6 +29,7 @@ + org.apache.maven.plugins maven-dependency-plugin @@ -149,7 +150,7 @@ regex ${graal.version} - + org.graalvm.js js From e85efa72708afdaf3fbf905739c1bdd07e865962 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Tue, 9 Jul 2024 19:35:31 +0200 Subject: [PATCH 5/6] [jsscripting] Rename classes accordingly Signed-off-by: Florian Hotze --- .../internal/DebuggingGraalScriptEngine.java | 4 ++-- .../internal/OpenhabGraalJSScriptEngine.java | 4 ++-- ...ineWithInvocableAndCompilableAndAutocloseable.java} | 6 +++--- ...ineWithInvocableAndCompilableAndAutoCloseable.java} | 10 +++++----- .../suppressions.properties | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) rename bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/{DelegatingScriptEngineWithInvocableAndAutocloseable.java => DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java} (92%) rename bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/{InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java => InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable.java} (95%) 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 fbcb514fc2bff..76f1afb2dd544 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 @@ -24,7 +24,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.graalvm.polyglot.PolyglotException; -import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable; +import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,7 +35,7 @@ * @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging */ class DebuggingGraalScriptEngine - extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { + extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable { private static final int STACK_TRACE_LENGTH = 5; 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 e4d324a8a72c5..cb4dcfd23b58b 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 @@ -50,7 +50,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.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable; +import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable; import org.openhab.core.automation.module.script.ScriptExtensionAccessor; import org.openhab.core.items.Item; import org.openhab.core.library.types.QuantityType; @@ -69,7 +69,7 @@ * {@link Lock} for multi-thread synchronization; globals and openhab-js injection code caching */ public class OpenhabGraalJSScriptEngine - extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { + extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(OpenhabGraalJSScriptEngine.class); private static final Source GLOBAL_SOURCE; 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/DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java similarity index 92% rename from bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java rename to bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java index d0225c72d896e..760f0e9fd231a 100644 --- 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/DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java @@ -31,11 +31,11 @@ * * @author Jonathan Gilbert - Initial contribution */ -public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable - implements ScriptEngine, Invocable, AutoCloseable, Compilable { +public abstract class DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable + implements ScriptEngine, Invocable, Compilable, AutoCloseable { protected @NonNull T delegate; - public DelegatingScriptEngineWithInvocableAndAutocloseable(@NonNull T delegate) { + public DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable(@NonNull T delegate) { this.delegate = delegate; } 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/InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable.java similarity index 95% rename from bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java rename to bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable.java index ccb28bc19f498..d23a5fc1358a6 100644 --- 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/InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable.java @@ -24,16 +24,16 @@ import javax.script.ScriptException; /** - * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}. - * being thrown. + * 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 abstract class InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable + extends DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable { - public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) { + public InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable(T delegate) { super(delegate); } diff --git a/bundles/org.openhab.automation.jsscripting/suppressions.properties b/bundles/org.openhab.automation.jsscripting/suppressions.properties index 38e9aa4cf1896..11aad04ecbe6b 100644 --- a/bundles/org.openhab.automation.jsscripting/suppressions.properties +++ b/bundles/org.openhab.automation.jsscripting/suppressions.properties @@ -1,3 +1,3 @@ # Please check here how to add suppressions https://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html org.openhab.automation.jsscripting.internal.OpenhabGraalJSScriptEngine=UnusedPrivateField -org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable=AvoidThrowingNullPointerException,AvoidCatchingNPE +org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable=AvoidThrowingNullPointerException,AvoidCatchingNPE From c813a9c812eb072687c86d2024fe7fae37987f55 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Tue, 9 Jul 2024 19:37:51 +0200 Subject: [PATCH 6/6] [jsscripting] Re-arrange methods accordingly Signed-off-by: Florian Hotze --- ...gineWithInvocableAndCompilableAndAutocloseable.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java index 760f0e9fd231a..79a93b92882b6 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable.java @@ -130,11 +130,6 @@ public T getInterface(Object o, Class aClass) { return delegate.getInterface(o, aClass); } - @Override - public void close() throws Exception { - delegate.close(); - } - @Override public CompiledScript compile(String s) throws ScriptException { return delegate.compile(s); @@ -144,4 +139,9 @@ public CompiledScript compile(String s) throws ScriptException { public CompiledScript compile(Reader reader) throws ScriptException { return delegate.compile(reader); } + + @Override + public void close() throws Exception { + delegate.close(); + } }