]> git.basschouten.com Git - openhab-addons.git/blob
3d23ae082d80749bfe17a06c9282a3eefc902e8a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 java.util.concurrent.locks.Lock;
17
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;
29
30 /**
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.
34  *
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
37  *         synchronization
38  */
39 @NonNullByDefault
40 public class ThreadsafeWrappingScriptedAutomationManagerDelegate {
41
42     private ScriptedAutomationManager delegate;
43     private final Lock lock;
44
45     public ThreadsafeWrappingScriptedAutomationManagerDelegate(ScriptedAutomationManager delegate, Lock lock) {
46         this.delegate = delegate;
47         this.lock = lock;
48     }
49
50     public void removeModuleType(String UID) {
51         delegate.removeModuleType(UID);
52     }
53
54     public void removeHandler(String typeUID) {
55         delegate.removeHandler(typeUID);
56     }
57
58     public void removePrivateHandler(String privId) {
59         delegate.removePrivateHandler(privId);
60     }
61
62     public void removeAll() {
63         delegate.removeAll();
64     }
65
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);
70         }
71
72         return delegate.addRule(element);
73     }
74
75     public void addConditionType(ConditionType condititonType) {
76         delegate.addConditionType(condititonType);
77     }
78
79     public void addConditionHandler(String uid, ScriptedHandler conditionHandler) {
80         delegate.addConditionHandler(uid, conditionHandler);
81     }
82
83     public String addPrivateConditionHandler(SimpleConditionHandler conditionHandler) {
84         return delegate.addPrivateConditionHandler(conditionHandler);
85     }
86
87     public void addActionType(ActionType actionType) {
88         delegate.addActionType(actionType);
89     }
90
91     public void addActionHandler(String uid, ScriptedHandler actionHandler) {
92         delegate.addActionHandler(uid, actionHandler);
93     }
94
95     public String addPrivateActionHandler(SimpleActionHandler actionHandler) {
96         return delegate.addPrivateActionHandler(actionHandler);
97     }
98
99     public void addTriggerType(TriggerType triggerType) {
100         delegate.addTriggerType(triggerType);
101     }
102
103     public void addTriggerHandler(String uid, ScriptedHandler triggerHandler) {
104         delegate.addTriggerHandler(uid, triggerHandler);
105     }
106
107     public String addPrivateTriggerHandler(SimpleTriggerHandler triggerHandler) {
108         return delegate.addPrivateTriggerHandler(triggerHandler);
109     }
110 }