2 * Copyright (c) 2010-2022 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 org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.openhab.core.automation.Rule;
18 import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedAutomationManager;
19 import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedHandler;
20 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleActionHandler;
21 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleConditionHandler;
22 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule;
23 import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleTriggerHandler;
24 import org.openhab.core.automation.type.ActionType;
25 import org.openhab.core.automation.type.ConditionType;
26 import org.openhab.core.automation.type.TriggerType;
29 * A replacement for {@link ScriptedAutomationManager} which wraps all rule registrations in a
30 * {@link ThreadsafeSimpleRuleDelegate}. This means that all rules registered via this class with be run in serial per
31 * instance of this class that they are registered with.
33 * @author Jonathan Gilbert - Initial contribution
34 * @author Florian Hotze - Pass in lock object for multi-thread synchronization
37 public class ThreadsafeWrappingScriptedAutomationManagerDelegate {
39 private ScriptedAutomationManager delegate;
40 private final Object lock;
42 public ThreadsafeWrappingScriptedAutomationManagerDelegate(ScriptedAutomationManager delegate, Object lock) {
43 this.delegate = delegate;
47 public void removeModuleType(String UID) {
48 delegate.removeModuleType(UID);
51 public void removeHandler(String typeUID) {
52 delegate.removeHandler(typeUID);
55 public void removePrivateHandler(String privId) {
56 delegate.removePrivateHandler(privId);
59 public void removeAll() {
63 public Rule addRule(Rule element) {
64 // wrap in a threadsafe version, safe per context
65 if (element instanceof SimpleRule) {
66 element = new ThreadsafeSimpleRuleDelegate(lock, (SimpleRule) element);
69 return delegate.addRule(element);
72 public void addConditionType(ConditionType condititonType) {
73 delegate.addConditionType(condititonType);
76 public void addConditionHandler(String uid, ScriptedHandler conditionHandler) {
77 delegate.addConditionHandler(uid, conditionHandler);
80 public String addPrivateConditionHandler(SimpleConditionHandler conditionHandler) {
81 return delegate.addPrivateConditionHandler(conditionHandler);
84 public void addActionType(ActionType actionType) {
85 delegate.addActionType(actionType);
88 public void addActionHandler(String uid, ScriptedHandler actionHandler) {
89 delegate.addActionHandler(uid, actionHandler);
92 public String addPrivateActionHandler(SimpleActionHandler actionHandler) {
93 return delegate.addPrivateActionHandler(actionHandler);
96 public void addTriggerType(TriggerType triggerType) {
97 delegate.addTriggerType(triggerType);
100 public void addTriggerHandler(String uid, ScriptedHandler triggerHandler) {
101 delegate.addTriggerHandler(uid, triggerHandler);
104 public String addPrivateTriggerHandler(SimpleTriggerHandler triggerHandler) {
105 return delegate.addPrivateTriggerHandler(triggerHandler);