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