|
44 | 44 | import java.util.Collections; |
45 | 45 | import java.util.Comparator; |
46 | 46 | import java.util.HashMap; |
| 47 | +import java.util.HashSet; |
47 | 48 | import java.util.Iterator; |
48 | 49 | import java.util.LinkedHashSet; |
49 | 50 | import java.util.List; |
50 | 51 | import java.util.ListIterator; |
51 | 52 | import java.util.Map; |
52 | 53 | import java.util.Optional; |
53 | 54 | import java.util.Properties; |
| 55 | +import java.util.Set; |
54 | 56 | import java.util.StringJoiner; |
55 | 57 | import java.util.function.BiConsumer; |
56 | 58 | import java.util.function.BiFunction; |
@@ -1470,17 +1472,17 @@ protected int buildImage(List<String> javaArgs, LinkedHashSet<Path> cp, LinkedHa |
1470 | 1472 | ProcessBuilder pb = new ProcessBuilder(); |
1471 | 1473 | pb.command(command); |
1472 | 1474 | Map<String, String> environment = pb.environment(); |
1473 | | - String sloppySanitationKey = "NATIVE_IMAGE_SLOPPY_BUILDER_SANITATION"; |
1474 | | - String sloppySanitationValue = System.getenv().getOrDefault(sloppySanitationKey, "false"); |
1475 | | - if (Boolean.parseBoolean(sloppySanitationValue)) { |
| 1475 | + String deprecatedSanitationKey = "NATIVE_IMAGE_DEPRECATED_BUILDER_SANITATION"; |
| 1476 | + String deprecatedSanitationValue = System.getenv().getOrDefault(deprecatedSanitationKey, "false"); |
| 1477 | + if (Boolean.parseBoolean(deprecatedSanitationValue)) { |
1476 | 1478 | if (useBundle()) { |
1477 | 1479 | bundleSupport = null; |
1478 | | - throw showError("Bundle support is not compatible with environment variable %s=%s.".formatted(sloppySanitationKey, sloppySanitationValue)); |
| 1480 | + throw showError("Bundle support is not compatible with environment variable %s=%s.".formatted(deprecatedSanitationKey, deprecatedSanitationValue)); |
1479 | 1481 | } |
1480 | 1482 | if (!imageBuilderEnvironment.isEmpty()) { |
1481 | | - throw showError("Option -E<env-var-key>[=<env-var-value>] is not compatible with environment variable %s=%s.".formatted(sloppySanitationKey, sloppySanitationValue)); |
| 1483 | + throw showError("Option -E<env-var-key>[=<env-var-value>] is not compatible with environment variable %s=%s.".formatted(deprecatedSanitationKey, deprecatedSanitationValue)); |
1482 | 1484 | } |
1483 | | - sloppySanitizeJVMEnvironment(environment); |
| 1485 | + deprecatedSanitizeJVMEnvironment(environment); |
1484 | 1486 | } else { |
1485 | 1487 | sanitizeJVMEnvironment(environment, imageBuilderEnvironment); |
1486 | 1488 | } |
@@ -1528,40 +1530,43 @@ boolean useBundle() { |
1528 | 1530 | } |
1529 | 1531 |
|
1530 | 1532 | @Deprecated |
1531 | | - private static void sloppySanitizeJVMEnvironment(Map<String, String> environment) { |
| 1533 | + private static void deprecatedSanitizeJVMEnvironment(Map<String, String> environment) { |
1532 | 1534 | String[] jvmAffectingEnvironmentVariables = {"JAVA_COMPILER", "_JAVA_OPTIONS", "JAVA_TOOL_OPTIONS", "JDK_JAVA_OPTIONS", "CLASSPATH"}; |
1533 | 1535 | for (String affectingEnvironmentVariable : jvmAffectingEnvironmentVariables) { |
1534 | 1536 | environment.remove(affectingEnvironmentVariable); |
1535 | 1537 | } |
1536 | 1538 | } |
1537 | 1539 |
|
1538 | 1540 | private static void sanitizeJVMEnvironment(Map<String, String> environment, Map<String, String> imageBuilderEnvironment) { |
1539 | | - Map<String, String> restrictedEnvironment = new HashMap<>(); |
1540 | | - List<String> jvmRequiredEnvironmentVariables = new ArrayList<>(List.of("PATH", "PWD", "HOME", "LANG", "LC_ALL")); |
1541 | | - jvmRequiredEnvironmentVariables.add("SRCHOME"); // FIXME |
| 1541 | + Set<String> requiredKeys = new HashSet<>(List.of("PATH", "PWD", "HOME", "LANG", "LC_ALL")); |
| 1542 | + requiredKeys.add("SRCHOME"); /* Remove once GR-44676 is fixed */ |
| 1543 | + Function<String, String> keyMapper; |
1542 | 1544 | if (OS.WINDOWS.isCurrent()) { |
1543 | | - jvmRequiredEnvironmentVariables.addAll(List.of("TEMP", "INCLUDE", "LIB")); |
| 1545 | + requiredKeys.addAll(List.of("TEMP", "INCLUDE", "LIB")); |
| 1546 | + keyMapper = String::toUpperCase; |
| 1547 | + } else { |
| 1548 | + keyMapper = Function.identity(); |
1544 | 1549 | } |
1545 | | - for (String requiredEnvironmentVariable : jvmRequiredEnvironmentVariables) { |
1546 | | - String val = environment.get(requiredEnvironmentVariable); |
1547 | | - if (val != null) { |
1548 | | - restrictedEnvironment.put(requiredEnvironmentVariable, val); |
| 1550 | + Map<String, String> restrictedEnvironment = new HashMap<>(); |
| 1551 | + environment.forEach((key, val) -> { |
| 1552 | + if (requiredKeys.contains(keyMapper.apply(key))) { |
| 1553 | + restrictedEnvironment.put(key, val); |
1549 | 1554 | } |
1550 | | - } |
| 1555 | + }); |
1551 | 1556 | for (Iterator<Map.Entry<String, String>> iterator = imageBuilderEnvironment.entrySet().iterator(); iterator.hasNext();) { |
1552 | 1557 | Map.Entry<String, String> entry = iterator.next(); |
1553 | | - String requiredKey = entry.getKey(); |
1554 | | - String requiredValue = entry.getValue(); |
1555 | | - if (requiredValue != null) { |
1556 | | - restrictedEnvironment.put(requiredKey, requiredValue); |
| 1558 | + if (entry.getValue() != null) { |
| 1559 | + restrictedEnvironment.put(entry.getKey(), entry.getValue()); |
1557 | 1560 | } else { |
1558 | | - String existingValue = environment.get(requiredKey); |
1559 | | - if (existingValue != null) { |
1560 | | - restrictedEnvironment.put(requiredKey, existingValue); |
1561 | | - /* Capture found existingValue for storing vars in bundle */ |
1562 | | - entry.setValue(existingValue); |
1563 | | - } else { |
1564 | | - NativeImage.showWarning("Environment variable '" + requiredKey + "' is undefined and therefore not available during image build-time."); |
| 1561 | + environment.forEach((key, val) -> { |
| 1562 | + if (keyMapper.apply(key).equals(keyMapper.apply(entry.getKey()))) { |
| 1563 | + restrictedEnvironment.put(entry.getKey(), val); |
| 1564 | + /* Capture found value for storing vars in bundle */ |
| 1565 | + entry.setValue(val); |
| 1566 | + } |
| 1567 | + }); |
| 1568 | + if (entry.getValue() == null) { |
| 1569 | + NativeImage.showWarning("Environment variable '" + entry.getKey() + "' is undefined and therefore not available during image build-time."); |
1565 | 1570 | /* Remove undefined environment for storing vars in bundle */ |
1566 | 1571 | iterator.remove(); |
1567 | 1572 | } |
|
0 commit comments