2 * Copyright (c) 2010-2022 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;
17 import java.lang.reflect.UndeclaredThrowableException;
19 import javax.script.Bindings;
20 import javax.script.Invocable;
21 import javax.script.ScriptContext;
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptException;
26 * Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
29 * @param <T> The delegate class
30 * @author Jonathan Gilbert - Initial contribution
32 public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable>
33 extends DelegatingScriptEngineWithInvocableAndAutocloseable<T> {
35 public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) {
39 protected void beforeInvocation() {
42 protected Object afterInvocation(Object obj) {
46 protected Exception afterThrowsInvocation(Exception e) {
51 public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
54 return afterInvocation(super.eval(s, scriptContext));
55 } catch (ScriptException se) {
56 throw (ScriptException) afterThrowsInvocation(se);
61 public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
64 return afterInvocation(super.eval(reader, scriptContext));
65 } catch (ScriptException se) {
66 throw (ScriptException) afterThrowsInvocation(se);
71 public Object eval(String s) throws ScriptException {
74 return afterInvocation(super.eval(s));
75 } catch (ScriptException se) {
76 throw (ScriptException) afterThrowsInvocation(se);
81 public Object eval(Reader reader) throws ScriptException {
84 return afterInvocation(super.eval(reader));
85 } catch (ScriptException se) {
86 throw (ScriptException) afterThrowsInvocation(se);
91 public Object eval(String s, Bindings bindings) throws ScriptException {
94 return afterInvocation(super.eval(s, bindings));
95 } catch (ScriptException se) {
96 throw (ScriptException) afterThrowsInvocation(se);
101 public Object eval(Reader reader, Bindings bindings) throws ScriptException {
104 return afterInvocation(super.eval(reader, bindings));
105 } catch (ScriptException se) {
106 throw (ScriptException) afterThrowsInvocation(se);
111 public Object invokeMethod(Object o, String s, Object... objects)
112 throws ScriptException, NoSuchMethodException, NullPointerException, IllegalArgumentException {
115 return afterInvocation(super.invokeMethod(o, s, objects));
116 } catch (ScriptException se) {
117 throw (ScriptException) afterThrowsInvocation(se);
118 } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeMethod to avoid
120 throw (NoSuchMethodException) afterThrowsInvocation(e);
121 } catch (NullPointerException e) {
122 throw (NullPointerException) afterThrowsInvocation(e);
123 } catch (IllegalArgumentException e) {
124 throw (IllegalArgumentException) afterThrowsInvocation(e);
125 } catch (Exception e) {
126 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
131 public Object invokeFunction(String s, Object... objects)
132 throws ScriptException, NoSuchMethodException, NullPointerException {
135 return afterInvocation(super.invokeFunction(s, objects));
136 } catch (ScriptException se) {
137 throw (ScriptException) afterThrowsInvocation(se);
138 } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeFunction to avoid
140 throw (NoSuchMethodException) afterThrowsInvocation(e);
141 } catch (NullPointerException e) {
142 throw (NullPointerException) afterThrowsInvocation(e);
143 } catch (Exception e) {
144 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions