]> git.basschouten.com Git - openhab-addons.git/blob
1cc0935485ea70cb46812951a91347bb06d6bf7b
[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.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         } catch (Exception e) {
58             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
59         }
60     }
61
62     @Override
63     public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
64         try {
65             beforeInvocation();
66             return afterInvocation(super.eval(reader, scriptContext));
67         } catch (ScriptException se) {
68             throw (ScriptException) afterThrowsInvocation(se);
69         } catch (Exception e) {
70             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
71         }
72     }
73
74     @Override
75     public Object eval(String s) throws ScriptException {
76         try {
77             beforeInvocation();
78             return afterInvocation(super.eval(s));
79         } catch (ScriptException se) {
80             throw (ScriptException) afterThrowsInvocation(se);
81         } catch (Exception e) {
82             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
83         }
84     }
85
86     @Override
87     public Object eval(Reader reader) throws ScriptException {
88         try {
89             beforeInvocation();
90             return afterInvocation(super.eval(reader));
91         } catch (ScriptException se) {
92             throw (ScriptException) afterThrowsInvocation(se);
93         } catch (Exception e) {
94             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
95         }
96     }
97
98     @Override
99     public Object eval(String s, Bindings bindings) throws ScriptException {
100         try {
101             beforeInvocation();
102             return afterInvocation(super.eval(s, bindings));
103         } catch (ScriptException se) {
104             throw (ScriptException) afterThrowsInvocation(se);
105         } catch (Exception e) {
106             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
107         }
108     }
109
110     @Override
111     public Object eval(Reader reader, Bindings bindings) throws ScriptException {
112         try {
113             beforeInvocation();
114             return afterInvocation(super.eval(reader, bindings));
115         } catch (ScriptException se) {
116             throw (ScriptException) afterThrowsInvocation(se);
117         } catch (Exception e) {
118             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
119         }
120     }
121
122     @Override
123     public Object invokeMethod(Object o, String s, Object... objects)
124             throws ScriptException, NoSuchMethodException, NullPointerException, IllegalArgumentException {
125         try {
126             beforeInvocation();
127             return afterInvocation(super.invokeMethod(o, s, objects));
128         } catch (ScriptException se) {
129             throw (ScriptException) afterThrowsInvocation(se);
130         } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeMethod to avoid
131                                             // deadlocks
132             throw (NoSuchMethodException) afterThrowsInvocation(e);
133         } catch (NullPointerException e) {
134             throw (NullPointerException) afterThrowsInvocation(e);
135         } catch (IllegalArgumentException e) {
136             throw (IllegalArgumentException) afterThrowsInvocation(e);
137         } catch (Exception e) {
138             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
139         }
140     }
141
142     @Override
143     public Object invokeFunction(String s, Object... objects)
144             throws ScriptException, NoSuchMethodException, NullPointerException {
145         try {
146             beforeInvocation();
147             return afterInvocation(super.invokeFunction(s, objects));
148         } catch (ScriptException se) {
149             throw (ScriptException) afterThrowsInvocation(se);
150         } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeFunction to avoid
151                                             // deadlocks
152             throw (NoSuchMethodException) afterThrowsInvocation(e);
153         } catch (NullPointerException e) {
154             throw (NullPointerException) afterThrowsInvocation(e);
155         } catch (Exception e) {
156             throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
157         }
158     }
159 }