]> git.basschouten.com Git - openhab-addons.git/blob
c1baa07e2fc4c74d7c226225627720e6f65e6eab
[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 package org.openhab.automation.jsscripting.internal.threading;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.locks.Lock;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.automation.Action;
23 import org.openhab.core.automation.Condition;
24 import org.openhab.core.automation.Module;
25 import org.openhab.core.automation.Rule;
26 import org.openhab.core.automation.Trigger;
27 import org.openhab.core.automation.Visibility;
28 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule;
29 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandler;
30 import org.openhab.core.config.core.ConfigDescriptionParameter;
31 import org.openhab.core.config.core.Configuration;
32
33 /**
34  * A version of {@link SimpleRule} which controls multithreaded execution access to this specific rule. This is useful
35  * for rules which wrap GraalJS Contexts, which are not multithreaded.
36  *
37  * @author Jonathan Gilbert - Initial contribution
38  */
39 @NonNullByDefault
40 class ThreadsafeSimpleRuleDelegate implements Rule, SimpleRuleActionHandler {
41
42     private final Lock lock;
43     private final SimpleRule delegate;
44
45     /**
46      * Constructor requires a lock object and delegate to forward invocations to.
47      *
48      * @param lock rule executions will synchronize on this object
49      * @param delegate the delegate to forward invocations to
50      */
51     ThreadsafeSimpleRuleDelegate(Lock lock, SimpleRule delegate) {
52         this.lock = lock;
53         this.delegate = delegate;
54     }
55
56     @Override
57     @NonNullByDefault({})
58     public Object execute(Action module, Map<String, ?> inputs) {
59         lock.lock();
60         try {
61             return delegate.execute(module, inputs);
62         } finally { // Make sure that Lock is unlocked regardless of an exception is thrown or not to avoid deadlocks
63             lock.unlock();
64         }
65     }
66
67     @Override
68     public String getUID() {
69         return delegate.getUID();
70     }
71
72     @Override
73     @Nullable
74     public String getTemplateUID() {
75         return delegate.getTemplateUID();
76     }
77
78     public void setTemplateUID(@Nullable String templateUID) {
79         delegate.setTemplateUID(templateUID);
80     }
81
82     @Override
83     @Nullable
84     public String getName() {
85         return delegate.getName();
86     }
87
88     public void setName(@Nullable String ruleName) {
89         delegate.setName(ruleName);
90     }
91
92     @Override
93     public Set<String> getTags() {
94         return delegate.getTags();
95     }
96
97     public void setTags(@Nullable Set<String> ruleTags) {
98         delegate.setTags(ruleTags);
99     }
100
101     @Override
102     @Nullable
103     public String getDescription() {
104         return delegate.getDescription();
105     }
106
107     public void setDescription(@Nullable String ruleDescription) {
108         delegate.setDescription(ruleDescription);
109     }
110
111     @Override
112     public Visibility getVisibility() {
113         return delegate.getVisibility();
114     }
115
116     public void setVisibility(@Nullable Visibility visibility) {
117         delegate.setVisibility(visibility);
118     }
119
120     @Override
121     public Configuration getConfiguration() {
122         return delegate.getConfiguration();
123     }
124
125     public void setConfiguration(@Nullable Configuration ruleConfiguration) {
126         delegate.setConfiguration(ruleConfiguration);
127     }
128
129     @Override
130     public List<ConfigDescriptionParameter> getConfigurationDescriptions() {
131         return delegate.getConfigurationDescriptions();
132     }
133
134     public void setConfigurationDescriptions(@Nullable List<ConfigDescriptionParameter> configDescriptions) {
135         delegate.setConfigurationDescriptions(configDescriptions);
136     }
137
138     @Override
139     public List<Condition> getConditions() {
140         return delegate.getConditions();
141     }
142
143     public void setConditions(@Nullable List<Condition> conditions) {
144         delegate.setConditions(conditions);
145     }
146
147     @Override
148     public List<Action> getActions() {
149         return delegate.getActions();
150     }
151
152     @Override
153     public List<Trigger> getTriggers() {
154         return delegate.getTriggers();
155     }
156
157     public void setActions(@Nullable List<Action> actions) {
158         delegate.setActions(actions);
159     }
160
161     public void setTriggers(@Nullable List<Trigger> triggers) {
162         delegate.setTriggers(triggers);
163     }
164
165     @Override
166     public List<Module> getModules() {
167         return delegate.getModules();
168     }
169
170     public <T extends Module> List<T> getModules(@Nullable Class<T> moduleClazz) {
171         return delegate.getModules(moduleClazz);
172     }
173
174     @Override
175     public int hashCode() {
176         return delegate.hashCode();
177     }
178
179     @Override
180     public boolean equals(@Nullable Object obj) {
181         return delegate.equals(obj);
182     }
183
184     @Override
185     @Nullable
186     public Module getModule(String moduleId) {
187         return delegate.getModule(moduleId);
188     }
189 }