Clear warnings in org.openhab.core.id (#4498)
Signed-off-by: Gaël L'hopital <gael@lhopital.org>pull/4565/head
parent
1b203d1b79
commit
15eb5cccd3
|
@ -18,6 +18,8 @@ import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.core.OpenHAB;
|
import org.openhab.core.OpenHAB;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -29,12 +31,14 @@ import org.slf4j.LoggerFactory;
|
||||||
*
|
*
|
||||||
* @author Kai Kreuzer - Initial contribution
|
* @author Kai Kreuzer - Initial contribution
|
||||||
*/
|
*/
|
||||||
|
@NonNullByDefault
|
||||||
public class InstanceUUID {
|
public class InstanceUUID {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(InstanceUUID.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(InstanceUUID.class);
|
||||||
|
|
||||||
static final String UUID_FILE_NAME = "uuid";
|
static final String UUID_FILE_NAME = "uuid";
|
||||||
|
|
||||||
|
@Nullable
|
||||||
static String uuid;
|
static String uuid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,22 +46,21 @@ public class InstanceUUID {
|
||||||
*
|
*
|
||||||
* @return a UUID which identifies the instance or null, if uuid cannot be persisted
|
* @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) {
|
if (uuid == null) {
|
||||||
try {
|
try {
|
||||||
File file = new File(OpenHAB.getUserDataFolder() + File.separator + UUID_FILE_NAME);
|
File file = new File(OpenHAB.getUserDataFolder() + File.separator + UUID_FILE_NAME);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
uuid = java.util.UUID.randomUUID().toString();
|
uuid = generateToFile(file);
|
||||||
writeFile(file, uuid);
|
|
||||||
} else {
|
} else {
|
||||||
uuid = readFirstLine(file);
|
String valueInFile = readFirstLine(file);
|
||||||
if (uuid != null && !uuid.isEmpty()) {
|
if (!valueInFile.isEmpty()) {
|
||||||
|
uuid = valueInFile;
|
||||||
LOGGER.debug("UUID '{}' has been restored from file '{}'", file.getAbsolutePath(), uuid);
|
LOGGER.debug("UUID '{}' has been restored from file '{}'", file.getAbsolutePath(), uuid);
|
||||||
} else {
|
} else {
|
||||||
uuid = java.util.UUID.randomUUID().toString();
|
uuid = generateToFile(file);
|
||||||
LOGGER.warn("UUID file '{}' has no content, rewriting it now with '{}'", file.getAbsolutePath(),
|
LOGGER.warn("UUID file '{}' has no content, rewriting it now with '{}'", file.getAbsolutePath(),
|
||||||
uuid);
|
uuid);
|
||||||
writeFile(file, uuid);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -68,16 +71,19 @@ public class InstanceUUID {
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeFile(File file, String content) throws IOException {
|
private static String generateToFile(File file) throws IOException {
|
||||||
// create intermediary directories
|
// create intermediary directories
|
||||||
file.getParentFile().mkdirs();
|
if (file.getParentFile() instanceof File parentFile) {
|
||||||
Files.writeString(file.toPath(), content, StandardCharsets.UTF_8);
|
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) {
|
private static String readFirstLine(File file) {
|
||||||
try (final BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
try (final BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
||||||
String line;
|
return reader.readLine() instanceof String line ? line : "";
|
||||||
return (line = reader.readLine()) == null ? "" : line;
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
LOGGER.warn("Failed reading the UUID file '{}': {}", file.getAbsolutePath(), ioe.getMessage());
|
LOGGER.warn("Failed reading the UUID file '{}': {}", file.getAbsolutePath(), ioe.getMessage());
|
||||||
return "";
|
return "";
|
||||||
|
|
Loading…
Reference in New Issue