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