]> git.basschouten.com Git - openhab-addons.git/blob
e22552e441fd133b2e62f70f6a4dfab6383fc59e
[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) {
83                             let rv = {};
84                             for (var key in mapOfValues) {
85                                 rv[key] = mapOfValues.get(key);
86                             }
87                             return rv;
88                             })\
89                             """, "<generated>").build()).execute(map);
90         } catch (IOException e) {
91             throw new IllegalArgumentException("Failed to generate exports", e);
92         }
93     }
94
95     /**
96      * Some specific objects need wrapping when exposed to a GraalJS environment. This method does this.
97      *
98      * @param values the map of names to values of things to process
99      * @return a map of the processed keys and values
100      */
101     private Map<String, Object> processValues(Map<String, Object> values) {
102         Map<String, Object> rv = new HashMap<>(values);
103
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));
108             }
109         }
110
111         return rv;
112     }
113 }