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
13 package org.openhab.automation.jrubyscripting.internal;
15 import java.util.List;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 import javax.script.ScriptEngine;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.automation.module.script.AbstractScriptEngineFactory;
26 import org.openhab.core.automation.module.script.ScriptEngineFactory;
27 import org.openhab.core.config.core.ConfigurableService;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Modified;
33 * This is an implementation of a {@link ScriptEngineFactory} for Ruby.
35 * @author Brian O'Connell - Initial contribution
38 @Component(service = ScriptEngineFactory.class, configurationPid = "org.openhab.automation.jrubyscripting")
39 @ConfigurableService(category = "automation", label = "JRuby Scripting", description_uri = "automation:jruby")
40 public class JRubyScriptEngineFactory extends AbstractScriptEngineFactory {
42 private final JRubyScriptEngineConfiguration configuration = new JRubyScriptEngineConfiguration();
44 // Filter out the File entry to prevent shadowing the Ruby File class which breaks Ruby in spectacularly
45 // difficult ways to debug.
46 private static final Set<String> FILTERED_PRESETS = Set.of("File");
47 private static final Set<String> INSTANCE_PRESETS = Set.of();
48 private static final Set<String> GLOBAL_PRESETS = Set.of("scriptExtension", "automationManager", "ruleRegistry",
49 "items", "voice", "rules", "things", "events", "itemRegistry", "ir", "actions", "se", "audio",
52 private final javax.script.ScriptEngineFactory factory = new org.jruby.embed.jsr223.JRubyEngineFactory();
54 private final List<String> scriptTypes = Stream
55 .concat(factory.getExtensions().stream(), factory.getMimeTypes().stream())
56 .collect(Collectors.toUnmodifiableList());
58 // Adds @ in front of a set of variables so that Ruby recogonizes them as instance variables
59 private static Map.Entry<String, Object> mapInstancePresets(Map.Entry<String, Object> entry) {
60 if (INSTANCE_PRESETS.contains(entry.getKey())) {
61 return Map.entry("@" + entry.getKey(), entry.getValue());
67 // Adds $ in front of a set of variables so that Ruby recogonizes them as global variables
68 private static Map.Entry<String, Object> mapGlobalPresets(Map.Entry<String, Object> entry) {
69 if (GLOBAL_PRESETS.contains(entry.getKey())) {
70 return Map.entry("$" + entry.getKey(), entry.getValue());
76 // The activate call activates the automation and sets its configuration
78 protected void activate(Map<String, Object> config) {
79 configuration.update(config, factory);
82 // The modified call updates configuration for the automation
84 protected void modified(Map<String, Object> config) {
85 configuration.update(config, factory);
89 public List<String> getScriptTypes() {
94 public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValues) {
95 // Empty comments prevent the formatter from breaking up the correct streams chaining
96 Map<String, Object> filteredScopeValues = //
100 .filter(map -> !FILTERED_PRESETS.contains(map.getKey())) //
101 .map(JRubyScriptEngineFactory::mapInstancePresets) //
102 .map(JRubyScriptEngineFactory::mapGlobalPresets) //
103 .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); //
105 super.scopeValues(scriptEngine, filteredScopeValues);
109 public @Nullable ScriptEngine createScriptEngine(String scriptType) {
110 return scriptTypes.contains(scriptType) ? configuration.configureRubyEnvironment(factory.getScriptEngine())