]> git.basschouten.com Git - openhab-addons.git/blob
e019722dae7733e7e96208be58be621de7878567
[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
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 InvocationInterceptingScriptEngineWithInvocable<T extends ScriptEngine & Invocable>
32         extends DelegatingScriptEngineWithInvocable<T> {
33
34     public InvocationInterceptingScriptEngineWithInvocable(T delegate) {
35         super(delegate);
36     }
37
38     protected void beforeInvocation() {
39     }
40
41     protected ScriptException afterThrowsInvocation(ScriptException se) {
42         return se;
43     }
44
45     @Override
46     public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
47         try {
48             beforeInvocation();
49             return super.eval(s, scriptContext);
50         } catch (ScriptException se) {
51             throw afterThrowsInvocation(se);
52         }
53     }
54
55     @Override
56     public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
57         try {
58             beforeInvocation();
59             return super.eval(reader, scriptContext);
60         } catch (ScriptException se) {
61             throw afterThrowsInvocation(se);
62         }
63     }
64
65     @Override
66     public Object eval(String s) throws ScriptException {
67         try {
68             beforeInvocation();
69             return super.eval(s);
70         } catch (ScriptException se) {
71             throw afterThrowsInvocation(se);
72         }
73     }
74
75     @Override
76     public Object eval(Reader reader) throws ScriptException {
77         try {
78             beforeInvocation();
79             return super.eval(reader);
80         } catch (ScriptException se) {
81             throw afterThrowsInvocation(se);
82         }
83     }
84
85     @Override
86     public Object eval(String s, Bindings bindings) throws ScriptException {
87         try {
88             beforeInvocation();
89             return super.eval(s, bindings);
90         } catch (ScriptException se) {
91             throw afterThrowsInvocation(se);
92         }
93     }
94
95     @Override
96     public Object eval(Reader reader, Bindings bindings) throws ScriptException {
97         try {
98             beforeInvocation();
99             return super.eval(reader, bindings);
100         } catch (ScriptException se) {
101             throw afterThrowsInvocation(se);
102         }
103     }
104
105     @Override
106     public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
107         try {
108             beforeInvocation();
109             return super.invokeMethod(o, s, objects);
110         } catch (ScriptException se) {
111             throw afterThrowsInvocation(se);
112         }
113     }
114
115     @Override
116     public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
117         try {
118             beforeInvocation();
119             return super.invokeFunction(s, objects);
120         } catch (ScriptException se) {
121             throw afterThrowsInvocation(se);
122         }
123     }
124 }