]> git.basschouten.com Git - openhab-addons.git/blob
1494f10f24e4ceb710a66b7da2f4599cba9ff612
[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
71         Map<String, Object> forScript = idToTypes.computeIfAbsent(scriptIdentifier, k -> new HashMap<>());
72         return forScript.computeIfAbsent(type,
73                 k -> Objects.nonNull(types.get(k)) ? types.get(k).apply(scriptIdentifier) : null);
74     }
75
76     @Override
77     public Map<String, Object> importPreset(String scriptIdentifier, String preset) {
78         if (getPresetName().equals(preset)) {
79             Map<String, Object> results = new HashMap<>(types.size());
80             for (String type : types.keySet()) {
81                 results.put(type, get(scriptIdentifier, type));
82             }
83             return results;
84         }
85
86         return Collections.emptyMap();
87     }
88
89     @Override
90     public void unload(String scriptIdentifier) {
91         // ignore by default
92     }
93 }