]> git.basschouten.com Git - openhab-addons.git/blob
b283192cd39a02a2d2d86300b648a56721ccee28
[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.scriptengine;
15
16 import java.io.Reader;
17 import java.lang.reflect.UndeclaredThrowableException;
18
19 import javax.script.Bindings;
20 import javax.script.Invocable;
21 import javax.script.ScriptContext;
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptException;
24
25 /**
26  * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
27  * being thrown.
28  *
29  * @param <T> The delegate class
30  * @author Jonathan Gilbert - Initial contribution
31  */
32 public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable>
33         extends DelegatingScriptEngineWithInvocableAndAutocloseable<T> {
34
35     public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) {
36         super(delegate);
37     }
38
39     protected void beforeInvocation() {
40     }
41
42     protected Object afterInvocation(Object obj) {
43         return obj;
44     }
45
46     protected Exception afterThrowsInvocation(Exception e) {
47         return e;
48     }
49
50     @Override
51     public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
52         try {
53             beforeInvocation();
54             return afterInvocation(super.eval(s, scriptContext));
55         } catch (ScriptException se) {
56             throw (ScriptException) afterThrowsInvocation(se);
57         }
58     }
59
60     @Override
61     public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
62         try {
63             beforeInvocation();
64             return afterInvocation(super.eval(reader, scriptContext));
65         } catch (ScriptException se) {
66             throw (ScriptException) afterThrowsInvocation(se);
67         }
68     }
69
70     @Override
71     public Object eval(String s) throws ScriptException {
72         try {
73             beforeInvocation();
74             return afterInvocation(super.eval(s));
75         } catch (ScriptException se) {
76             throw (ScriptException) afterThrowsInvocation(se);
77         }
78     }
79
80     @Override
81     public Object eval(Reader reader) throws ScriptException {
82         try {
83             beforeInvocation();
84             return afterInvocation(super.eval(reader));
85         } catch (ScriptException se) {
86             throw (ScriptException) afterThrowsInvocation(se);
87         }
88     }
89
90     @Override
91     public Object eval(String s, Bindings bindings) throws ScriptException {
92         try {
93             beforeInvocation();
94             return afterInvocation(super.eval(s, bindings));
95         } catch (ScriptException se) {
96             throw (ScriptException) afterThrowsInvocation(se);
97         }
98     }
99
100     @Override
101     public Object eval(Reader reader, Bindings bindings) throws ScriptException {
102         try {
103             beforeInvocation();
104             return afterInvocation(super.eval(reader, bindings));
105         } catch (ScriptException se) {
106             throw (ScriptException) afterThrowsInvocation(se);
107         }
108     }
109
110     @Override
111     public Object invokeMethod(Object o, String s, Object... objects)
112             throws ScriptException, NoSuchMethodException, NullPointerException, IllegalArgumentException {
113         try {
114             beforeInvocation();
115             return afterInvocation(super.invokeMethod(o, s, objects));
116         } catch (ScriptException se) {
117             throw (ScriptException) afterThrowsInvocation(se);
118         } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeMethod to avoid
119                                             // deadlocks
120             throw (NoSuchMethodException) afterThrowsInvocation(e);
121         } catch (NullPointerException e) {
122             throw (NullPointerException) afterThrowsInvocation(e);
123         } catch (IllegalArgumentException e) {
124             throw (IllegalArgumentException) afterThrowsInvocation(e);
125         } catch (Exception e) {
126             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
127         }
128     }
129
130     @Override
131     public Object invokeFunction(String s, Object... objects)
132             throws ScriptException, NoSuchMethodException, NullPointerException {
133         try {
134             beforeInvocation();
135             return afterInvocation(super.invokeFunction(s, objects));
136         } catch (ScriptException se) {
137             throw (ScriptException) afterThrowsInvocation(se);
138         } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeFunction to avoid
139                                             // deadlocks
140             throw (NoSuchMethodException) afterThrowsInvocation(e);
141         } catch (NullPointerException e) {
142             throw (NullPointerException) afterThrowsInvocation(e);
143         } catch (Exception e) {
144             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
145         }
146     }
147 }