From 41cab0df572956c3385d0fdf051fd565240c898c Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Sun, 30 Jun 2024 20:31:17 +0200 Subject: [PATCH] [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 + } + } }