]> git.basschouten.com Git - openhab-addons.git/blob
e5d77ff4213e79f6b0e2feb3e5ba1c8382cdef4e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jsscripting.internal;
14
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Optional;
19
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;
27
28 /**
29  * Class providing script extensions via CommonJS modules.
30  *
31  * @author Jonathan Gilbert - Initial contribution
32  */
33
34 @NonNullByDefault
35 public class ScriptExtensionModuleProvider {
36
37     private static final String RUNTIME_MODULE_PREFIX = "@runtime";
38     private static final String DEFAULT_MODULE_NAME = "Defaults";
39
40     private final ScriptExtensionAccessor scriptExtensionAccessor;
41
42     public ScriptExtensionModuleProvider(ScriptExtensionAccessor scriptExtensionAccessor) {
43         this.scriptExtensionAccessor = scriptExtensionAccessor;
44     }
45
46     public ModuleLocator locatorFor(Context ctx, String engineIdentifier) {
47         return name -> {
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);
52                 } else {
53                     return runtimeModule(segments[1], engineIdentifier, ctx);
54                 }
55             }
56
57             return Optional.empty();
58         };
59     }
60
61     private Optional<Value> runtimeModule(String name, String scriptIdentifier, Context ctx) {
62         Map<String, Object> symbols;
63
64         if (DEFAULT_MODULE_NAME.equals(name)) {
65             symbols = scriptExtensionAccessor.findDefaultPresets(scriptIdentifier);
66         } else {
67             symbols = scriptExtensionAccessor.findPreset(name, scriptIdentifier);
68         }
69
70         return Optional.of(symbols).map(this::processValues).map(v -> toValue(ctx, v));
71     }
72
73     private Value toValue(Context ctx, Map<String, Object> map) {
74         try {
75             return ctx.eval(Source.newBuilder( // convert to Map to JS Object
76                     "js",
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);
82         }
83     }
84
85     /**
86      * Some specific objects need wrapping when exposed to a GraalJS environment. This method does this.
87      *
88      * @param values the map of names to values of things to process
89      * @return a map of the processed keys and values
90      */
91     private Map<String, Object> processValues(Map<String, Object> values) {
92         Map<String, Object> rv = new HashMap<>(values);
93
94         for (Map.Entry<String, Object> entry : rv.entrySet()) {
95             if (entry.getValue() instanceof ScriptedAutomationManager) {
96                 entry.setValue(new ThreadsafeWrappingScriptedAutomationManagerDelegate(
97                         (ScriptedAutomationManager) entry.getValue()));
98             }
99         }
100
101         return rv;
102     }
103 }