]> git.basschouten.com Git - openhab-addons.git/blob
f96ce621db4d172377335c74433f092501f880d9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13
14 package org.openhab.automation.jsscripting.internal.scriptengine;
15
16 import java.io.Reader;
17 import java.util.Objects;
18
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;
25
26 import org.eclipse.jdt.annotation.Nullable;
27
28 /**
29  * {@link ScriptEngine} implementation that delegates to a supplied ScriptEngine instance. Allows overriding specific
30  * methods.
31  *
32  * @author Jonathan Gilbert - Initial contribution
33  */
34 public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable>
35         implements ScriptEngine, Invocable, AutoCloseable {
36     protected T delegate;
37
38     public DelegatingScriptEngineWithInvocableAndAutocloseable(T delegate) {
39         this.delegate = delegate;
40     }
41
42     @Override
43     public @Nullable Object eval(String s, ScriptContext scriptContext) throws ScriptException {
44         return Objects.nonNull(delegate) ? delegate.eval(s, scriptContext) : null;
45     }
46
47     @Override
48     public @Nullable Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
49         return Objects.nonNull(delegate) ? delegate.eval(reader, scriptContext) : null;
50     }
51
52     @Override
53     public @Nullable Object eval(String s) throws ScriptException {
54         return Objects.nonNull(delegate) ? delegate.eval(s) : null;
55     }
56
57     @Override
58     public @Nullable Object eval(Reader reader) throws ScriptException {
59         return Objects.nonNull(delegate) ? delegate.eval(reader) : null;
60     }
61
62     @Override
63     public @Nullable Object eval(String s, Bindings bindings) throws ScriptException {
64         return Objects.nonNull(delegate) ? delegate.eval(s, bindings) : null;
65     }
66
67     @Override
68     public @Nullable Object eval(Reader reader, Bindings bindings) throws ScriptException {
69         return Objects.nonNull(delegate) ? delegate.eval(reader, bindings) : null;
70     }
71
72     @Override
73     public void put(String s, Object o) {
74         if (Objects.nonNull(delegate))
75             delegate.put(s, o);
76     }
77
78     @Override
79     public @Nullable Object get(String s) {
80         return Objects.nonNull(delegate) ? delegate.get(s) : null;
81     }
82
83     @Override
84     public @Nullable Bindings getBindings(int i) {
85         return Objects.nonNull(delegate) ? delegate.getBindings(i) : null;
86     }
87
88     @Override
89     public void setBindings(Bindings bindings, int i) {
90         if (Objects.nonNull(delegate))
91             delegate.setBindings(bindings, i);
92     }
93
94     @Override
95     public @Nullable Bindings createBindings() {
96         return Objects.nonNull(delegate) ? delegate.createBindings() : null;
97     }
98
99     @Override
100     public @Nullable ScriptContext getContext() {
101         return Objects.nonNull(delegate) ? delegate.getContext() : null;
102     }
103
104     @Override
105     public void setContext(ScriptContext scriptContext) {
106         if (Objects.nonNull(delegate))
107             delegate.setContext(scriptContext);
108     }
109
110     @Override
111     public @Nullable ScriptEngineFactory getFactory() {
112         return Objects.nonNull(delegate) ? delegate.getFactory() : null;
113     }
114
115     @Override
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;
119     }
120
121     @Override
122     public @Nullable Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
123         return Objects.nonNull(delegate) ? delegate.invokeFunction(s, objects) : null;
124     }
125
126     @Override
127     public <T> T getInterface(Class<T> aClass) {
128         return delegate.getInterface(aClass);
129     }
130
131     @Override
132     public <T> T getInterface(Object o, Class<T> aClass) {
133         return delegate.getInterface(o, aClass);
134     }
135
136     @Override
137     public void close() throws Exception {
138         if (Objects.nonNull(delegate))
139             delegate.close();
140     }
141 }