]> git.basschouten.com Git - openhab-addons.git/blob
ca5d30115e78e2563fb909f2e47cc5e55058fe82
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.automation.jsscripting.internal.threading.ThreadsafeTimers;
20
21 /**
22  * Abstraction layer to collect all features injected into the JS runtime during the context creation.
23  *
24  * @author Florian Hotze - Initial contribution
25  */
26 @NonNullByDefault
27 public class JSRuntimeFeatures {
28     /**
29      * All elements of this Map are injected into the JS runtime using their key as the name.
30      */
31     private final Map<String, Object> features = new HashMap<>();
32     public final ThreadsafeTimers threadsafeTimers;
33
34     JSRuntimeFeatures(Object lock, JSScriptServiceUtil jsScriptServiceUtil) {
35         this.threadsafeTimers = new ThreadsafeTimers(lock, jsScriptServiceUtil.getScriptExecution(),
36                 jsScriptServiceUtil.getScheduler());
37
38         features.put("ThreadsafeTimers", threadsafeTimers);
39     }
40
41     /**
42      * Get the features that are to be injected into the JS runtime during context creation.
43      * 
44      * @return the runtime features
45      */
46     public Map<String, Object> getFeatures() {
47         return features;
48     }
49
50     /**
51      * Un-initialization hook, called when the engine is closed.
52      * Use this method to clean up resources or cancel operations that were created by the JS runtime.
53      */
54     public void close() {
55         threadsafeTimers.clearAll();
56     }
57 }