]> git.basschouten.com Git - openhab-addons.git/blob
29ac13a93f97733d8694436fbeafc7f97267fd1d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.concurrent.ConcurrentHashMap;
21 import java.util.function.Function;
22
23 import org.openhab.core.automation.module.script.ScriptExtensionProvider;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.service.component.annotations.Activate;
26
27 /**
28  * Base class to offer support for script extension providers
29  *
30  * @author Jonathan Gilbert - Initial contribution
31  */
32 public abstract class ScriptDisposalAwareScriptExtensionProvider
33         implements ScriptExtensionProvider, ScriptDisposalAware {
34     private Map<String, Function<String, Object>> types;
35     private Map<String, Map<String, Object>> idToTypes = new ConcurrentHashMap<>();
36
37     protected abstract String getPresetName();
38
39     protected abstract void initializeTypes(final BundleContext context);
40
41     protected void addType(String name, Function<String, Object> value) {
42         types.put(name, value);
43     }
44
45     @Activate
46     public void activate(final BundleContext context) {
47         types = new HashMap<>();
48         initializeTypes(context);
49     }
50
51     @Override
52     public Collection<String> getDefaultPresets() {
53         return Collections.emptyList();
54     }
55
56     @Override
57     public Collection<String> getPresets() {
58         return Collections.singleton(getPresetName());
59     }
60
61     @Override
62     public Collection<String> getTypes() {
63         return types.keySet();
64     }
65
66     @Override
67     public Object get(String scriptIdentifier, String type) throws IllegalArgumentException {
68
69         Map<String, Object> forScript = idToTypes.computeIfAbsent(scriptIdentifier, k -> new HashMap<>());
70         return forScript.computeIfAbsent(type, k -> types.get(k).apply(scriptIdentifier));
71     }
72
73     @Override
74     public Map<String, Object> importPreset(String scriptIdentifier, String preset) {
75         if (getPresetName().equals(preset)) {
76             Map<String, Object> results = new HashMap<>(types.size());
77             for (String type : types.keySet()) {
78                 results.put(type, get(scriptIdentifier, type));
79             }
80             return results;
81         }
82
83         return Collections.emptyMap();
84     }
85
86     @Override
87     public void unload(String scriptIdentifier) {
88         Map<String, Object> forScript = idToTypes.remove(scriptIdentifier);
89
90         if (forScript != null) {
91             for (Object o : forScript.values()) {
92                 if (o instanceof ScriptDisposalAware) {
93                     ((ScriptDisposalAware) o).unload(scriptIdentifier);
94                 }
95             }
96         }
97     }
98 }