2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.jsscripting.internal.threading;
15 import java.util.List;
18 import java.util.concurrent.locks.Lock;
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;
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.
37 * @author Jonathan Gilbert - Initial contribution
40 class ThreadsafeSimpleRuleDelegate implements Rule, SimpleRuleActionHandler {
42 private final Lock lock;
43 private final SimpleRule delegate;
46 * Constructor requires a lock object and delegate to forward invocations to.
48 * @param lock rule executions will synchronize on this object
49 * @param delegate the delegate to forward invocations to
51 ThreadsafeSimpleRuleDelegate(Lock lock, SimpleRule delegate) {
53 this.delegate = delegate;
58 public Object execute(Action module, Map<String, ?> inputs) {
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
68 public String getUID() {
69 return delegate.getUID();
74 public String getTemplateUID() {
75 return delegate.getTemplateUID();
78 public void setTemplateUID(@Nullable String templateUID) {
79 delegate.setTemplateUID(templateUID);
84 public String getName() {
85 return delegate.getName();
88 public void setName(@Nullable String ruleName) {
89 delegate.setName(ruleName);
93 public Set<String> getTags() {
94 return delegate.getTags();
97 public void setTags(@Nullable Set<String> ruleTags) {
98 delegate.setTags(ruleTags);
103 public String getDescription() {
104 return delegate.getDescription();
107 public void setDescription(@Nullable String ruleDescription) {
108 delegate.setDescription(ruleDescription);
112 public Visibility getVisibility() {
113 return delegate.getVisibility();
116 public void setVisibility(@Nullable Visibility visibility) {
117 delegate.setVisibility(visibility);
121 public Configuration getConfiguration() {
122 return delegate.getConfiguration();
125 public void setConfiguration(@Nullable Configuration ruleConfiguration) {
126 delegate.setConfiguration(ruleConfiguration);
130 public List<ConfigDescriptionParameter> getConfigurationDescriptions() {
131 return delegate.getConfigurationDescriptions();
134 public void setConfigurationDescriptions(@Nullable List<ConfigDescriptionParameter> configDescriptions) {
135 delegate.setConfigurationDescriptions(configDescriptions);
139 public List<Condition> getConditions() {
140 return delegate.getConditions();
143 public void setConditions(@Nullable List<Condition> conditions) {
144 delegate.setConditions(conditions);
148 public List<Action> getActions() {
149 return delegate.getActions();
153 public List<Trigger> getTriggers() {
154 return delegate.getTriggers();
157 public void setActions(@Nullable List<Action> actions) {
158 delegate.setActions(actions);
161 public void setTriggers(@Nullable List<Trigger> triggers) {
162 delegate.setTriggers(triggers);
166 public List<Module> getModules() {
167 return delegate.getModules();
170 public <T extends Module> List<T> getModules(@Nullable Class<T> moduleClazz) {
171 return delegate.getModules(moduleClazz);
175 public int hashCode() {
176 return delegate.hashCode();
180 public boolean equals(@Nullable Object obj) {
181 return delegate.equals(obj);
186 public Module getModule(String moduleId) {
187 return delegate.getModule(moduleId);