mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 13:21:53 +01:00
Fix inital loading and improve code for YAML model (#4199)
Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
parent
c3ada84b77
commit
551c06b24f
@ -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;
|
||||
@ -24,6 +26,7 @@ import java.util.Optional;
|
||||
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;
|
||||
@ -52,6 +55,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
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
|
||||
@ -82,7 +86,8 @@ public class YamlModelRepositoryImpl implements WatchService.WatchEventListener,
|
||||
.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);
|
||||
@ -91,6 +96,13 @@ public class YamlModelRepositoryImpl implements WatchService.WatchEventListener,
|
||||
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
|
||||
|
@ -51,7 +51,7 @@ import org.openhab.core.service.WatchService;
|
||||
@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");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user