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 f66b5f9ca1c..c83fba16ee4 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 @@ -15,6 +15,7 @@ package org.openhab.automation.jsscripting.internal; import javax.script.Invocable; 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.slf4j.Logger; @@ -28,8 +29,9 @@ import org.slf4j.LoggerFactory; class DebuggingGraalScriptEngine extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable { - private static final Logger STACK_LOGGER = LoggerFactory - .getLogger("org.openhab.automation.script.javascript.stack"); + private static final String SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER = "openhab-transformation-script-"; + + private @Nullable Logger logger; public DebuggingGraalScriptEngine(T delegate) { super(delegate); @@ -37,13 +39,42 @@ class DebuggingGraalScriptEngine { private static final Logger LOGGER = LoggerFactory.getLogger(OpenhabGraalJSScriptEngine.class); - private static Source GLOBAL_SOURCE; + private static final Source GLOBAL_SOURCE; static { try { GLOBAL_SOURCE = Source.newBuilder("js", getFileAsReader("node_modules/@jsscripting-globals.js"), "@jsscripting-globals.js").cached(true).build(); } catch (IOException e) { - throw new RuntimeException("Failed to load @jsscripting-globals.js", e); + throw new IllegalStateException("Failed to load @jsscripting-globals.js", e); } } - private static Source OPENHAB_JS_SOURCE; + private static final Source OPENHAB_JS_SOURCE; static { try { OPENHAB_JS_SOURCE = Source .newBuilder("js", getFileAsReader("node_modules/@openhab-globals.js"), "@openhab-globals.js") .cached(true).build(); } catch (IOException e) { - throw new RuntimeException("Failed to load @openhab-globals.js", e); + throw new IllegalStateException("Failed to load @openhab-globals.js", e); } } private static final String OPENHAB_JS_INJECTION_CODE = "Object.assign(this, require('openhab'));"; @@ -134,7 +134,7 @@ public class OpenhabGraalJSScriptEngine private final JSRuntimeFeatures jsRuntimeFeatures; // these fields start as null because they are populated on first use - private String engineIdentifier; + private @Nullable String engineIdentifier; private @Nullable Consumer scriptDependencyListener; private boolean initialized = false; @@ -239,10 +239,11 @@ public class OpenhabGraalJSScriptEngine } // these are added post-construction, so we need to fetch them late - this.engineIdentifier = (String) ctx.getAttribute(CONTEXT_KEY_ENGINE_IDENTIFIER); - if (this.engineIdentifier == null) { + String localEngineIdentifier = (String) ctx.getAttribute(CONTEXT_KEY_ENGINE_IDENTIFIER); + if (localEngineIdentifier == null) { throw new IllegalStateException("Failed to retrieve engine identifier from engine bindings"); } + engineIdentifier = localEngineIdentifier; ScriptExtensionAccessor scriptExtensionAccessor = (ScriptExtensionAccessor) ctx .getAttribute(CONTEXT_KEY_EXTENSION_ACCESSOR); @@ -262,7 +263,7 @@ public class OpenhabGraalJSScriptEngine // Wrap the "require" function to also allow loading modules from the ScriptExtensionModuleProvider Function, Function> wrapRequireFn = originalRequireFn -> moduleName -> scriptExtensionModuleProvider - .locatorFor(delegate.getPolyglotContext(), engineIdentifier).locateModule(moduleName) + .locatorFor(delegate.getPolyglotContext(), localEngineIdentifier).locateModule(moduleName) .map(m -> (Object) m).orElseGet(() -> originalRequireFn.apply(new Object[] { moduleName })); delegate.getBindings(ScriptContext.ENGINE_SCOPE).put(REQUIRE_WRAPPER_NAME, wrapRequireFn); delegate.put("require", wrapRequireFn.apply((Function) delegate.get("require")));