Skip to content

Commit

Permalink
Fix inital loadinf and improve code
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed Apr 27, 2024
1 parent 10a20af commit 3b42ba7
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.model.yaml.internal;

import static org.openhab.core.service.WatchService.Kind.CREATE;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -24,6 +26,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -52,6 +55,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;

/**
* The {@link YamlModelRepositoryImpl} is an OSGi service, that encapsulates all YAML file processing
Expand Down Expand Up @@ -82,7 +86,8 @@ public YamlModelRepositoryImpl(@Reference(target = WatchService.CONFIG_WATCHER_F
.disable(YAMLGenerator.Feature.SPLIT_LINES) // do not split long lines
.enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR) // indent arrays
.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) // use quotes only where necessary
.build();
.enable(YAMLParser.Feature.PARSE_BOOLEAN_LIKE_WORDS_AS_STRINGS).build(); // do not parse ON/OFF/... as
// booleans
this.objectMapper = new ObjectMapper(yamlFactory);
objectMapper.findAndRegisterModules();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
Expand All @@ -91,6 +96,13 @@ public YamlModelRepositoryImpl(@Reference(target = WatchService.CONFIG_WATCHER_F
this.watchService = watchService;
watchService.registerListener(this, Path.of(""));
watchPath = watchService.getWatchPath();

// read initial contents
try (Stream<Path> files = Files.walk(watchPath)) {
files.filter(Files::isRegularFile).map(watchPath::relativize).forEach(f -> processWatchEvent(CREATE, f));
} catch (IOException e) {
logger.warn("Could not list YAML files in '{}', models might be missing: {}", watchPath, e.getMessage());
}
}

@Deactivate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
@MockitoSettings(strictness = Strictness.LENIENT)
@NonNullByDefault
public class YamlModelRepositoryImplTest {
private static final Path SOURCE_PATH = Path.of("src/test/resources");
private static final Path SOURCE_PATH = Path.of("src/test/resources/model");
private static final String MODEL_NAME = "model";
private static final Path MODEL_PATH = Path.of(MODEL_NAME + ".yaml");

Expand Down

0 comments on commit 3b42ba7

Please sign in to comment.