11package datadog .environment ;
22
3+ import static java .util .Locale .ROOT ;
4+
35import java .io .BufferedReader ;
46import java .io .FileReader ;
57import java .io .IOException ;
810import java .nio .file .Path ;
911import java .nio .file .Paths ;
1012import java .util .Arrays ;
11- import java .util .Locale ;
1213
1314/** Detects operating systems and libc library. */
1415public 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