Rename readCommitHash to findCommitHash, make it static and return optional.

Signed-off-by: HenrikJannsen <boilingfrog@gmx.com>
This commit is contained in:
HenrikJannsen 2022-11-29 19:56:04 -05:00
parent 39db3cc98b
commit a925017e4b
No known key found for this signature in database
GPG Key ID: 02AA2BAE387C8307
2 changed files with 9 additions and 6 deletions

View File

@ -21,6 +21,8 @@ import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
@ -140,14 +142,15 @@ public class Version {
'}');
}
public String readCommitHash() {
public static Optional<String> findCommitHash() {
try {
String pth = getClass().getResource(getClass().getSimpleName() + ".class").toString();
String pth = Objects.requireNonNull(Version.class.getResource(Version.class.getSimpleName() + ".class")).toString();
String mnf = pth.substring(0, pth.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
Attributes attr = new Manifest(new URL(mnf).openStream()).getMainAttributes();
return attr.getValue("Implementation-Version");
} catch (Exception ignored) { }
return "unknown";
return Optional.of(attr.getValue("Implementation-Version"));
} catch (Exception ignored) {
return Optional.empty();
}
}
public static final byte COMPENSATION_REQUEST = (byte) 0x01;

View File

@ -150,7 +150,7 @@ public class GetInventoryRequestHandler implements MessageListener {
// node
inventory.put(InventoryItem.version, Version.VERSION);
inventory.put(InventoryItem.commitHash, new Version().readCommitHash());
Version.findCommitHash().ifPresent(commitHash -> inventory.put(InventoryItem.commitHash, commitHash));
inventory.put(InventoryItem.usedMemory, String.valueOf(Profiler.getUsedMemoryInBytes()));
inventory.put(InventoryItem.jvmStartTime, String.valueOf(ManagementFactory.getRuntimeMXBean().getStartTime()));