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.jrubyscripting.internal;
15 import java.io.Reader;
16 import java.util.Objects;
18 import javax.script.Bindings;
19 import javax.script.Compilable;
20 import javax.script.CompiledScript;
21 import javax.script.Invocable;
22 import javax.script.ScriptContext;
23 import javax.script.ScriptEngine;
24 import javax.script.ScriptEngineFactory;
25 import javax.script.ScriptException;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.jruby.embed.jsr223.JRubyEngine;
32 * This is a wrapper for {@link JRubyEngine}.
34 * The purpose of this class is to intercept the call to eval and save the context into
35 * a global variable for use in the helper library.
37 * @author Jimmy Tanagra - Initial contribution
40 public class JRubyEngineWrapper implements Compilable, Invocable, ScriptEngine {
42 private final JRubyEngine engine;
44 private static final String CONTEXT_VAR_NAME = "ctx";
45 private static final String GLOBAL_VAR_NAME = "$" + CONTEXT_VAR_NAME;
47 JRubyEngineWrapper(JRubyEngine engine) {
48 this.engine = Objects.requireNonNull(engine);
52 public CompiledScript compile(@Nullable String script) throws ScriptException {
53 return engine.compile(script);
57 public CompiledScript compile(@Nullable Reader reader) throws ScriptException {
58 return engine.compile(reader);
62 public Object eval(@Nullable String script, @Nullable ScriptContext context) throws ScriptException {
63 Object ctx = Objects.requireNonNull(context).getBindings(ScriptContext.ENGINE_SCOPE).get(CONTEXT_VAR_NAME);
66 return engine.eval(script, context);
69 context.setAttribute(GLOBAL_VAR_NAME, ctx, ScriptContext.ENGINE_SCOPE);
71 return engine.eval(script, context);
73 context.removeAttribute(GLOBAL_VAR_NAME, ScriptContext.ENGINE_SCOPE);
78 public Object eval(@Nullable Reader reader, @Nullable ScriptContext context) throws ScriptException {
79 Object ctx = Objects.requireNonNull(context).getBindings(ScriptContext.ENGINE_SCOPE).get(CONTEXT_VAR_NAME);
82 return engine.eval(reader, context);
85 context.setAttribute(GLOBAL_VAR_NAME, ctx, ScriptContext.ENGINE_SCOPE);
87 return engine.eval(reader, context);
89 context.removeAttribute(GLOBAL_VAR_NAME, ScriptContext.ENGINE_SCOPE);
94 public Object eval(@Nullable String script, @Nullable Bindings bindings) throws ScriptException {
95 Object ctx = Objects.requireNonNull(bindings).get(CONTEXT_VAR_NAME);
98 return engine.eval(script, bindings);
101 bindings.put(GLOBAL_VAR_NAME, ctx);
103 return engine.eval(script, bindings);
105 bindings.remove(GLOBAL_VAR_NAME);
110 public Object eval(@Nullable Reader reader, @Nullable Bindings bindings) throws ScriptException {
111 Object ctx = Objects.requireNonNull(bindings).get(CONTEXT_VAR_NAME);
114 return engine.eval(reader, bindings);
117 bindings.put(GLOBAL_VAR_NAME, ctx);
119 return engine.eval(reader, bindings);
121 bindings.remove(GLOBAL_VAR_NAME);
126 public Object eval(@Nullable String script) throws ScriptException {
127 Object ctx = getBindings(ScriptContext.ENGINE_SCOPE).get(CONTEXT_VAR_NAME);
130 return engine.eval(script);
133 getContext().setAttribute(GLOBAL_VAR_NAME, ctx, ScriptContext.ENGINE_SCOPE);
135 return engine.eval(script);
137 getContext().removeAttribute(GLOBAL_VAR_NAME, ScriptContext.ENGINE_SCOPE);
142 public Object eval(@Nullable Reader reader) throws ScriptException {
143 Object ctx = getBindings(ScriptContext.ENGINE_SCOPE).get(CONTEXT_VAR_NAME);
146 return engine.eval(reader);
149 getContext().setAttribute(GLOBAL_VAR_NAME, ctx, ScriptContext.ENGINE_SCOPE);
151 return engine.eval(reader);
153 getContext().removeAttribute(GLOBAL_VAR_NAME, ScriptContext.ENGINE_SCOPE);
158 public Object get(@Nullable String key) {
159 return engine.get(key);
163 public void put(@Nullable String key, @Nullable Object value) {
164 engine.put(key, value);
168 public Bindings getBindings(int scope) {
169 return engine.getBindings(scope);
173 public void setBindings(@Nullable Bindings bindings, int scope) {
174 engine.setBindings(bindings, scope);
178 public Bindings createBindings() {
179 return engine.createBindings();
183 public ScriptContext getContext() {
184 return engine.getContext();
188 public void setContext(@Nullable ScriptContext context) {
189 engine.setContext(context);
193 public ScriptEngineFactory getFactory() {
194 return engine.getFactory();
198 public Object invokeMethod(@Nullable Object receiver, @Nullable String method, Object @Nullable... args)
199 throws ScriptException, NoSuchMethodException {
200 return engine.invokeMethod(receiver, method, args);
204 public Object invokeFunction(@Nullable String method, Object @Nullable... args)
205 throws ScriptException, NoSuchMethodException {
206 return engine.invokeFunction(method, args);
210 public <T> T getInterface(@Nullable Class<T> returnType) {
211 return engine.getInterface(returnType);
215 public <T> T getInterface(@Nullable Object receiver, @Nullable Class<T> returnType) {
216 return engine.getInterface(receiver, returnType);