]> git.basschouten.com Git - openhab-addons.git/blob
7624fa21d58fc02b1fc917450d8986437eeaef9c
[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
14 package org.openhab.automation.jsscripting.internal.scope;
15
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.function.Function;
24
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.automation.module.script.ScriptExtensionProvider;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.service.component.annotations.Activate;
29
30 /**
31  * Base class to offer support for script extension providers
32  *
33  * @author Jonathan Gilbert - Initial contribution
34  */
35 public abstract class AbstractScriptExtensionProvider implements ScriptExtensionProvider {
36     private Map<String, Function<String, Object>> types = new HashMap<>();
37     private Map<String, Map<String, Object>> idToTypes = new ConcurrentHashMap<>();
38
39     protected abstract String getPresetName();
40
41     protected abstract void initializeTypes(final BundleContext context);
42
43     protected void addType(String name, Function<String, Object> value) {
44         types.put(name, value);
45     }
46
47     @Activate
48     public void activate(final BundleContext context) {
49         types.clear();
50         initializeTypes(context);
51     }
52
53     @Override
54     public Collection<String> getDefaultPresets() {
55         return Collections.emptyList();
56     }
57
58     @Override
59     public Collection<String> getPresets() {
60         return Set.of(getPresetName());
61     }
62
63     @Override
64     public Collection<String> getTypes() {
65         return types.keySet();
66     }
67
68     @Override
69     public @Nullable Object get(String scriptIdentifier, String type) throws IllegalArgumentException {
70         Map<String, Object> forScript = idToTypes.computeIfAbsent(scriptIdentifier, k -> new HashMap<>());
71         return forScript.computeIfAbsent(type,
72                 k -> Objects.nonNull(types.get(k)) ? types.get(k).apply(scriptIdentifier) : null);
73     }
74
75     @Override
76     public Map<String, Object> importPreset(String scriptIdentifier, String preset) {
77         if (getPresetName().equals(preset)) {
78             Map<String, Object> results = new HashMap<>(types.size());
79             for (String type : types.keySet()) {
80                 results.put(type, get(scriptIdentifier, type));
81             }
82             return results;
83         }
84
85         return Collections.emptyMap();
86     }
87
88     @Override
89     public void unload(String scriptIdentifier) {
90         // ignore by default
91     }
92 }