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