Skip to content

Commit b3a0523

Browse files
committed
[MNG-8178] Fall back to System.getProperty for missing context props
A call to context.getSystemProperties() may yield empty an empty map, or one missing the desired key, which makes a subsequent call of toLowerCase fail with a NullPointerException. Fall-back to using System.getProperty (with an empty default, in case that one fails too).
1 parent 36645f6 commit b3a0523

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.inject.Singleton;
2323

2424
import java.util.Locale;
25+
import java.util.Map;
2526

2627
import org.apache.maven.model.Activation;
2728
import org.apache.maven.model.ActivationOS;
@@ -42,6 +43,14 @@ public class OperatingSystemProfileActivator implements ProfileActivator {
4243

4344
private static final String REGEX_PREFIX = "regex:";
4445

46+
private static String systemProperty(Map<String, String> map, String key) {
47+
String v = map.get(key);
48+
if (v == null) {
49+
v = System.getProperty(key, "");
50+
}
51+
return v;
52+
}
53+
4554
@Override
4655
public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
4756
Activation activation = profile.getActivation();
@@ -58,9 +67,12 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model
5867

5968
boolean active = ensureAtLeastOneNonNull(os);
6069

61-
String actualOsName = context.getSystemProperties().get("os.name").toLowerCase(Locale.ENGLISH);
62-
String actualOsArch = context.getSystemProperties().get("os.arch").toLowerCase(Locale.ENGLISH);
63-
String actualOsVersion = context.getSystemProperties().get("os.version").toLowerCase(Locale.ENGLISH);
70+
String actualOsName =
71+
systemProperty(context.getSystemProperties(), "os.name").toLowerCase(Locale.ENGLISH);
72+
String actualOsArch =
73+
systemProperty(context.getSystemProperties(), "os.arch").toLowerCase(Locale.ENGLISH);
74+
String actualOsVersion =
75+
systemProperty(context.getSystemProperties(), "os.version").toLowerCase(Locale.ENGLISH);
6476

6577
if (active && os.getFamily() != null) {
6678
active = determineFamilyMatch(os.getFamily(), actualOsName);

0 commit comments

Comments
 (0)