]> git.basschouten.com Git - openhab-addons.git/blob
1f3a02c277787fd25bd20302bd6746bc6ac85ca1
[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.concurrent.ConcurrentHashMap;
22 import java.util.function.Function;
23
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.automation.module.script.ScriptExtensionProvider;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.service.component.annotations.Activate;
28
29 /**
30  * Base class to offer support for script extension providers
31  *
32  * @author Jonathan Gilbert - Initial contribution
33  */
34 public abstract class ScriptDisposalAwareScriptExtensionProvider
35         implements ScriptExtensionProvider, ScriptDisposalAware {
36     private Map<String, Function<String, Object>> types;
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 = new HashMap<>();
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 Collections.singleton(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         Map<String, Object> forScript = idToTypes.remove(scriptIdentifier);
92
93         if (forScript != null) {
94             for (Object o : forScript.values()) {
95                 if (o instanceof ScriptDisposalAware) {
96                     ((ScriptDisposalAware) o).unload(scriptIdentifier);
97                 }
98             }
99         }
100     }
101 }