Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear warnings in org.openhab.core.id #4498

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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