]> git.basschouten.com Git - openhab-addons.git/blob
2e8e31ade7b7494d8536f33e55af5f12470b5c9b
[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  */
35 @NonNullByDefault
36 public class ThreadsafeWrappingScriptedAutomationManagerDelegate {
37
38     private ScriptedAutomationManager delegate;
39     private Object lock = new Object();
40
41     public ThreadsafeWrappingScriptedAutomationManagerDelegate(ScriptedAutomationManager delegate) {
42         this.delegate = delegate;
43     }
44
45     public void removeModuleType(String UID) {
46         delegate.removeModuleType(UID);
47     }
48
49     public void removeHandler(String typeUID) {
50         delegate.removeHandler(typeUID);
51     }
52
53     public void removePrivateHandler(String privId) {
54         delegate.removePrivateHandler(privId);
55     }
56
57     public void removeAll() {
58         delegate.removeAll();
59     }
60
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);
65         }
66
67         return delegate.addRule(element);
68     }
69
70     public void addConditionType(ConditionType condititonType) {
71         delegate.addConditionType(condititonType);
72     }
73
74     public void addConditionHandler(String uid, ScriptedHandler conditionHandler) {
75         delegate.addConditionHandler(uid, conditionHandler);
76     }
77
78     public String addPrivateConditionHandler(SimpleConditionHandler conditionHandler) {
79         return delegate.addPrivateConditionHandler(conditionHandler);
80     }
81
82     public void addActionType(ActionType actionType) {
83         delegate.addActionType(actionType);
84     }
85
86     public void addActionHandler(String uid, ScriptedHandler actionHandler) {
87         delegate.addActionHandler(uid, actionHandler);
88     }
89
90     public String addPrivateActionHandler(SimpleActionHandler actionHandler) {
91         return delegate.addPrivateActionHandler(actionHandler);
92     }
93
94     public void addTriggerType(TriggerType triggerType) {
95         delegate.addTriggerType(triggerType);
96     }
97
98     public void addTriggerHandler(String uid, ScriptedHandler triggerHandler) {
99         delegate.addTriggerHandler(uid, triggerHandler);
100     }
101
102     public String addPrivateTriggerHandler(SimpleTriggerHandler triggerHandler) {
103         return delegate.addPrivateTriggerHandler(triggerHandler);
104     }
105 }