From c36b8f40b553358978fa6de3642e7a044d427fd6 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sun, 9 Jul 2023 12:31:03 +0200 Subject: [PATCH] Move generic repository stuff to bundle org.openhab.core.model.core Signed-off-by: Laurent Garnier --- .../internal}/yaml/YamlModelRepository.java | 10 ++- .../core/model/core}/yaml/YamlElement.java | 15 +++- .../core/model/core}/yaml/YamlFile.java | 29 ++++++- .../model/core/yaml/YamlModelListener.java | 75 +++++++++++++++++++ .../model/core}/yaml/YamlParseException.java | 2 +- bundles/org.openhab.core.semantics/pom.xml | 5 ++ .../internal/config/yaml/YamlSemanticTag.java | 4 +- .../config/yaml/YamlSemanticTagProvider.java | 6 +- .../config/yaml/YamlSemanticTags.java | 6 +- .../model/yaml/YamlModelListener.java | 39 ---------- 10 files changed, 137 insertions(+), 54 deletions(-) rename bundles/{org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model => org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/internal}/yaml/YamlModelRepository.java (94%) rename bundles/{org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model => org.openhab.core.model.core/src/main/java/org/openhab/core/model/core}/yaml/YamlElement.java (56%) rename bundles/{org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model => org.openhab.core.model.core/src/main/java/org/openhab/core/model/core}/yaml/YamlFile.java (63%) create mode 100644 bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlModelListener.java rename bundles/{org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model => org.openhab.core.model.core/src/main/java/org/openhab/core/model/core}/yaml/YamlParseException.java (95%) delete mode 100644 bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlModelListener.java diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlModelRepository.java b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/internal/yaml/YamlModelRepository.java similarity index 94% rename from bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlModelRepository.java rename to bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/internal/yaml/YamlModelRepository.java index b9d0e6cee2e..3b3f702ab7d 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlModelRepository.java +++ b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/internal/yaml/YamlModelRepository.java @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package org.openhab.core.semantics.model.yaml; +package org.openhab.core.model.core.internal.yaml; import java.io.IOException; import java.nio.file.Files; @@ -24,6 +24,10 @@ import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.core.model.core.yaml.YamlElement; +import org.openhab.core.model.core.yaml.YamlFile; +import org.openhab.core.model.core.yaml.YamlModelListener; +import org.openhab.core.model.core.yaml.YamlParseException; import org.openhab.core.service.WatchService; import org.openhab.core.service.WatchService.Kind; import org.osgi.service.component.annotations.Activate; @@ -73,10 +77,12 @@ public void deactivate() { watchService.unregisterListener(this); } + // The method is "synchronized" to avoid concurrent files processing @Override public synchronized void processWatchEvent(Kind kind, Path path) { Path fullPath = watchPath.resolve(path); String dirName = path.subpath(0, 1).toString(); + logger.info("dirName={}", dirName); if (Files.isDirectory(fullPath) || fullPath.toFile().isHidden() || !fullPath.toString().endsWith(".yaml")) { logger.debug("Ignored {}", fullPath); @@ -87,7 +93,7 @@ public synchronized void processWatchEvent(Kind kind, Path path) { } private void processWatchEvent(String dirName, Kind kind, Path fullPath, YamlModelListener listener) { - logger.debug("processWatchEvent dirName={} kind={} fullPath={} listener={}", dirName, kind, fullPath, + logger.info("processWatchEvent dirName={} kind={} fullPath={} listener={}", dirName, kind, fullPath, listener.getClass().getSimpleName()); Map oldObjects; Map newObjects; diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlElement.java b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlElement.java similarity index 56% rename from bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlElement.java rename to bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlElement.java index e994732d3cd..209e8605ce1 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlElement.java +++ b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlElement.java @@ -10,16 +10,27 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package org.openhab.core.semantics.model.yaml; +package org.openhab.core.model.core.yaml; /** - * The {@link YamlElement} interface offers an identifier to any element defined in a YAML configuration file. + * The {@link YamlElement} interface offers an identifier and a check validity method + * to any element defined in a YAML configuration file. * * @author Laurent Garnier - Initial contribution */ public interface YamlElement { + /** + * Get the identifier of the YAML element + * + * @return the identifier as a string + */ String getId(); + /** + * Check that the YAML element is valid + * + * @throws YamlParseException if something is wrong + */ void checkValidity() throws YamlParseException; } diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlFile.java b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlFile.java similarity index 63% rename from bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlFile.java rename to bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlFile.java index 889feeb20a9..29ee7720867 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlFile.java +++ b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlFile.java @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package org.openhab.core.semantics.model.yaml; +package org.openhab.core.model.core.yaml; import java.util.List; @@ -26,19 +26,44 @@ @NonNullByDefault public abstract class YamlFile { + /** + * YAML file version + */ public int version; + /** + * Get the list of elements present in the YAML file. + * + * @return the list of elements + */ public abstract List getElements(); + /** + * Check that the version in the YAML file in the expected one. + * + * @throws YamlParseException if the version in the file is not the expected one + */ protected abstract void checkVersion() throws YamlParseException; + /** + * Check that the file content is valid. + * It includes the check of the version, the check of duplicated elements (same identifier) + * and the check of each element. + * + * @throws YamlParseException if something is invalid + */ public void checkValidity() throws YamlParseException { + // Checking version checkVersion(); + + // Checking duplicated elements List elts = getElements(); long nbDistinctIds = elts.stream().map(YamlElement::getId).distinct().count(); if (nbDistinctIds < elts.size()) { - throw new YamlParseException((elts.size() - nbDistinctIds + 1) + " elements with same ids"); + throw new YamlParseException("Elements with same ids detected in the file"); } + + // Checking each element for (int i = 0; i < elts.size(); i++) { try { elts.get(i).checkValidity(); diff --git a/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlModelListener.java b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlModelListener.java new file mode 100644 index 00000000000..148dac434a4 --- /dev/null +++ b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlModelListener.java @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2010-2023 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.model.core.yaml; + +import java.util.List; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * The {@link YamlModelListener} interface is responsible for managing a particular model type + * with data processed from YAML configuration files. + * + * @author Laurent Garnier - Initial contribution + */ +@NonNullByDefault +public interface YamlModelListener { + + /** + * Method called by the model repository when elements from a model are added. + * + * @param modelName the name of the model + * @param elements the list of added elements + */ + void addedModel(String modelName, List elements); + + /** + * Method called by the model repository when elements from a model are updated. + * + * @param modelName the name of the model + * @param elements the list of updated elements + */ + void updatedModel(String modelName, List elements); + + /** + * Method called by the model repository when elements from a model are removed. + * + * @param modelName the name of the model + * @param elements the list of removed elements + */ + void removedModel(String modelName, List elements); + + /** + * Get the root name of this model type which is also the name of the root folder + * containing the user files for this model type. + * + * A path is unexpected. What is expected is for example "items" or "things". + * + * @return the model root name + */ + String getRootName(); + + /** + * Get the DTO class to be used for a file providing objects for this model type. + * + * @return the DTO file class + */ + Class getFileClass(); + + /** + * Get the DTO class to be used for each object of this model type. + * + * @return the DTO element class + */ + Class getElementClass(); +} diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlParseException.java b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlParseException.java similarity index 95% rename from bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlParseException.java rename to bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlParseException.java index 95894fdcc2f..2adf8f9fe22 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlParseException.java +++ b/bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/yaml/YamlParseException.java @@ -10,7 +10,7 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package org.openhab.core.semantics.model.yaml; +package org.openhab.core.model.core.yaml; import org.eclipse.jdt.annotation.NonNullByDefault; diff --git a/bundles/org.openhab.core.semantics/pom.xml b/bundles/org.openhab.core.semantics/pom.xml index 1ebf3cec206..809e0a3afe4 100644 --- a/bundles/org.openhab.core.semantics/pom.xml +++ b/bundles/org.openhab.core.semantics/pom.xml @@ -20,6 +20,11 @@ org.openhab.core ${project.version} + + org.openhab.core.bundles + org.openhab.core.model.core + ${project.version} + org.ow2.asm asm diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTag.java b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTag.java index ff3c11dcd1c..e4da6e02494 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTag.java +++ b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTag.java @@ -16,8 +16,8 @@ import java.util.Objects; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.semantics.model.yaml.YamlElement; -import org.openhab.core.semantics.model.yaml.YamlParseException; +import org.openhab.core.model.core.yaml.YamlElement; +import org.openhab.core.model.core.yaml.YamlParseException; /** * The {@link YamlSemanticTag} is a data transfer object used to serialize a semantic tag diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTagProvider.java b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTagProvider.java index 5f79737ee11..6af680474a6 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTagProvider.java +++ b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTagProvider.java @@ -19,13 +19,13 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.common.registry.AbstractProvider; +import org.openhab.core.model.core.yaml.YamlElement; +import org.openhab.core.model.core.yaml.YamlFile; +import org.openhab.core.model.core.yaml.YamlModelListener; import org.openhab.core.semantics.SemanticTag; import org.openhab.core.semantics.SemanticTagImpl; import org.openhab.core.semantics.SemanticTagProvider; import org.openhab.core.semantics.SemanticTagRegistry; -import org.openhab.core.semantics.model.yaml.YamlElement; -import org.openhab.core.semantics.model.yaml.YamlFile; -import org.openhab.core.semantics.model.yaml.YamlModelListener; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTags.java b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTags.java index 3f44af0c5ca..e5af9ad6905 100644 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTags.java +++ b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/internal/config/yaml/YamlSemanticTags.java @@ -15,9 +15,9 @@ import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.core.semantics.model.yaml.YamlElement; -import org.openhab.core.semantics.model.yaml.YamlFile; -import org.openhab.core.semantics.model.yaml.YamlParseException; +import org.openhab.core.model.core.yaml.YamlElement; +import org.openhab.core.model.core.yaml.YamlFile; +import org.openhab.core.model.core.yaml.YamlParseException; /** * The {@link YamlSemanticTags} is a data transfer object used to serialize a list of semantic tags diff --git a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlModelListener.java b/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlModelListener.java deleted file mode 100644 index 6a49659b659..00000000000 --- a/bundles/org.openhab.core.semantics/src/main/java/org/openhab/core/semantics/model/yaml/YamlModelListener.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) 2010-2023 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.semantics.model.yaml; - -import java.util.List; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * The {@link YamlModelListener} interface is responsible for managing a particular model - * with data processed from YAML configuration files. - * - * @author Laurent Garnier - Initial contribution - */ -@NonNullByDefault -public interface YamlModelListener { - - void addedModel(String modelName, List elements); - - void updatedModel(String modelName, List elements); - - void removedModel(String modelName, List elements); - - String getRootName(); - - Class getFileClass(); - - Class getElementClass(); -}