[automation] Added API for ScriptEngineFactory implementations to pull presets (#1256)

* Added API for ScriptEngineFactory implementations to pull presets

Existing API only allows engine factory implementors to request presets are pushed into existing scopes, rather than returning them directory, so that they can be bound into module or library systems. This change allows implementors to capture the presets requested (and potentially ignore any pushed), and therefore expose them via idiomatic import mechanisms for the language being implemented

Signed-off-by: Jonathan Gilbert <jpg@trillica.com>
pull/1403/head
Jonathan Gilbert 2020-03-28 08:22:04 +11:00 committed by GitHub
parent 15b3b07614
commit caf912bc99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 7 deletions

View File

@ -12,10 +12,7 @@
*/
package org.openhab.core.automation.module.script.internal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.script.ScriptEngine;
@ -100,15 +97,18 @@ public class ScriptExtensionManager {
}
}
public void importPreset(String preset, ScriptEngineFactory engineProvider, ScriptEngine scriptEngine,
public Map<String, Object> importPreset(String preset, ScriptEngineFactory engineProvider, ScriptEngine scriptEngine,
String scriptIdentifier) {
Map<String, Object> allValues = new HashMap<>();
for (ScriptExtensionProvider provider : scriptExtensionProviders) {
if (provider.getPresets().contains(preset)) {
Map<String, Object> scopeValues = provider.importPreset(scriptIdentifier, preset);
engineProvider.scopeValues(scriptEngine, scopeValues);
allValues.putAll(scopeValues);
}
}
return allValues;
}
public void dispose(String scriptIdentifier) {

View File

@ -13,9 +13,11 @@
package org.openhab.core.automation.module.script.internal;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.automation.module.script.ScriptEngineContainer;
import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.openhab.core.automation.module.script.ScriptExtensionProvider;
/**
@ -56,7 +58,22 @@ public class ScriptExtensionManagerWrapper {
return manager.getDefaultPresets();
}
public void importPreset(String preset) {
manager.importPreset(preset, container.getFactory(), container.getScriptEngine(), container.getIdentifier());
/**
* Imports a collection of named host objects/classes into a script engine instance. Sets of objects are provided
* under their object name, and categorized by preset name. This method will import all named objects for a specific
* preset name.
*
* @implNote This call both returns the imported objects, and requests that the {@link ScriptEngineFactory} import them.
* The mechanism of how they are imported by the ScriptEngineFactory, or whether they are imported at all (aside from
* being returned by this call) is dependent of the implementation of the ScriptEngineFactory.
*
* @apiNote Objects may appear in multiple named presets.
* @see ScriptExtensionManager
*
* @param preset the name of the preset to import
* @return a map of host object names to objects
*/
public Map<String, Object> importPreset(String preset) {
return manager.importPreset(preset, container.getFactory(), container.getScriptEngine(), container.getIdentifier());
}
}