2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
14 package org.openhab.automation.jsscripting.internal.scriptengine;
16 import java.io.Reader;
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;
25 * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
28 * @param <T> The delegate class
29 * @author Jonathan Gilbert - Initial contribution
31 public abstract class InvocationInterceptingScriptEngineWithInvocable<T extends ScriptEngine & Invocable>
32 extends DelegatingScriptEngineWithInvocable<T> {
34 public InvocationInterceptingScriptEngineWithInvocable(T delegate) {
38 protected void beforeInvocation() {
41 protected ScriptException afterThrowsInvocation(ScriptException se) {
46 public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
49 return super.eval(s, scriptContext);
50 } catch (ScriptException se) {
51 throw afterThrowsInvocation(se);
56 public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
59 return super.eval(reader, scriptContext);
60 } catch (ScriptException se) {
61 throw afterThrowsInvocation(se);
66 public Object eval(String s) throws ScriptException {
70 } catch (ScriptException se) {
71 throw afterThrowsInvocation(se);
76 public Object eval(Reader reader) throws ScriptException {
79 return super.eval(reader);
80 } catch (ScriptException se) {
81 throw afterThrowsInvocation(se);
86 public Object eval(String s, Bindings bindings) throws ScriptException {
89 return super.eval(s, bindings);
90 } catch (ScriptException se) {
91 throw afterThrowsInvocation(se);
96 public Object eval(Reader reader, Bindings bindings) throws ScriptException {
99 return super.eval(reader, bindings);
100 } catch (ScriptException se) {
101 throw afterThrowsInvocation(se);
106 public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
109 return super.invokeMethod(o, s, objects);
110 } catch (ScriptException se) {
111 throw afterThrowsInvocation(se);
116 public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
119 return super.invokeFunction(s, objects);
120 } catch (ScriptException se) {
121 throw afterThrowsInvocation(se);