|
83 | 83 | */ |
84 | 84 | public final class InstantiatingGrpcChannelProvider implements TransportChannelProvider { |
85 | 85 |
|
86 | | - static String systemProductName; |
87 | | - |
88 | | - static { |
89 | | - try { |
90 | | - systemProductName = |
91 | | - Files.asCharSource(new File("/sys/class/dmi/id/product_name"), StandardCharsets.UTF_8) |
92 | | - .readFirstLine(); |
93 | | - } catch (IOException e) { |
94 | | - // If not on Compute Engine, FileNotFoundException will be thrown. Use empty string |
95 | | - // as it won't match with the GCE_PRODUCTION_NAME constants |
96 | | - systemProductName = ""; |
97 | | - } |
98 | | - } |
| 86 | + private static String systemProductName; |
99 | 87 |
|
100 | 88 | @VisibleForTesting |
101 | 89 | static final Logger LOG = Logger.getLogger(InstantiatingGrpcChannelProvider.class.getName()); |
@@ -345,13 +333,29 @@ boolean isCredentialDirectPathCompatible() { |
345 | 333 | static boolean isOnComputeEngine() { |
346 | 334 | String osName = System.getProperty("os.name"); |
347 | 335 | if ("Linux".equals(osName)) { |
| 336 | + String systemProductName = getSystemProductName(); |
348 | 337 | // systemProductName will be empty string if not on Compute Engine |
349 | 338 | return systemProductName.contains(GCE_PRODUCTION_NAME_PRIOR_2016) |
350 | 339 | || systemProductName.contains(GCE_PRODUCTION_NAME_AFTER_2016); |
351 | 340 | } |
352 | 341 | return false; |
353 | 342 | } |
354 | 343 |
|
| 344 | + private static String getSystemProductName() { |
| 345 | + // The static field systemProductName should only be set in tests |
| 346 | + if (systemProductName != null) { |
| 347 | + return systemProductName; |
| 348 | + } |
| 349 | + try { |
| 350 | + return Files.asCharSource(new File("/sys/class/dmi/id/product_name"), StandardCharsets.UTF_8) |
| 351 | + .readFirstLine(); |
| 352 | + } catch (IOException e) { |
| 353 | + // If not on Compute Engine, FileNotFoundException will be thrown. Use empty string |
| 354 | + // as it won't match with the GCE_PRODUCTION_NAME constants |
| 355 | + return ""; |
| 356 | + } |
| 357 | + } |
| 358 | + |
355 | 359 | // Universe Domain configuration is currently only supported in the GDU |
356 | 360 | @VisibleForTesting |
357 | 361 | boolean canUseDirectPathWithUniverseDomain() { |
|
0 commit comments