2 * Copyright (c) 2010-2024 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
13 package org.openhab.automation.jsscripting.internal.scriptengine;
15 import java.io.Reader;
16 import java.lang.reflect.UndeclaredThrowableException;
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 InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable>
32 extends DelegatingScriptEngineWithInvocableAndAutocloseable<T> {
34 public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) {
38 protected void beforeInvocation() {
41 protected Object afterInvocation(Object obj) {
45 protected Exception afterThrowsInvocation(Exception e) {
50 public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
53 return afterInvocation(super.eval(s, scriptContext));
54 } catch (ScriptException se) {
55 throw (ScriptException) afterThrowsInvocation(se);
56 } catch (Exception e) {
57 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
62 public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
65 return afterInvocation(super.eval(reader, scriptContext));
66 } catch (ScriptException se) {
67 throw (ScriptException) afterThrowsInvocation(se);
68 } catch (Exception e) {
69 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
74 public Object eval(String s) throws ScriptException {
77 return afterInvocation(super.eval(s));
78 } catch (ScriptException se) {
79 throw (ScriptException) afterThrowsInvocation(se);
80 } catch (Exception e) {
81 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
86 public Object eval(Reader reader) throws ScriptException {
89 return afterInvocation(super.eval(reader));
90 } catch (ScriptException se) {
91 throw (ScriptException) afterThrowsInvocation(se);
92 } catch (Exception e) {
93 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
98 public Object eval(String s, Bindings bindings) throws ScriptException {
101 return afterInvocation(super.eval(s, bindings));
102 } catch (ScriptException se) {
103 throw (ScriptException) afterThrowsInvocation(se);
104 } catch (Exception e) {
105 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
110 public Object eval(Reader reader, Bindings bindings) throws ScriptException {
113 return afterInvocation(super.eval(reader, bindings));
114 } catch (ScriptException se) {
115 throw (ScriptException) afterThrowsInvocation(se);
116 } catch (Exception e) {
117 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
122 public Object invokeMethod(Object o, String s, Object... objects)
123 throws ScriptException, NoSuchMethodException, NullPointerException, IllegalArgumentException {
126 return afterInvocation(super.invokeMethod(o, s, objects));
127 } catch (ScriptException se) {
128 throw (ScriptException) afterThrowsInvocation(se);
129 } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeMethod to avoid
131 throw (NoSuchMethodException) afterThrowsInvocation(e);
132 } catch (NullPointerException e) {
133 throw (NullPointerException) afterThrowsInvocation(e);
134 } catch (IllegalArgumentException e) {
135 throw (IllegalArgumentException) afterThrowsInvocation(e);
136 } catch (Exception e) {
137 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
142 public Object invokeFunction(String s, Object... objects)
143 throws ScriptException, NoSuchMethodException, NullPointerException {
146 return afterInvocation(super.invokeFunction(s, objects));
147 } catch (ScriptException se) {
148 throw (ScriptException) afterThrowsInvocation(se);
149 } catch (NoSuchMethodException e) { // Make sure to unlock on exceptions from Invocable.invokeFunction to avoid
151 throw (NoSuchMethodException) afterThrowsInvocation(e);
152 } catch (NullPointerException e) {
153 throw (NullPointerException) afterThrowsInvocation(e);
154 } catch (Exception e) {
155 throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions