]> git.basschouten.com Git - openhab-addons.git/blob
89d12fb8aef0424593c52540d05b5fad410c32f3
[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.concurrent.ConcurrentHashMap;
22 import java.util.function.Function;
23
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.automation.module.script.ScriptExtensionProvider;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.service.component.annotations.Activate;
28
29 /**
30  * Base class to offer support for script extension providers
31  *
32  * @author Jonathan Gilbert - Initial contribution
33  */
34 public abstract class AbstractScriptExtensionProvider implements ScriptExtensionProvider {
35     private Map<String, Function<String, Object>> types = new HashMap<>();
36     private Map<String, Map<String, Object>> idToTypes = new ConcurrentHashMap<>();
37
38     protected abstract String getPresetName();
39
40     protected abstract void initializeTypes(final BundleContext context);
41
42     protected void addType(String name, Function<String, Object> value) {
43         types.put(name, value);
44     }
45
46     @Activate
47     public void activate(final BundleContext context) {
48         types.clear();
49         initializeTypes(context);
50     }
51
52     @Override
53     public Collection<String> getDefaultPresets() {
54         return Collections.emptyList();
55     }
56
57     @Override
58     public Collection<String> getPresets() {
59         return Collections.singleton(getPresetName());
60     }
61
62     @Override
63     public Collection<String> getTypes() {
64         return types.keySet();
65     }
66
67     @Override
68     public @Nullable Object get(String scriptIdentifier, String type) throws IllegalArgumentException {
69
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 }