]> git.basschouten.com Git - openhab-addons.git/blob
6684d065e89a3655e27afe53a98d92c92dd4364e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13
14 package org.openhab.automation.jsscripting.internal.threading;
15
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;
27
28 /**
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.
32  *
33  * @author Jonathan Gilbert - Initial contribution
34  * @author Florian Hotze - Pass in lock object for multi-thread synchronization
35  */
36 @NonNullByDefault
37 public class ThreadsafeWrappingScriptedAutomationManagerDelegate {
38
39     private ScriptedAutomationManager delegate;
40     private final Object lock;
41
42     public ThreadsafeWrappingScriptedAutomationManagerDelegate(ScriptedAutomationManager delegate, Object lock) {
43         this.delegate = delegate;
44         this.lock = lock;
45     }
46
47     public void removeModuleType(String UID) {
48         delegate.removeModuleType(UID);
49     }
50
51     public void removeHandler(String typeUID) {
52         delegate.removeHandler(typeUID);
53     }
54
55     public void removePrivateHandler(String privId) {
56         delegate.removePrivateHandler(privId);
57     }
58
59     public void removeAll() {
60         delegate.removeAll();
61     }
62
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);
67         }
68
69         return delegate.addRule(element);
70     }
71
72     public void addConditionType(ConditionType condititonType) {
73         delegate.addConditionType(condititonType);
74     }
75
76     public void addConditionHandler(String uid, ScriptedHandler conditionHandler) {
77         delegate.addConditionHandler(uid, conditionHandler);
78     }
79
80     public String addPrivateConditionHandler(SimpleConditionHandler conditionHandler) {
81         return delegate.addPrivateConditionHandler(conditionHandler);
82     }
83
84     public void addActionType(ActionType actionType) {
85         delegate.addActionType(actionType);
86     }
87
88     public void addActionHandler(String uid, ScriptedHandler actionHandler) {
89         delegate.addActionHandler(uid, actionHandler);
90     }
91
92     public String addPrivateActionHandler(SimpleActionHandler actionHandler) {
93         return delegate.addPrivateActionHandler(actionHandler);
94     }
95
96     public void addTriggerType(TriggerType triggerType) {
97         delegate.addTriggerType(triggerType);
98     }
99
100     public void addTriggerHandler(String uid, ScriptedHandler triggerHandler) {
101         delegate.addTriggerHandler(uid, triggerHandler);
102     }
103
104     public String addPrivateTriggerHandler(SimpleTriggerHandler triggerHandler) {
105         return delegate.addPrivateTriggerHandler(triggerHandler);
106     }
107 }