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.util.Objects;
19 import javax.script.Bindings;
20 import javax.script.Invocable;
21 import javax.script.ScriptContext;
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptEngineFactory;
24 import javax.script.ScriptException;
26 import org.eclipse.jdt.annotation.Nullable;
29 * {@link ScriptEngine} implementation that delegates to a supplied ScriptEngine instance. Allows overriding specific
32 * @author Jonathan Gilbert - Initial contribution
34 public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable>
35 implements ScriptEngine, Invocable, AutoCloseable {
38 public DelegatingScriptEngineWithInvocableAndAutocloseable(T delegate) {
39 this.delegate = delegate;
43 public @Nullable Object eval(String s, ScriptContext scriptContext) throws ScriptException {
44 return Objects.nonNull(delegate) ? delegate.eval(s, scriptContext) : null;
48 public @Nullable Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
49 return Objects.nonNull(delegate) ? delegate.eval(reader, scriptContext) : null;
53 public @Nullable Object eval(String s) throws ScriptException {
54 return Objects.nonNull(delegate) ? delegate.eval(s) : null;
58 public @Nullable Object eval(Reader reader) throws ScriptException {
59 return Objects.nonNull(delegate) ? delegate.eval(reader) : null;
63 public @Nullable Object eval(String s, Bindings bindings) throws ScriptException {
64 return Objects.nonNull(delegate) ? delegate.eval(s, bindings) : null;
68 public @Nullable Object eval(Reader reader, Bindings bindings) throws ScriptException {
69 return Objects.nonNull(delegate) ? delegate.eval(reader, bindings) : null;
73 public void put(String s, Object o) {
74 if (Objects.nonNull(delegate))
79 public @Nullable Object get(String s) {
80 return Objects.nonNull(delegate) ? delegate.get(s) : null;
84 public @Nullable Bindings getBindings(int i) {
85 return Objects.nonNull(delegate) ? delegate.getBindings(i) : null;
89 public void setBindings(Bindings bindings, int i) {
90 if (Objects.nonNull(delegate))
91 delegate.setBindings(bindings, i);
95 public @Nullable Bindings createBindings() {
96 return Objects.nonNull(delegate) ? delegate.createBindings() : null;
100 public @Nullable ScriptContext getContext() {
101 return Objects.nonNull(delegate) ? delegate.getContext() : null;
105 public void setContext(ScriptContext scriptContext) {
106 if (Objects.nonNull(delegate))
107 delegate.setContext(scriptContext);
111 public @Nullable ScriptEngineFactory getFactory() {
112 return Objects.nonNull(delegate) ? delegate.getFactory() : null;
116 public @Nullable Object invokeMethod(Object o, String s, Object... objects)
117 throws ScriptException, NoSuchMethodException {
118 return Objects.nonNull(delegate) ? delegate.invokeMethod(o, s, objects) : null;
122 public @Nullable Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
123 return Objects.nonNull(delegate) ? delegate.invokeFunction(s, objects) : null;
127 public <T> T getInterface(Class<T> aClass) {
128 return delegate.getInterface(aClass);
132 public <T> T getInterface(Object o, Class<T> aClass) {
133 return delegate.getInterface(o, aClass);
137 public void close() throws Exception {
138 if (Objects.nonNull(delegate))