2 * Copyright (c) 2010-2023 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);
57 } catch (Exception e) {
58 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
63 public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
66 return afterInvocation(super.eval(reader, scriptContext));
67 } catch (ScriptException se) {
68 throw (ScriptException) afterThrowsInvocation(se);
69 } catch (Exception e) {
70 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
75 public Object eval(String s) throws ScriptException {
78 return afterInvocation(super.eval(s));
79 } catch (ScriptException se) {
80 throw (ScriptException) afterThrowsInvocation(se);
81 } catch (Exception e) {
82 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
87 public Object eval(Reader reader) throws ScriptException {
90 return afterInvocation(super.eval(reader));
91 } catch (ScriptException se) {
92 throw (ScriptException) afterThrowsInvocation(se);
93 } catch (Exception e) {
94 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
99 public Object eval(String s, Bindings bindings) throws ScriptException {
102 return afterInvocation(super.eval(s, bindings));
103 } catch (ScriptException se) {
104 throw (ScriptException) afterThrowsInvocation(se);
105 } catch (Exception e) {
106 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
111 public Object eval(Reader reader, Bindings bindings) throws ScriptException {
114 return afterInvocation(super.eval(reader, bindings));
115 } catch (ScriptException se) {
116 throw (ScriptException) afterThrowsInvocation(se);
117 } catch (Exception e) {
118 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
123 public Object invokeMethod(Object o, String s, Object... objects)
124 throws ScriptException, NoSuchMethodException, NullPointerException, IllegalArgumentException {
127 return afterInvocation(super.invokeMethod(o, s, objects));
128 } catch (ScriptException se) {
129 throw (ScriptException) afterThrowsInvocation(se);
130 } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeMethod to avoid
132 throw (NoSuchMethodException) afterThrowsInvocation(e);
133 } catch (NullPointerException e) {
134 throw (NullPointerException) afterThrowsInvocation(e);
135 } catch (IllegalArgumentException e) {
136 throw (IllegalArgumentException) afterThrowsInvocation(e);
137 } catch (Exception e) {
138 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
143 public Object invokeFunction(String s, Object... objects)
144 throws ScriptException, NoSuchMethodException, NullPointerException {
147 return afterInvocation(super.invokeFunction(s, objects));
148 } catch (ScriptException se) {
149 throw (ScriptException) afterThrowsInvocation(se);
150 } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeFunction to avoid
152 throw (NoSuchMethodException) afterThrowsInvocation(e);
153 } catch (NullPointerException e) {
154 throw (NullPointerException) afterThrowsInvocation(e);
155 } catch (Exception e) {
156 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions