Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[automation] Extracted accessor into interface and added to script engine context … #1843

Merged
merged 1 commit into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String, Object> findPreset(String preset, String scriptIdentifier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,6 +145,8 @@ private boolean isCustomFactory(ScriptEngineFactory engineFactory) {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand All @@ -37,7 +38,7 @@
*/
@Component(service = ScriptExtensionManager.class)
@NonNullByDefault
public class ScriptExtensionManager {
public class ScriptExtensionManager implements ScriptExtensionAccessor {

private final Set<ScriptExtensionProvider> scriptExtensionProviders = new CopyOnWriteArraySet<>();

Expand Down Expand Up @@ -100,19 +101,36 @@ public List<String> getDefaultPresets() {

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<String, Object> importPreset(String preset, ScriptEngineFactory engineProvider,
ScriptEngine scriptEngine, String scriptIdentifier) {

Map<String, Object> rv = findPreset(preset, scriptIdentifier);

engineProvider.scopeValues(scriptEngine, rv);

return rv;
}

public Map<String, Object> findDefaultPresets(String scriptIdentifier) {
Map<String, Object> allValues = new HashMap<>();

for (String preset : getDefaultPresets()) {
allValues.putAll(findPreset(preset, scriptIdentifier));
}

return allValues;
}

public Map<String, Object> findPreset(String preset, 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);
}
}
Expand Down