Skip to content

Commit

Permalink
Add a special property to hold the scriptType
Browse files Browse the repository at this point in the history
This preserves the scriptType case instead of forcing it to lowercase

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Mar 27, 2023
1 parent 852ca2e commit dd98143
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
@NonNullByDefault
@Component(factory = "org.openhab.core.automation.module.script.transformation.factory")
public class ScriptTransformationService implements TransformationService, 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 Pattern INLINE_SCRIPT_CONFIG_PATTERN = Pattern.compile("\\|(?<inlineScript>.+)");
Expand All @@ -75,16 +76,15 @@ public class ScriptTransformationService implements TransformationService, Regis
@Activate
public ScriptTransformationService(@Reference TransformationRegistry transformationRegistry,
@Reference ScriptEngineManager scriptEngineManager, Map<String, Object> config) {
String servicePropertyName = ConfigParser.valueAs(config.get(TransformationService.SERVICE_PROPERTY_NAME),
String.class);
if (servicePropertyName == null) {
String scriptType = ConfigParser.valueAs(config.get(SCRIPT_TYPE_PROPERTY_NAME), String.class);
if (scriptType == null) {
throw new IllegalStateException(
"'" + TransformationService.SERVICE_PROPERTY_NAME + "' must not be null in service configuration");
"'" + SCRIPT_TYPE_PROPERTY_NAME + "' must not be null in service configuration");
}

this.transformationRegistry = transformationRegistry;
this.scriptEngineManager = scriptEngineManager;
this.scriptType = servicePropertyName.toLowerCase();
this.scriptType = scriptType;
transformationRegistry.addRegistryChangeListener(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
*
* @author Jimmy Tanagra - Initial contribution
*/
@Component(immediate = true, service = { ConfigDescriptionProvider.class, ConfigOptionProvider.class })
@Component(immediate = true, service = { ScriptTransformationServiceFactory.class, ConfigDescriptionProvider.class,
ConfigOptionProvider.class })
@NonNullByDefault
public class ScriptTransformationServiceFactory implements ConfigDescriptionProvider, ConfigOptionProvider {
private final Logger logger = LoggerFactory.getLogger(ScriptTransformationServiceFactory.class);
Expand Down Expand Up @@ -98,6 +99,7 @@ public void setScriptEngineFactory(ScriptEngineFactory engineFactory) {

Dictionary<String, Object> properties = new Hashtable<>();
properties.put(TransformationService.SERVICE_PROPERTY_NAME, type.toUpperCase());
properties.put(ScriptTransformationService.SCRIPT_TYPE_PROPERTY_NAME, type);
ComponentInstance<ScriptTransformationService> instance = scriptTransformationFactory
.newInstance(properties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.openhab.core.transform.Transformation;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationRegistry;
import org.openhab.core.transform.TransformationService;

/**
* The {@link ScriptTransformationServiceTest} holds tests for the {@link ScriptTransformationService}
Expand All @@ -51,7 +50,7 @@
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class ScriptTransformationServiceTest {
private static final String SCRIPT_LANGUAGE = "customdsl";
private static final String SCRIPT_LANGUAGE = "customDsl";
private static final String SCRIPT_UID = "scriptUid." + SCRIPT_LANGUAGE;
private static final String INVALID_SCRIPT_UID = "invalidScriptUid";

Expand All @@ -76,7 +75,7 @@ public class ScriptTransformationServiceTest {
@BeforeEach
public void setUp() throws ScriptException {
Map<String, Object> properties = new HashMap<>();
properties.put(TransformationService.SERVICE_PROPERTY_NAME, SCRIPT_LANGUAGE);
properties.put(ScriptTransformationService.SCRIPT_TYPE_PROPERTY_NAME, SCRIPT_LANGUAGE);
service = new ScriptTransformationService(transformationRegistry, scriptEngineManager, properties);

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

0 comments on commit dd98143

Please sign in to comment.