Skip to content

Commit

Permalink
Move ConfigDescription to ScriptTransformationService
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Mar 28, 2023
1 parent acb10ce commit e9f79b3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -41,6 +42,10 @@
import org.openhab.core.automation.module.script.profile.ScriptProfile;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.common.registry.RegistryChangeListener;
import org.openhab.core.config.core.ConfigDescription;
import org.openhab.core.config.core.ConfigDescriptionBuilder;
import org.openhab.core.config.core.ConfigDescriptionProvider;
import org.openhab.core.config.core.ConfigDescriptionRegistry;
import org.openhab.core.config.core.ConfigOptionProvider;
import org.openhab.core.config.core.ConfigParser;
import org.openhab.core.config.core.ParameterOption;
Expand All @@ -63,12 +68,15 @@
*/
@NonNullByDefault
@Component(factory = "org.openhab.core.automation.module.script.transformation.factory", service = {
TransformationService.class, ScriptTransformationService.class, ConfigOptionProvider.class })
public class ScriptTransformationService
implements TransformationService, ConfigOptionProvider, RegistryChangeListener<Transformation> {
TransformationService.class, ScriptTransformationService.class, ConfigOptionProvider.class,
ConfigDescriptionProvider.class })
public class ScriptTransformationService implements TransformationService, ConfigOptionProvider,
ConfigDescriptionProvider, RegistryChangeListener<Transformation> {
public static final String SCRIPT_TYPE_PROPERTY_NAME = "openhab.transform.script.scriptType";
public static final String OPENHAB_TRANSFORMATION_SCRIPT = "openhab-transformation-script-";

private static final URI CONFIG_DESCRIPTION_TEMPLATE_URI = URI.create(PROFILE_CONFIG_URI_PREFIX + "SCRIPT");

private static final Pattern INLINE_SCRIPT_CONFIG_PATTERN = Pattern.compile("\\|(?<inlineScript>.+)");

private static final Pattern SCRIPT_CONFIG_PATTERN = Pattern.compile("(?<scriptUid>.+?)(\\?(?<params>.*?))?");
Expand All @@ -84,17 +92,20 @@ public class ScriptTransformationService

private final TransformationRegistry transformationRegistry;
private final ScriptEngineManager scriptEngineManager;
private final ConfigDescriptionRegistry configDescRegistry;

@Activate
public ScriptTransformationService(@Reference TransformationRegistry transformationRegistry,
@Reference ScriptEngineManager scriptEngineManager, Map<String, Object> config) {
@Reference ConfigDescriptionRegistry configDescRegistry, @Reference ScriptEngineManager scriptEngineManager,
Map<String, Object> config) {
String scriptType = ConfigParser.valueAs(config.get(SCRIPT_TYPE_PROPERTY_NAME), String.class);
if (scriptType == null) {
throw new IllegalStateException(
"'" + SCRIPT_TYPE_PROPERTY_NAME + "' must not be null in service configuration");
}

this.transformationRegistry = transformationRegistry;
this.configDescRegistry = configDescRegistry;
this.scriptEngineManager = scriptEngineManager;
this.scriptType = scriptType;
transformationRegistry.addRegistryChangeListener(this);
Expand Down Expand Up @@ -244,6 +255,36 @@ public void updated(Transformation oldElement, Transformation element) {
return null;
}

@Override
public Collection<ConfigDescription> getConfigDescriptions(@Nullable Locale locale) {
var configDescription = getConfigDescription(URI.create(PROFILE_CONFIG_URI_PREFIX + scriptType.toUpperCase()),
locale);
if (configDescription != null) {
return List.of(configDescription);
}

return Collections.emptyList();
}

@Override
public @Nullable ConfigDescription getConfigDescription(URI uri, @Nullable Locale locale) {
String uriString = uri.toString();
if (!uriString.startsWith(PROFILE_CONFIG_URI_PREFIX)) {
return null;
}
String transformationId = uriString.substring(PROFILE_CONFIG_URI_PREFIX.length());
if (!transformationId.equals(scriptType.toUpperCase())) {
return null;
}

ConfigDescription template = configDescRegistry.getConfigDescription(CONFIG_DESCRIPTION_TEMPLATE_URI, locale);
if (template == null) {
return null;
}
return ConfigDescriptionBuilder.create(uri).withParameters(template.getParameters())
.withParameterGroups(template.getParameterGroups()).build();
}

private void clearCache(String uid) {
ScriptRecord scriptRecord = scriptCache.remove(uid);
if (scriptRecord != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,16 @@
*/
package org.openhab.core.automation.module.script;

import static org.openhab.core.automation.module.script.profile.ScriptProfileFactory.PROFILE_CONFIG_URI_PREFIX;

import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import javax.script.ScriptEngine;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.module.script.internal.ScriptEngineFactoryHelper;
import org.openhab.core.config.core.ConfigDescription;
import org.openhab.core.config.core.ConfigDescriptionBuilder;
import org.openhab.core.config.core.ConfigDescriptionProvider;
import org.openhab.core.config.core.ConfigDescriptionRegistry;
import org.openhab.core.transform.TransformationService;
import org.osgi.service.component.ComponentFactory;
import org.osgi.service.component.ComponentInstance;
Expand All @@ -49,20 +38,17 @@
*
* @author Jimmy Tanagra - Initial contribution
*/
@Component(immediate = true, service = { ScriptTransformationServiceFactory.class, ConfigDescriptionProvider.class })
@Component(immediate = true, service = { ScriptTransformationServiceFactory.class })
@NonNullByDefault
public class ScriptTransformationServiceFactory implements ConfigDescriptionProvider {
private static final URI CONFIG_DESCRIPTION_TEMPLATE_URI = URI.create(PROFILE_CONFIG_URI_PREFIX + "SCRIPT");
public class ScriptTransformationServiceFactory {

private final ConfigDescriptionRegistry configDescRegistry;
private final ComponentFactory<ScriptTransformationService> scriptTransformationFactory;

private final Map<String, ComponentInstance<ScriptTransformationService>> scriptTransformations = new ConcurrentHashMap<>();

@Activate
public ScriptTransformationServiceFactory(@Reference ConfigDescriptionRegistry configDescRegistry,
public ScriptTransformationServiceFactory(
@Reference(target = "(component.factory=org.openhab.core.automation.module.script.transformation.factory)") ComponentFactory<ScriptTransformationService> factory) {
this.configDescRegistry = configDescRegistry;
this.scriptTransformationFactory = factory;
}

Expand Down Expand Up @@ -114,37 +100,4 @@ private void unregisterService(ComponentInstance<ScriptTransformationService> in
instance.getInstance().deactivate();
instance.dispose();
}

@Override
public Collection<ConfigDescription> getConfigDescriptions(@Nullable Locale locale) {
ConfigDescription template = configDescRegistry.getConfigDescription(CONFIG_DESCRIPTION_TEMPLATE_URI, locale);
if (template == null) {
return Collections.emptyList();
}

return scriptTransformations.keySet().stream()
.map(type -> ConfigDescriptionBuilder.create(URI.create(PROFILE_CONFIG_URI_PREFIX + type.toUpperCase()))
.withParameters(template.getParameters()).withParameterGroups(template.getParameterGroups())
.build())
.toList();
}

@Override
public @Nullable ConfigDescription getConfigDescription(URI uri, @Nullable Locale locale) {
String uriString = uri.toString();
if (!uriString.startsWith(PROFILE_CONFIG_URI_PREFIX)) {
return null;
}
String scriptType = uriString.substring(PROFILE_CONFIG_URI_PREFIX.length());
if (!scriptTransformations.containsKey(scriptType.toLowerCase())) {
return null;
}

ConfigDescription template = configDescRegistry.getConfigDescription(CONFIG_DESCRIPTION_TEMPLATE_URI, locale);
if (template == null) {
return null;
}
return ConfigDescriptionBuilder.create(uri).withParameters(template.getParameters())
.withParameterGroups(template.getParameterGroups()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.core.config.core.ConfigDescriptionRegistry;
import org.openhab.core.transform.Transformation;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationRegistry;
Expand Down Expand Up @@ -76,7 +77,8 @@ public class ScriptTransformationServiceTest {
public void setUp() throws ScriptException {
Map<String, Object> properties = new HashMap<>();
properties.put(ScriptTransformationService.SCRIPT_TYPE_PROPERTY_NAME, SCRIPT_LANGUAGE);
service = new ScriptTransformationService(transformationRegistry, scriptEngineManager, properties);
service = new ScriptTransformationService(transformationRegistry, mock(ConfigDescriptionRegistry.class),
scriptEngineManager, properties);

when(scriptEngineManager.createScriptEngine(eq(SCRIPT_LANGUAGE), any())).thenReturn(scriptEngineContainer);
when(scriptEngineManager.isSupported(anyString()))
Expand Down

0 comments on commit e9f79b3

Please sign in to comment.