2 * Copyright (c) 2010-2021 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;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.graalvm.polyglot.Context;
22 import org.graalvm.polyglot.Source;
23 import org.graalvm.polyglot.Value;
24 import org.openhab.automation.jsscripting.internal.threading.ThreadsafeWrappingScriptedAutomationManagerDelegate;
25 import org.openhab.core.automation.module.script.ScriptExtensionAccessor;
26 import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedAutomationManager;
29 * Class providing script extensions via CommonJS modules.
31 * @author Jonathan Gilbert - Initial contribution
35 public class ScriptExtensionModuleProvider {
37 private static final String RUNTIME_MODULE_PREFIX = "@runtime";
38 private static final String DEFAULT_MODULE_NAME = "Defaults";
40 private final ScriptExtensionAccessor scriptExtensionAccessor;
42 public ScriptExtensionModuleProvider(ScriptExtensionAccessor scriptExtensionAccessor) {
43 this.scriptExtensionAccessor = scriptExtensionAccessor;
46 public ModuleLocator locatorFor(Context ctx, String engineIdentifier) {
48 String[] segments = name.split("/");
49 if (segments[0].equals(RUNTIME_MODULE_PREFIX)) {
50 if (segments.length == 1) {
51 return runtimeModule(DEFAULT_MODULE_NAME, engineIdentifier, ctx);
53 return runtimeModule(segments[1], engineIdentifier, ctx);
57 return Optional.empty();
61 private Optional<Value> runtimeModule(String name, String scriptIdentifier, Context ctx) {
62 Map<String, Object> symbols;
64 if (DEFAULT_MODULE_NAME.equals(name)) {
65 symbols = scriptExtensionAccessor.findDefaultPresets(scriptIdentifier);
67 symbols = scriptExtensionAccessor.findPreset(name, scriptIdentifier);
70 return Optional.of(symbols).map(this::processValues).map(v -> toValue(ctx, v));
73 private Value toValue(Context ctx, Map<String, Object> map) {
75 return ctx.eval(Source.newBuilder( // convert to Map to JS Object
77 "(function (mapOfValues) {\n" + "let rv = {};\n" + "for (var key in mapOfValues) {\n"
78 + " rv[key] = mapOfValues.get(key);\n" + "}\n" + "return rv;\n" + "})",
79 "<generated>").build()).execute(map);
80 } catch (IOException e) {
81 throw new IllegalArgumentException("Failed to generate exports", e);
86 * Some specific objects need wrapping when exposed to a GraalJS environment. This method does this.
88 * @param values the map of names to values of things to process
89 * @return a map of the processed keys and values
91 private Map<String, Object> processValues(Map<String, Object> values) {
92 Map<String, Object> rv = new HashMap<>(values);
94 for (Map.Entry<String, Object> entry : rv.entrySet()) {
95 if (entry.getValue() instanceof ScriptedAutomationManager) {
96 entry.setValue(new ThreadsafeWrappingScriptedAutomationManagerDelegate(
97 (ScriptedAutomationManager) entry.getValue()));