]> git.basschouten.com Git - openhab-addons.git/blob
040bc98c427f559013b44ce4185c5c39b2138357
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import java.util.concurrent.locks.Lock;
20
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;
28
29 /**
30  * Class providing script extensions via CommonJS modules (with module name `@runtime`).
31  *
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
34  *         synchronization
35  */
36
37 @NonNullByDefault
38 public class ScriptExtensionModuleProvider {
39
40     private static final String RUNTIME_MODULE_PREFIX = "@runtime";
41     private static final String DEFAULT_MODULE_NAME = "Defaults";
42     private final Lock lock;
43
44     private final ScriptExtensionAccessor scriptExtensionAccessor;
45
46     public ScriptExtensionModuleProvider(ScriptExtensionAccessor scriptExtensionAccessor, Lock lock) {
47         this.scriptExtensionAccessor = scriptExtensionAccessor;
48         this.lock = lock;
49     }
50
51     public ModuleLocator locatorFor(Context ctx, String engineIdentifier) {
52         return name -> {
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);
57                 } else {
58                     return runtimeModule(segments[1], engineIdentifier, ctx);
59                 }
60             }
61
62             return Optional.empty();
63         };
64     }
65
66     private Optional<Value> runtimeModule(String name, String scriptIdentifier, Context ctx) {
67         Map<String, Object> symbols;
68
69         if (DEFAULT_MODULE_NAME.equals(name)) {
70             symbols = scriptExtensionAccessor.findDefaultPresets(scriptIdentifier);
71         } else {
72             symbols = scriptExtensionAccessor.findPreset(name, scriptIdentifier);
73         }
74
75         return Optional.of(symbols).map(this::processValues).map(v -> toValue(ctx, v));
76     }
77
78     private Value toValue(Context ctx, Map<String, Object> map) {
79         try {
80             return ctx.eval(Source.newBuilder( // convert to Map to JS Object
81                     "js",
82                     "(function (mapOfValues) {\n" + "let rv = {};\n" + "for (var key in mapOfValues) {\n"
83                             + "    rv[key] = mapOfValues.get(key);\n" + "}\n" + "return rv;\n" + "})",
84                     "<generated>").build()).execute(map);
85         } catch (IOException e) {
86             throw new IllegalArgumentException("Failed to generate exports", e);
87         }
88     }
89
90     /**
91      * Some specific objects need wrapping when exposed to a GraalJS environment. This method does this.
92      *
93      * @param values the map of names to values of things to process
94      * @return a map of the processed keys and values
95      */
96     private Map<String, Object> processValues(Map<String, Object> values) {
97         Map<String, Object> rv = new HashMap<>(values);
98
99         for (Map.Entry<String, Object> entry : rv.entrySet()) {
100             if (entry.getValue() instanceof ScriptedAutomationManager) {
101                 entry.setValue(new ThreadsafeWrappingScriptedAutomationManagerDelegate(
102                         (ScriptedAutomationManager) entry.getValue(), lock));
103             }
104         }
105
106         return rv;
107     }
108 }