*/
package org.openhab.automation.jrubyscripting.internal;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import javax.script.ScriptEngine;
+import javax.script.ScriptException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
.concat(factory.getExtensions().stream(), factory.getMimeTypes().stream())
.collect(Collectors.toUnmodifiableList());
- // Adds @ in front of a set of variables so that Ruby recogonizes them as instance variables
+ // Adds @ in front of a set of variables so that Ruby recognizes them as instance variables
private static Map.Entry<String, Object> mapInstancePresets(Map.Entry<String, Object> entry) {
if (INSTANCE_PRESETS.contains(entry.getKey())) {
return Map.entry("@" + entry.getKey(), entry.getValue());
}
}
- // Adds $ in front of a set of variables so that Ruby recogonizes them as global variables
+ // Adds $ in front of a set of variables so that Ruby recognizes them as global variables
private static Map.Entry<String, Object> mapGlobalPresets(Map.Entry<String, Object> entry) {
if (GLOBAL_PRESETS.contains(entry.getKey())) {
return Map.entry("$" + entry.getKey(), entry.getValue());
@Override
public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValues) {
// Empty comments prevent the formatter from breaking up the correct streams chaining
+ logger.debug("Scope Values: {}", scopeValues);
Map<String, Object> filteredScopeValues = //
scopeValues //
.entrySet() //
.map(JRubyScriptEngineFactory::mapGlobalPresets) //
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); //
- super.scopeValues(scriptEngine, filteredScopeValues);
+ Map<Boolean, Map<String, Object>> partitionedMap = //
+ filteredScopeValues.entrySet() //
+ .stream() //
+ .collect(Collectors.partitioningBy(entry -> (entry.getValue() instanceof Class),
+ Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
+
+ importClassesToRuby(scriptEngine, partitionedMap.getOrDefault(true, new HashMap<>()));
+ super.scopeValues(scriptEngine, partitionedMap.getOrDefault(false, new HashMap<>()));
+ }
+
+ private void importClassesToRuby(ScriptEngine scriptEngine, Map<String, Object> objects) {
+ String import_statements = objects.entrySet() //
+ .stream() //
+ .map(entry -> "java_import " + ((Class<?>) entry.getValue()).getName() + " rescue nil") //
+ .collect(Collectors.joining("\n"));
+ try {
+ scriptEngine.eval(import_statements);
+ } catch (ScriptException e) {
+ logger.debug("Error importing java classes", e);
+ }
}
@Override