]> git.basschouten.com Git - openhab-addons.git/blob
9f73e3916efe5888c64e3a1f38dd194bbb8a52b1
[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 ScriptDisposalAwareScriptExtensionProvider
36         implements ScriptExtensionProvider, ScriptDisposalAware {
37     private Map<String, Function<String, Object>> types;
38     private Map<String, Map<String, Object>> idToTypes = new ConcurrentHashMap<>();
39
40     protected abstract String getPresetName();
41
42     protected abstract void initializeTypes(final BundleContext context);
43
44     protected void addType(String name, Function<String, Object> value) {
45         types.put(name, value);
46     }
47
48     @Activate
49     public void activate(final BundleContext context) {
50         types = new HashMap<>();
51         initializeTypes(context);
52     }
53
54     @Override
55     public Collection<String> getDefaultPresets() {
56         return Collections.emptyList();
57     }
58
59     @Override
60     public Collection<String> getPresets() {
61         return Set.of(getPresetName());
62     }
63
64     @Override
65     public Collection<String> getTypes() {
66         return types.keySet();
67     }
68
69     @Override
70     public @Nullable Object get(String scriptIdentifier, String type) throws IllegalArgumentException {
71
72         Map<String, Object> forScript = idToTypes.computeIfAbsent(scriptIdentifier, k -> new HashMap<>());
73         return forScript.computeIfAbsent(type,
74                 k -> Objects.nonNull(types.get(k)) ? types.get(k).apply(scriptIdentifier) : null);
75     }
76
77     @Override
78     public Map<String, Object> importPreset(String scriptIdentifier, String preset) {
79         if (getPresetName().equals(preset)) {
80             Map<String, Object> results = new HashMap<>(types.size());
81             for (String type : types.keySet()) {
82                 results.put(type, get(scriptIdentifier, type));
83             }
84             return results;
85         }
86
87         return Collections.emptyMap();
88     }
89
90     @Override
91     public void unload(String scriptIdentifier) {
92         Map<String, Object> forScript = idToTypes.remove(scriptIdentifier);
93
94         if (forScript != null) {
95             for (Object o : forScript.values()) {
96                 if (o instanceof ScriptDisposalAware script) {
97                     script.unload(scriptIdentifier);
98                 }
99             }
100         }
101     }
102 }