From afae4d1093552b7b0c63832e57a2357b14047b9a Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Thu, 7 Mar 2024 18:49:34 +0100 Subject: [PATCH] [jsscripting] Fix multi-thread access requested by logger initialization (#16497) * [jsscripting] Fix multi-threading issue with logger initialization Signed-off-by: Florian Hotze Signed-off-by: Ciprian Pascu --- .../internal/DebuggingGraalScriptEngine.java | 14 ++++++++++---- .../internal/OpenhabGraalJSScriptEngine.java | 11 +++++------ 2 files changed, 15 insertions(+), 10 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 c83fba16ee4..6342c6848b4 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 @@ -13,6 +13,7 @@ package org.openhab.automation.jsscripting.internal; import javax.script.Invocable; +import javax.script.ScriptContext; import javax.script.ScriptEngine; import org.eclipse.jdt.annotation.Nullable; @@ -38,11 +39,15 @@ class DebuggingGraalScriptEngine scriptDependencyListener; private boolean initialized = false; @@ -243,7 +242,6 @@ public class OpenhabGraalJSScriptEngine 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); @@ -251,12 +249,13 @@ public class OpenhabGraalJSScriptEngine throw new IllegalStateException("Failed to retrieve script extension accessor from engine bindings"); } - scriptDependencyListener = (Consumer) ctx + Consumer localScriptDependencyListener = (Consumer) ctx .getAttribute("oh.dependency-listener"/* CONTEXT_KEY_DEPENDENCY_LISTENER */); - if (scriptDependencyListener == null) { + if (localScriptDependencyListener == null) { LOGGER.warn( "Failed to retrieve script script dependency listener from engine bindings. Script dependency tracking will be disabled."); } + scriptDependencyListener = localScriptDependencyListener; ScriptExtensionModuleProvider scriptExtensionModuleProvider = new ScriptExtensionModuleProvider( scriptExtensionAccessor, lock); @@ -317,7 +316,7 @@ public class OpenhabGraalJSScriptEngine * @param path a root path * @return whether the given path is a node root directory */ - private boolean isRootNodePath(Path path) { + private static boolean isRootNodePath(Path path) { return path.startsWith(path.getRoot().resolve(NODE_DIR)); } @@ -328,7 +327,7 @@ public class OpenhabGraalJSScriptEngine * @param path a root path, e.g. C:\node_modules\foo.js * @return the class resource path for loading local modules */ - private String nodeFileToResource(Path path) { + private static String nodeFileToResource(Path path) { return "/" + path.subpath(0, path.getNameCount()).toString().replace('\\', '/'); }