]> git.basschouten.com Git - openhab-addons.git/blob
225e5fddf20f389bcb634e8d48e9440635f7f856
[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.scope;
14
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.function.Supplier;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.automation.module.script.ScriptExtensionProvider;
25 import org.osgi.service.component.annotations.Component;
26
27 /**
28  * Shared Cache implementation for JS scripting.
29  *
30  * @author Jonathan Gilbert - Initial contribution
31  */
32 @Component(immediate = true)
33 @NonNullByDefault
34 public class SharedCache implements ScriptExtensionProvider {
35
36     private static final String PRESET_NAME = "cache";
37     private static final String OBJECT_NAME = "sharedcache";
38
39     private JSCache cache = new JSCache();
40
41     @Override
42     public Collection<String> getDefaultPresets() {
43         return Set.of(PRESET_NAME);
44     }
45
46     @Override
47     public Collection<String> getPresets() {
48         return Set.of(PRESET_NAME);
49     }
50
51     @Override
52     public Collection<String> getTypes() {
53         return Set.of(OBJECT_NAME);
54     }
55
56     @Override
57     public @Nullable Object get(String scriptIdentifier, String type) throws IllegalArgumentException {
58         if (OBJECT_NAME.equals(type)) {
59             return cache;
60         }
61
62         return null;
63     }
64
65     @Override
66     public Map<String, Object> importPreset(String scriptIdentifier, String preset) {
67         if (PRESET_NAME.equals(preset)) {
68             final Object requestedType = get(scriptIdentifier, OBJECT_NAME);
69             if (requestedType != null) {
70                 return Map.of(OBJECT_NAME, requestedType);
71             }
72         }
73
74         return Collections.emptyMap();
75     }
76
77     @Override
78     public void unload(String scriptIdentifier) {
79         // ignore for now
80     }
81
82     public static class JSCache {
83         private Map<String, Object> backingMap = new HashMap<>();
84
85         public void put(String k, Object v) {
86             backingMap.put(k, v);
87         }
88
89         public @Nullable Object remove(String k) {
90             return backingMap.remove(k);
91         }
92
93         public @Nullable Object get(String k) {
94             return backingMap.get(k);
95         }
96
97         public @Nullable Object get(String k, Supplier<Object> supplier) {
98             return backingMap.computeIfAbsent(k, (unused_key) -> supplier.get());
99         }
100     }
101 }