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