Skip to content

Commit

Permalink
don't throw exceptions in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Dec 4, 2023
1 parent 9fbac5d commit 7a57442
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/main/java/io/cryostat/core/JvmIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ public class JvmIdentifier {

private final String hash;

public JvmIdentifier() {
this(RuntimeMetrics.readLocalMetrics());
private JvmIdentifier(String hash) {
this.hash = hash;
}

public JvmIdentifier(RuntimeMetrics metrics) {
public static JvmIdentifier getLocal() throws IDException {
return from(RuntimeMetrics.readLocalMetrics());
}

public static JvmIdentifier from(RuntimeMetrics metrics) throws IDException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
DataOutputStream dos = new DataOutputStream(baos)) {
dos.writeUTF(metrics.getClassPath());
Expand All @@ -47,10 +51,10 @@ public JvmIdentifier(RuntimeMetrics metrics) {
dos.writeUTF(metrics.getVmVersion());
dos.writeLong(metrics.getStartTime());
byte[] hash = DigestUtils.sha256(baos.toByteArray());
this.hash =
new String(Base64.getUrlEncoder().encode(hash), StandardCharsets.UTF_8).trim();
return new JvmIdentifier(
new String(Base64.getUrlEncoder().encode(hash), StandardCharsets.UTF_8).trim());
} catch (IOException e) {
throw new RuntimeException(new IDException(e));
throw new IDException(e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/cryostat/core/net/JFRJMXConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public synchronized JvmIdentifier getJvmIdentifier() throws IDException, IOExcep
"VmVersion",
"StartTime"));
try {
return new JvmIdentifier(
return JvmIdentifier.from(
new RuntimeMetrics(
getAttributeMap(
ConnectionToolkit.RUNTIME_BEAN_NAME,
Expand Down Expand Up @@ -274,7 +274,7 @@ public synchronized MBeanMetrics getMBeanMetrics()
new MemoryMetrics(memoryMap),
new ThreadMetrics(threadMap),
new OperatingSystemMetrics(osMap),
new JvmIdentifier(runtimeMetrics).getHash());
JvmIdentifier.from(runtimeMetrics).getHash());
}

public synchronized boolean isV1() throws ConnectionException, IOException {
Expand Down

0 comments on commit 7a57442

Please sign in to comment.