From e8d017da773493192e776b11f3b2140ce6c8b19c Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Sun, 29 Nov 2020 09:24:19 +1100 Subject: [PATCH] Extracted accessor into interface and added to script engine context to allow access to script extensions from script engine factories (#1843) Signed-off-by: Jonathan Gilbert --- .../module/script/ScriptEngineFactory.java | 5 +++ .../script/ScriptExtensionAccessor.java | 43 +++++++++++++++++++ .../internal/ScriptEngineManagerImpl.java | 3 ++ .../internal/ScriptExtensionManager.java | 28 +++++++++--- 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptExtensionAccessor.java diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptEngineFactory.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptEngineFactory.java index 49d2a8094a..e7adcc58a4 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptEngineFactory.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptEngineFactory.java @@ -40,6 +40,11 @@ public interface ScriptEngineFactory { */ String CONTEXT_KEY_ENGINE_IDENTIFIER = "oh.engine-identifier"; + /** + * Key to access Extension Accessor {@link ScriptExtensionAccessor} + */ + String CONTEXT_KEY_EXTENSION_ACCESSOR = "oh.extension-accessor"; + /** * This method returns a list of file extensions and MimeTypes that are supported by the ScriptEngine, e.g. py, * application/python, js, application/javascript, etc. diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptExtensionAccessor.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptExtensionAccessor.java new file mode 100644 index 0000000000..b2fb38a5e5 --- /dev/null +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/ScriptExtensionAccessor.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.automation.module.script; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * Accessor allowing script engines to lookup presets. + * + * @author Jonathan Gilbert - Initial contribution + */ +@NonNullByDefault +public interface ScriptExtensionAccessor { + + /** + * Access the default presets for a script engine + * + * @param scriptIdentifier the identifier for the script engine + * @return map of preset objects + */ + Map findDefaultPresets(String scriptIdentifier); + + /** + * Access specific presets for a script engine + * + * @param preset the name of the preset + * @param scriptIdentifier the identifier for the script engine + * @return map of preset objects + */ + Map findPreset(String preset, String scriptIdentifier); +} diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java index 6693ca5e9d..7f7f3c6031 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java @@ -13,6 +13,7 @@ package org.openhab.core.automation.module.script.internal; import static org.openhab.core.automation.module.script.ScriptEngineFactory.CONTEXT_KEY_ENGINE_IDENTIFIER; +import static org.openhab.core.automation.module.script.ScriptEngineFactory.CONTEXT_KEY_EXTENSION_ACCESSOR; import java.io.InputStreamReader; import java.util.HashMap; @@ -144,6 +145,8 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager { scriptContext.setAttribute(CONTEXT_KEY_ENGINE_IDENTIFIER, engineIdentifier, ScriptContext.ENGINE_SCOPE); + scriptContext.setAttribute(CONTEXT_KEY_EXTENSION_ACCESSOR, scriptExtensionManager, + ScriptContext.ENGINE_SCOPE); } else { logger.error("ScriptEngine for language '{}' could not be created for identifier: {}", scriptType, engineIdentifier); diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptExtensionManager.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptExtensionManager.java index a66d1e5716..b4a415a769 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptExtensionManager.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptExtensionManager.java @@ -24,6 +24,7 @@ import javax.script.ScriptEngine; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.automation.module.script.ScriptEngineFactory; +import org.openhab.core.automation.module.script.ScriptExtensionAccessor; import org.openhab.core.automation.module.script.ScriptExtensionProvider; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -37,7 +38,7 @@ import org.osgi.service.component.annotations.ReferencePolicy; */ @Component(service = ScriptExtensionManager.class) @NonNullByDefault -public class ScriptExtensionManager { +public class ScriptExtensionManager implements ScriptExtensionAccessor { private final Set scriptExtensionProviders = new CopyOnWriteArraySet<>(); @@ -100,19 +101,36 @@ public class ScriptExtensionManager { public void importDefaultPresets(ScriptEngineFactory engineProvider, ScriptEngine scriptEngine, String scriptIdentifier) { - for (String preset : getDefaultPresets()) { - importPreset(preset, engineProvider, scriptEngine, scriptIdentifier); - } + + engineProvider.scopeValues(scriptEngine, findDefaultPresets(scriptIdentifier)); } public Map importPreset(String preset, ScriptEngineFactory engineProvider, ScriptEngine scriptEngine, String scriptIdentifier) { + + Map rv = findPreset(preset, scriptIdentifier); + + engineProvider.scopeValues(scriptEngine, rv); + + return rv; + } + + public Map findDefaultPresets(String scriptIdentifier) { + Map allValues = new HashMap<>(); + + for (String preset : getDefaultPresets()) { + allValues.putAll(findPreset(preset, scriptIdentifier)); + } + + return allValues; + } + + public Map findPreset(String preset, String scriptIdentifier) { Map allValues = new HashMap<>(); for (ScriptExtensionProvider provider : scriptExtensionProviders) { if (provider.getPresets().contains(preset)) { Map scopeValues = provider.importPreset(scriptIdentifier, preset); - engineProvider.scopeValues(scriptEngine, scopeValues); allValues.putAll(scopeValues); } }