2 * Copyright (c) 2010-2021 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;
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;
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.
36 * @author Jonathan Gilbert - Initial contribution
39 class ThreadsafeSimpleRuleDelegate implements Rule, SimpleRuleActionHandler {
41 private final Object lock;
42 private final SimpleRule delegate;
45 * Constructor requires a lock object and delegate to forward invocations to.
47 * @param lock rule executions will synchronize on this object
48 * @param delegate the delegate to forward invocations to
50 ThreadsafeSimpleRuleDelegate(Object lock, SimpleRule delegate) {
52 this.delegate = delegate;
57 public Object execute(Action module, Map<String, ?> inputs) {
59 return delegate.execute(module, inputs);
64 public String getUID() {
65 return delegate.getUID();
70 public String getTemplateUID() {
71 return delegate.getTemplateUID();
74 public void setTemplateUID(@Nullable String templateUID) {
75 delegate.setTemplateUID(templateUID);
80 public String getName() {
81 return delegate.getName();
84 public void setName(@Nullable String ruleName) {
85 delegate.setName(ruleName);
89 public Set<String> getTags() {
90 return delegate.getTags();
93 public void setTags(@Nullable Set<String> ruleTags) {
94 delegate.setTags(ruleTags);
99 public String getDescription() {
100 return delegate.getDescription();
103 public void setDescription(@Nullable String ruleDescription) {
104 delegate.setDescription(ruleDescription);
108 public Visibility getVisibility() {
109 return delegate.getVisibility();
112 public void setVisibility(@Nullable Visibility visibility) {
113 delegate.setVisibility(visibility);
117 public Configuration getConfiguration() {
118 return delegate.getConfiguration();
121 public void setConfiguration(@Nullable Configuration ruleConfiguration) {
122 delegate.setConfiguration(ruleConfiguration);
126 public List<ConfigDescriptionParameter> getConfigurationDescriptions() {
127 return delegate.getConfigurationDescriptions();
130 public void setConfigurationDescriptions(@Nullable List<ConfigDescriptionParameter> configDescriptions) {
131 delegate.setConfigurationDescriptions(configDescriptions);
135 public List<Condition> getConditions() {
136 return delegate.getConditions();
139 public void setConditions(@Nullable List<Condition> conditions) {
140 delegate.setConditions(conditions);
144 public List<Action> getActions() {
145 return delegate.getActions();
149 public List<Trigger> getTriggers() {
150 return delegate.getTriggers();
153 public void setActions(@Nullable List<Action> actions) {
154 delegate.setActions(actions);
157 public void setTriggers(@Nullable List<Trigger> triggers) {
158 delegate.setTriggers(triggers);
162 public List<Module> getModules() {
163 return delegate.getModules();
166 public <T extends Module> List<T> getModules(@Nullable Class<T> moduleClazz) {
167 return delegate.getModules(moduleClazz);
171 public int hashCode() {
172 return delegate.hashCode();
176 public boolean equals(@Nullable Object obj) {
177 return delegate.equals(obj);
182 public Module getModule(String moduleId) {
183 return delegate.getModule(moduleId);