Skip to content

Commit 1e39672

Browse files
committed
feat(environment): Use SystemProperties within component
1 parent 8fd6f88 commit 1e39672

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

components/environment/src/main/java/datadog/environment/JavaVirtualMachine.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static boolean isOracleJDK8() {
9494
}
9595

9696
public static boolean isJ9() {
97-
return System.getProperty("java.vm.name").contains("J9");
97+
return SystemProperties.getOrDefault("java.vm.name", "").contains("J9");
9898
}
9999

100100
public static boolean isIbm8() {
@@ -190,11 +190,11 @@ static final class Runtime {
190190

191191
public Runtime() {
192192
this(
193-
System.getProperty("java.version"),
194-
System.getProperty("java.runtime.version"),
195-
System.getProperty("java.runtime.name"),
196-
System.getProperty("java.vm.vendor"),
197-
System.getProperty("java.vendor.version"));
193+
SystemProperties.get("java.version"),
194+
SystemProperties.get("java.runtime.version"),
195+
SystemProperties.get("java.runtime.name"),
196+
SystemProperties.get("java.vm.vendor"),
197+
SystemProperties.get("java.vendor.version"));
198198
}
199199

200200
// Only visible for testing

components/environment/src/main/java/datadog/environment/OperatingSystem.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package datadog.environment;
22

3+
import static java.util.Locale.ROOT;
4+
35
import java.io.BufferedReader;
46
import java.io.FileReader;
57
import java.io.IOException;
@@ -8,10 +10,12 @@
810
import java.nio.file.Path;
911
import java.nio.file.Paths;
1012
import java.util.Arrays;
11-
import java.util.Locale;
1213

1314
/** Detects operating systems and libc library. */
1415
public final class OperatingSystem {
16+
private static final String OS_NAME_PROPERTY = "os.name";
17+
private static final String OS_ARCH_PROPERTY = "os.arch";
18+
1519
private OperatingSystem() {}
1620

1721
/**
@@ -20,7 +24,7 @@ private OperatingSystem() {}
2024
* @return @{@code true} if operating system is Linux based, {@code false} otherwise.
2125
*/
2226
public static boolean isLinux() {
23-
return System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux");
27+
return propertyContains(OS_NAME_PROPERTY, "linux");
2428
}
2529

2630
/**
@@ -30,8 +34,7 @@ public static boolean isLinux() {
3034
*/
3135
public static boolean isWindows() {
3236
// https://mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/
33-
final String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
34-
return os.contains("win");
37+
return propertyContains(OS_NAME_PROPERTY, "win");
3538
}
3639

3740
/**
@@ -40,8 +43,7 @@ public static boolean isWindows() {
4043
* @return @{@code true} if operating system is macOS, {@code false} otherwise.
4144
*/
4245
public static boolean isMacOs() {
43-
final String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
44-
return os.contains("mac");
46+
return propertyContains(OS_NAME_PROPERTY, "mac");
4547
}
4648

4749
/**
@@ -50,7 +52,11 @@ public static boolean isMacOs() {
5052
* @return {@code true} if the architecture is AArch64, {@code false} otherwise.
5153
*/
5254
public static boolean isAarch64() {
53-
return System.getProperty("os.arch").toLowerCase().contains("aarch64");
55+
return propertyContains(OS_ARCH_PROPERTY, "aarch64");
56+
}
57+
58+
private static boolean propertyContains(String property, String content) {
59+
return SystemProperties.getOrDefault(property, "").toLowerCase(ROOT).contains(content);
5460
}
5561

5662
/**
@@ -102,7 +108,7 @@ private static boolean isMuslJavaExecutable() throws IOException {
102108
byte[] prefix = new byte[] {(byte) '/', (byte) 'l', (byte) 'd', (byte) '-'}; // '/ld-*'
103109
byte[] musl = new byte[] {(byte) 'm', (byte) 'u', (byte) 's', (byte) 'l'}; // 'musl'
104110

105-
Path binary = Paths.get(System.getProperty("java.home"), "bin", "java");
111+
Path binary = Paths.get(SystemProperties.getOrDefault("java.home", ""), "bin", "java");
106112
byte[] buffer = new byte[4096];
107113

108114
try (InputStream is = Files.newInputStream(binary)) {

0 commit comments

Comments
 (0)