diff --git a/bundles/org.openhab.automation.jsscripting/pom.xml b/bundles/org.openhab.automation.jsscripting/pom.xml index b93b4fc5765..960cae8bd64 100644 --- a/bundles/org.openhab.automation.jsscripting/pom.xml +++ b/bundles/org.openhab.automation.jsscripting/pom.xml @@ -27,6 +27,7 @@ + org.apache.maven.plugins maven-dependency-plugin @@ -42,6 +43,7 @@ + com.github.eirslett frontend-maven-plugin @@ -112,6 +114,7 @@ + org.openhab.tools.sat sat-plugin @@ -123,32 +126,33 @@ - - org.graalvm.truffle - truffle-api - ${graal.version} - - - org.graalvm.js - js-scriptengine - ${graal.version} - org.graalvm.sdk graal-sdk ${graal.version} + + org.graalvm.truffle + truffle-api + ${graal.version} + + + + org.graalvm.js + js-scriptengine + ${graal.version} + + org.graalvm.regex regex ${graal.version} - + + org.graalvm.js js ${graal.version} - 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 508e9cf26aa..76f1afb2dd5 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,16 +12,19 @@ */ 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; 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; @@ -31,10 +34,9 @@ import org.slf4j.LoggerFactory; * @author Jonathan Gilbert - Initial contribution * @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging */ -class DebuggingGraalScriptEngine - extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { +class DebuggingGraalScriptEngine + extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable { - private static final String SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER = "openhab-transformation-script-"; private static final int STACK_TRACE_LENGTH = 5; private @Nullable Logger logger; @@ -91,9 +93,8 @@ class DebuggingGraalScriptEngine { + 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 83% 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 7e00c6af117..79a93b92882 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 @@ -15,6 +15,8 @@ package org.openhab.automation.jsscripting.internal.scriptengine; 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,11 +31,11 @@ import org.eclipse.jdt.annotation.NonNull; * * @author Jonathan Gilbert - Initial contribution */ -public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable - implements ScriptEngine, Invocable, AutoCloseable { +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; } @@ -128,6 +130,16 @@ public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable 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); } @@ -155,4 +157,28 @@ public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoClos 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 + } + } } diff --git a/bundles/org.openhab.automation.jsscripting/suppressions.properties b/bundles/org.openhab.automation.jsscripting/suppressions.properties index 38e9aa4cf18..11aad04ecbe 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