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
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
36 public class ThreadsafeWrappingScriptedAutomationManagerDelegate {
38 private ScriptedAutomationManager delegate;
39 private Object lock = new Object();
41 public ThreadsafeWrappingScriptedAutomationManagerDelegate(ScriptedAutomationManager delegate) {
42 this.delegate = delegate;
45 public void removeModuleType(String UID) {
46 delegate.removeModuleType(UID);
49 public void removeHandler(String typeUID) {
50 delegate.removeHandler(typeUID);
53 public void removePrivateHandler(String privId) {
54 delegate.removePrivateHandler(privId);
57 public void removeAll() {
61 public Rule addRule(Rule element) {
62 // wrap in a threadsafe version, safe per context
63 if (element instanceof SimpleRule) {
64 element = new ThreadsafeSimpleRuleDelegate(lock, (SimpleRule) element);
67 return delegate.addRule(element);
70 public void addConditionType(ConditionType condititonType) {
71 delegate.addConditionType(condititonType);
74 public void addConditionHandler(String uid, ScriptedHandler conditionHandler) {
75 delegate.addConditionHandler(uid, conditionHandler);
78 public String addPrivateConditionHandler(SimpleConditionHandler conditionHandler) {
79 return delegate.addPrivateConditionHandler(conditionHandler);
82 public void addActionType(ActionType actionType) {
83 delegate.addActionType(actionType);
86 public void addActionHandler(String uid, ScriptedHandler actionHandler) {
87 delegate.addActionHandler(uid, actionHandler);
90 public String addPrivateActionHandler(SimpleActionHandler actionHandler) {
91 return delegate.addPrivateActionHandler(actionHandler);
94 public void addTriggerType(TriggerType triggerType) {
95 delegate.addTriggerType(triggerType);
98 public void addTriggerHandler(String uid, ScriptedHandler triggerHandler) {
99 delegate.addTriggerHandler(uid, triggerHandler);
102 public String addPrivateTriggerHandler(SimpleTriggerHandler triggerHandler) {
103 return delegate.addPrivateTriggerHandler(triggerHandler);