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 <jpg@trillica.com>pull/1864/head
parent
9e89a4dbda
commit
e8d017da77
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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<ScriptExtensionProvider> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue