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