mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 23:22:02 +01:00
[jsscripting] Fix multi-thread access requested by logger initialization (#16497)
* [jsscripting] Fix multi-threading issue with logger initialization Signed-off-by: Florian Hotze <florianh_dev@icloud.com> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
da67b21413
commit
afae4d1093
@ -13,6 +13,7 @@
|
|||||||
package org.openhab.automation.jsscripting.internal;
|
package org.openhab.automation.jsscripting.internal;
|
||||||
|
|
||||||
import javax.script.Invocable;
|
import javax.script.Invocable;
|
||||||
|
import javax.script.ScriptContext;
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
@ -38,11 +39,15 @@ class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoClosea
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Exception afterThrowsInvocation(Exception e) {
|
protected void beforeInvocation() {
|
||||||
|
super.beforeInvocation();
|
||||||
if (logger == null) {
|
if (logger == null) {
|
||||||
initializeLogger();
|
initializeLogger();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Exception afterThrowsInvocation(Exception e) {
|
||||||
Throwable cause = e.getCause();
|
Throwable cause = e.getCause();
|
||||||
if (cause instanceof IllegalArgumentException) {
|
if (cause instanceof IllegalArgumentException) {
|
||||||
logger.error("Failed to execute script:", e);
|
logger.error("Failed to execute script:", e);
|
||||||
@ -59,9 +64,10 @@ class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoClosea
|
|||||||
* Therefore, the logger needs to be initialized on the first use after script engine creation.
|
* Therefore, the logger needs to be initialized on the first use after script engine creation.
|
||||||
*/
|
*/
|
||||||
private void initializeLogger() {
|
private void initializeLogger() {
|
||||||
Object fileName = delegate.getContext().getAttribute("javax.script.filename");
|
ScriptContext ctx = delegate.getContext();
|
||||||
Object ruleUID = delegate.getContext().getAttribute("ruleUID");
|
Object fileName = ctx.getAttribute("javax.script.filename");
|
||||||
Object ohEngineIdentifier = delegate.getContext().getAttribute("oh.engine-identifier");
|
Object ruleUID = ctx.getAttribute("ruleUID");
|
||||||
|
Object ohEngineIdentifier = ctx.getAttribute("oh.engine-identifier");
|
||||||
|
|
||||||
String identifier = "stack";
|
String identifier = "stack";
|
||||||
if (fileName != null) {
|
if (fileName != null) {
|
||||||
|
@ -134,7 +134,6 @@ public class OpenhabGraalJSScriptEngine
|
|||||||
private final JSRuntimeFeatures jsRuntimeFeatures;
|
private final JSRuntimeFeatures jsRuntimeFeatures;
|
||||||
|
|
||||||
// these fields start as null because they are populated on first use
|
// these fields start as null because they are populated on first use
|
||||||
private @Nullable String engineIdentifier;
|
|
||||||
private @Nullable Consumer<String> scriptDependencyListener;
|
private @Nullable Consumer<String> scriptDependencyListener;
|
||||||
|
|
||||||
private boolean initialized = false;
|
private boolean initialized = false;
|
||||||
@ -243,7 +242,6 @@ public class OpenhabGraalJSScriptEngine
|
|||||||
if (localEngineIdentifier == null) {
|
if (localEngineIdentifier == null) {
|
||||||
throw new IllegalStateException("Failed to retrieve engine identifier from engine bindings");
|
throw new IllegalStateException("Failed to retrieve engine identifier from engine bindings");
|
||||||
}
|
}
|
||||||
engineIdentifier = localEngineIdentifier;
|
|
||||||
|
|
||||||
ScriptExtensionAccessor scriptExtensionAccessor = (ScriptExtensionAccessor) ctx
|
ScriptExtensionAccessor scriptExtensionAccessor = (ScriptExtensionAccessor) ctx
|
||||||
.getAttribute(CONTEXT_KEY_EXTENSION_ACCESSOR);
|
.getAttribute(CONTEXT_KEY_EXTENSION_ACCESSOR);
|
||||||
@ -251,12 +249,13 @@ public class OpenhabGraalJSScriptEngine
|
|||||||
throw new IllegalStateException("Failed to retrieve script extension accessor from engine bindings");
|
throw new IllegalStateException("Failed to retrieve script extension accessor from engine bindings");
|
||||||
}
|
}
|
||||||
|
|
||||||
scriptDependencyListener = (Consumer<String>) ctx
|
Consumer<String> localScriptDependencyListener = (Consumer<String>) ctx
|
||||||
.getAttribute("oh.dependency-listener"/* CONTEXT_KEY_DEPENDENCY_LISTENER */);
|
.getAttribute("oh.dependency-listener"/* CONTEXT_KEY_DEPENDENCY_LISTENER */);
|
||||||
if (scriptDependencyListener == null) {
|
if (localScriptDependencyListener == null) {
|
||||||
LOGGER.warn(
|
LOGGER.warn(
|
||||||
"Failed to retrieve script script dependency listener from engine bindings. Script dependency tracking will be disabled.");
|
"Failed to retrieve script script dependency listener from engine bindings. Script dependency tracking will be disabled.");
|
||||||
}
|
}
|
||||||
|
scriptDependencyListener = localScriptDependencyListener;
|
||||||
|
|
||||||
ScriptExtensionModuleProvider scriptExtensionModuleProvider = new ScriptExtensionModuleProvider(
|
ScriptExtensionModuleProvider scriptExtensionModuleProvider = new ScriptExtensionModuleProvider(
|
||||||
scriptExtensionAccessor, lock);
|
scriptExtensionAccessor, lock);
|
||||||
@ -317,7 +316,7 @@ public class OpenhabGraalJSScriptEngine
|
|||||||
* @param path a root path
|
* @param path a root path
|
||||||
* @return whether the given path is a node root directory
|
* @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));
|
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
|
* @param path a root path, e.g. C:\node_modules\foo.js
|
||||||
* @return the class resource path for loading local modules
|
* @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('\\', '/');
|
return "/" + path.subpath(0, path.getNameCount()).toString().replace('\\', '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user