Skip to content

Commit

Permalink
Clear warnings in org.openhab.core.id (#4498)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaël L'hopital <gael@lhopital.org>
  • Loading branch information
clinique authored Jan 14, 2025
1 parent 1b203d1 commit 15eb5cc
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.OpenHAB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,35 +31,36 @@
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public class InstanceUUID {

private static final Logger LOGGER = LoggerFactory.getLogger(InstanceUUID.class);

static final String UUID_FILE_NAME = "uuid";

@Nullable
static String uuid;

/**
* Retrieves a unified unique id, based on {@link java.util.UUID#randomUUID()}
*
* @return a UUID which identifies the instance or null, if uuid cannot be persisted
*/
public static synchronized String get() {
public static synchronized @Nullable String get() {
if (uuid == null) {
try {
File file = new File(OpenHAB.getUserDataFolder() + File.separator + UUID_FILE_NAME);
if (!file.exists()) {
uuid = java.util.UUID.randomUUID().toString();
writeFile(file, uuid);
uuid = generateToFile(file);
} else {
uuid = readFirstLine(file);
if (uuid != null && !uuid.isEmpty()) {
String valueInFile = readFirstLine(file);
if (!valueInFile.isEmpty()) {
uuid = valueInFile;
LOGGER.debug("UUID '{}' has been restored from file '{}'", file.getAbsolutePath(), uuid);
} else {
uuid = java.util.UUID.randomUUID().toString();
uuid = generateToFile(file);
LOGGER.warn("UUID file '{}' has no content, rewriting it now with '{}'", file.getAbsolutePath(),
uuid);
writeFile(file, uuid);
}
}
} catch (IOException e) {
Expand All @@ -68,16 +71,19 @@ public static synchronized String get() {
return uuid;
}

private static void writeFile(File file, String content) throws IOException {
private static String generateToFile(File file) throws IOException {
// create intermediary directories
file.getParentFile().mkdirs();
Files.writeString(file.toPath(), content, StandardCharsets.UTF_8);
if (file.getParentFile() instanceof File parentFile) {
parentFile.mkdirs();
}
String newUuid = java.util.UUID.randomUUID().toString();
Files.writeString(file.toPath(), newUuid, StandardCharsets.UTF_8);
return newUuid;
}

private static String readFirstLine(File file) {
try (final BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
String line;
return (line = reader.readLine()) == null ? "" : line;
return reader.readLine() instanceof String line ? line : "";
} catch (IOException ioe) {
LOGGER.warn("Failed reading the UUID file '{}': {}", file.getAbsolutePath(), ioe.getMessage());
return "";
Expand Down

0 comments on commit 15eb5cc

Please sign in to comment.