]> git.basschouten.com Git - openhab-addons.git/blob
082a98dde154adaec6ff33855a82737de3da9574
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 import org.eclipse.jdt.annotation.NonNullByDefault;
25
26 /**
27  * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
28  * being thrown.
29  *
30  * @param <T> The delegate class
31  * @author Jonathan Gilbert - Initial contribution
32  */
33 @NonNullByDefault
34 public abstract class InvocationInterceptingScriptEngineWithInvocable<T extends ScriptEngine & Invocable>
35         extends DelegatingScriptEngineWithInvocable<T> {
36
37     public InvocationInterceptingScriptEngineWithInvocable(T delegate) {
38         super(delegate);
39     }
40
41     protected void beforeInvocation() {
42     }
43
44     protected ScriptException afterThrowsInvocation(ScriptException se) {
45         return se;
46     }
47
48     @Override
49     public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
50         try {
51             beforeInvocation();
52             return super.eval(s, scriptContext);
53         } catch (ScriptException se) {
54             throw afterThrowsInvocation(se);
55         }
56     }
57
58     @Override
59     public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
60         try {
61             beforeInvocation();
62             return super.eval(reader, scriptContext);
63         } catch (ScriptException se) {
64             throw afterThrowsInvocation(se);
65         }
66     }
67
68     @Override
69     public Object eval(String s) throws ScriptException {
70         try {
71             beforeInvocation();
72             return super.eval(s);
73         } catch (ScriptException se) {
74             throw afterThrowsInvocation(se);
75         }
76     }
77
78     @Override
79     public Object eval(Reader reader) throws ScriptException {
80         try {
81             beforeInvocation();
82             return super.eval(reader);
83         } catch (ScriptException se) {
84             throw afterThrowsInvocation(se);
85         }
86     }
87
88     @Override
89     public Object eval(String s, Bindings bindings) throws ScriptException {
90         try {
91             beforeInvocation();
92             return super.eval(s, bindings);
93         } catch (ScriptException se) {
94             throw afterThrowsInvocation(se);
95         }
96     }
97
98     @Override
99     public Object eval(Reader reader, Bindings bindings) throws ScriptException {
100         try {
101             beforeInvocation();
102             return super.eval(reader, bindings);
103         } catch (ScriptException se) {
104             throw afterThrowsInvocation(se);
105         }
106     }
107
108     @Override
109     public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
110         try {
111             beforeInvocation();
112             return super.invokeMethod(o, s, objects);
113         } catch (ScriptException se) {
114             throw afterThrowsInvocation(se);
115         }
116     }
117
118     @Override
119     public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
120         try {
121             beforeInvocation();
122             return super.invokeFunction(s, objects);
123         } catch (ScriptException se) {
124             throw afterThrowsInvocation(se);
125         }
126     }
127 }