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
14 package org.openhab.automation.jsscripting.internal.threading;
16 import java.util.concurrent.locks.Lock;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.automation.Rule;
20 import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedAutomationManager;
21 import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedHandler;
22 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleActionHandler;
23 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleConditionHandler;
24 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule;
25 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleTriggerHandler;
26 import org.openhab.core.automation.type.ActionType;
27 import org.openhab.core.automation.type.ConditionType;
28 import org.openhab.core.automation.type.TriggerType;
31 * A replacement for {@link ScriptedAutomationManager} which wraps all rule registrations in a
32 * {@link ThreadsafeSimpleRuleDelegate}. This means that all rules registered via this class with be run in serial per
33 * instance of this class that they are registered with.
35 * @author Jonathan Gilbert - Initial contribution
36 * @author Florian Hotze - Pass in lock object for multi-thread synchronization; Switch to {@link Lock} for multi-thread
40 public class ThreadsafeWrappingScriptedAutomationManagerDelegate {
42 private ScriptedAutomationManager delegate;
43 private final Lock lock;
45 public ThreadsafeWrappingScriptedAutomationManagerDelegate(ScriptedAutomationManager delegate, Lock lock) {
46 this.delegate = delegate;
50 public void removeModuleType(String UID) {
51 delegate.removeModuleType(UID);
54 public void removeHandler(String typeUID) {
55 delegate.removeHandler(typeUID);
58 public void removePrivateHandler(String privId) {
59 delegate.removePrivateHandler(privId);
62 public void removeAll() {
66 public Rule addRule(Rule element) {
67 // wrap in a threadsafe version, safe per context
68 if (element instanceof SimpleRule) {
69 element = new ThreadsafeSimpleRuleDelegate(lock, (SimpleRule) element);
72 return delegate.addRule(element);
75 public void addConditionType(ConditionType condititonType) {
76 delegate.addConditionType(condititonType);
79 public void addConditionHandler(String uid, ScriptedHandler conditionHandler) {
80 delegate.addConditionHandler(uid, conditionHandler);
83 public String addPrivateConditionHandler(SimpleConditionHandler conditionHandler) {
84 return delegate.addPrivateConditionHandler(conditionHandler);
87 public void addActionType(ActionType actionType) {
88 delegate.addActionType(actionType);
91 public void addActionHandler(String uid, ScriptedHandler actionHandler) {
92 delegate.addActionHandler(uid, actionHandler);
95 public String addPrivateActionHandler(SimpleActionHandler actionHandler) {
96 return delegate.addPrivateActionHandler(actionHandler);
99 public void addTriggerType(TriggerType triggerType) {
100 delegate.addTriggerType(triggerType);
103 public void addTriggerHandler(String uid, ScriptedHandler triggerHandler) {
104 delegate.addTriggerHandler(uid, triggerHandler);
107 public String addPrivateTriggerHandler(SimpleTriggerHandler triggerHandler) {
108 return delegate.addPrivateTriggerHandler(triggerHandler);