2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.jsscripting.internal;
15 import java.io.IOException;
16 import java.util.HashMap;
18 import java.util.Optional;
19 import java.util.concurrent.locks.Lock;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.graalvm.polyglot.Context;
23 import org.graalvm.polyglot.Source;
24 import org.graalvm.polyglot.Value;
25 import org.openhab.automation.jsscripting.internal.threading.ThreadsafeWrappingScriptedAutomationManagerDelegate;
26 import org.openhab.core.automation.module.script.ScriptExtensionAccessor;
27 import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedAutomationManager;
30 * Class providing script extensions via CommonJS modules (with module name `@runtime`).
32 * @author Jonathan Gilbert - Initial contribution
33 * @author Florian Hotze - Pass in lock object for multi-thread synchronization; Switch to {@link Lock} for multi-thread
38 public class ScriptExtensionModuleProvider {
40 private static final String RUNTIME_MODULE_PREFIX = "@runtime";
41 private static final String DEFAULT_MODULE_NAME = "Defaults";
42 private final Lock lock;
44 private final ScriptExtensionAccessor scriptExtensionAccessor;
46 public ScriptExtensionModuleProvider(ScriptExtensionAccessor scriptExtensionAccessor, Lock lock) {
47 this.scriptExtensionAccessor = scriptExtensionAccessor;
51 public ModuleLocator locatorFor(Context ctx, String engineIdentifier) {
53 String[] segments = name.split("/");
54 if (segments[0].equals(RUNTIME_MODULE_PREFIX)) {
55 if (segments.length == 1) {
56 return runtimeModule(DEFAULT_MODULE_NAME, engineIdentifier, ctx);
58 return runtimeModule(segments[1], engineIdentifier, ctx);
62 return Optional.empty();
66 private Optional<Value> runtimeModule(String name, String scriptIdentifier, Context ctx) {
67 Map<String, Object> symbols;
69 if (DEFAULT_MODULE_NAME.equals(name)) {
70 symbols = scriptExtensionAccessor.findDefaultPresets(scriptIdentifier);
72 symbols = scriptExtensionAccessor.findPreset(name, scriptIdentifier);
75 return Optional.of(symbols).map(this::processValues).map(v -> toValue(ctx, v));
78 private Value toValue(Context ctx, Map<String, Object> map) {
80 return ctx.eval(Source.newBuilder( // convert to Map to JS Object
82 (function (mapOfValues) {
84 for (var key in mapOfValues) {
85 rv[key] = mapOfValues.get(key);
89 """, "<generated>").build()).execute(map);
90 } catch (IOException e) {
91 throw new IllegalArgumentException("Failed to generate exports", e);
96 * Some specific objects need wrapping when exposed to a GraalJS environment. This method does this.
98 * @param values the map of names to values of things to process
99 * @return a map of the processed keys and values
101 private Map<String, Object> processValues(Map<String, Object> values) {
102 Map<String, Object> rv = new HashMap<>(values);
104 for (Map.Entry<String, Object> entry : rv.entrySet()) {
105 if (entry.getValue() instanceof ScriptedAutomationManager) {
106 entry.setValue(new ThreadsafeWrappingScriptedAutomationManagerDelegate(
107 (ScriptedAutomationManager) entry.getValue(), lock));