]> git.basschouten.com Git - openhab-addons.git/blob
cefa9b3e023d3fc15a5b37121785f1655e24a6a9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 package org.openhab.automation.jrubyscripting.internal;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
21 import javax.script.ScriptEngine;
22
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;
31
32 /**
33  * This is an implementation of a {@link ScriptEngineFactory} for Ruby.
34  *
35  * @author Brian O'Connell - Initial contribution
36  */
37 @NonNullByDefault
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 {
41
42     private final JRubyScriptEngineConfiguration configuration = new JRubyScriptEngineConfiguration();
43
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",
50             "lifecycleTracker");
51
52     private final javax.script.ScriptEngineFactory factory = new org.jruby.embed.jsr223.JRubyEngineFactory();
53
54     private final List<String> scriptTypes = Stream
55             .concat(factory.getExtensions().stream(), factory.getMimeTypes().stream())
56             .collect(Collectors.toUnmodifiableList());
57
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());
62         } else {
63             return entry;
64         }
65     }
66
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());
71         } else {
72             return entry;
73         }
74     }
75
76     // The activate call activates the automation and sets its configuration
77     @Activate
78     protected void activate(Map<String, Object> config) {
79         configuration.update(config, factory);
80     }
81
82     // The modified call updates configuration for the automation
83     @Modified
84     protected void modified(Map<String, Object> config) {
85         configuration.update(config, factory);
86     }
87
88     @Override
89     public List<String> getScriptTypes() {
90         return scriptTypes;
91     }
92
93     @Override
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 = //
97                 scopeValues //
98                         .entrySet() //
99                         .stream() //
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())); //
104
105         super.scopeValues(scriptEngine, filteredScopeValues);
106     }
107
108     @Override
109     public @Nullable ScriptEngine createScriptEngine(String scriptType) {
110         return scriptTypes.contains(scriptType) ? configuration.configureRubyEnvironment(factory.getScriptEngine())
111                 : null;
112     }
113 }