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;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
27 * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
30 * @param <T> The delegate class
31 * @author Jonathan Gilbert - Initial contribution
34 public abstract class InvocationInterceptingScriptEngineWithInvocable<T extends ScriptEngine & Invocable>
35 extends DelegatingScriptEngineWithInvocable<T> {
37 public InvocationInterceptingScriptEngineWithInvocable(T delegate) {
41 protected void beforeInvocation() {
44 protected ScriptException afterThrowsInvocation(ScriptException se) {
49 public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
52 return super.eval(s, scriptContext);
53 } catch (ScriptException se) {
54 throw afterThrowsInvocation(se);
59 public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
62 return super.eval(reader, scriptContext);
63 } catch (ScriptException se) {
64 throw afterThrowsInvocation(se);
69 public Object eval(String s) throws ScriptException {
73 } catch (ScriptException se) {
74 throw afterThrowsInvocation(se);
79 public Object eval(Reader reader) throws ScriptException {
82 return super.eval(reader);
83 } catch (ScriptException se) {
84 throw afterThrowsInvocation(se);
89 public Object eval(String s, Bindings bindings) throws ScriptException {
92 return super.eval(s, bindings);
93 } catch (ScriptException se) {
94 throw afterThrowsInvocation(se);
99 public Object eval(Reader reader, Bindings bindings) throws ScriptException {
102 return super.eval(reader, bindings);
103 } catch (ScriptException se) {
104 throw afterThrowsInvocation(se);
109 public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
112 return super.invokeMethod(o, s, objects);
113 } catch (ScriptException se) {
114 throw afterThrowsInvocation(se);
119 public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
122 return super.invokeFunction(s, objects);
123 } catch (ScriptException se) {
124 throw afterThrowsInvocation(se);