diff --git a/bundles/org.openhab.automation.jsscripting/pom.xml b/bundles/org.openhab.automation.jsscripting/pom.xml index 824527ded031c..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 @@ -44,6 +45,7 @@ + com.github.eirslett frontend-maven-plugin @@ -114,6 +116,7 @@ + org.openhab.tools.sat sat-plugin @@ -125,32 +128,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} - 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..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 @@ -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 @@ * @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 @@ 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, - "transformation."); + if (ohEngineIdentifier.toString().startsWith(OPENHAB_TRANSFORMATION_SCRIPT)) { + identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT, "transformation."); } } 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 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 7e00c6af11797..79a93b92882b6 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 @@ 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 @@ * * @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 T getInterface(Object o, Class aClass) { return delegate.getInterface(o, aClass); } + @Override + public CompiledScript compile(String s) throws ScriptException { + return delegate.compile(s); + } + + @Override + public CompiledScript compile(Reader reader) throws ScriptException { + return delegate.compile(reader); + } + @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/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable.java similarity index 81% 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 71501f3973665..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 @@ -16,22 +16,24 @@ 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; 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); } @@ -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 + } + } } 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