Skip to content

Commit

Permalink
MNG-8178: Fall back to System.getProperty for missing context props
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kohlschuetter committed Jul 9, 2024
1 parent 36645f6 commit 1029de6
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@
import javax.inject.Singleton;

import java.util.Locale;
import java.util.Map;

import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationOS;
Expand All @@ -42,6 43,14 @@ public class OperatingSystemProfileActivator implements ProfileActivator {

private static final String REGEX_PREFIX = "regex:";

private static String systemProperty(Map<String, String> map, String key) {
String v = map.get(key);
if (v == null) {
v = System.getProperty(key, "");
}
return v;
}

@Override
public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
Activation activation = profile.getActivation();
Expand All @@ -58,9 67,12 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model

boolean active = ensureAtLeastOneNonNull(os);

String actualOsName = context.getSystemProperties().get("os.name").toLowerCase(Locale.ENGLISH);
String actualOsArch = context.getSystemProperties().get("os.arch").toLowerCase(Locale.ENGLISH);
String actualOsVersion = context.getSystemProperties().get("os.version").toLowerCase(Locale.ENGLISH);
String actualOsName =
systemProperty(context.getSystemProperties(), "os.name").toLowerCase(Locale.ENGLISH);
String actualOsArch =
systemProperty(context.getSystemProperties(), "os.arch").toLowerCase(Locale.ENGLISH);
String actualOsVersion =
systemProperty(context.getSystemProperties(), "os.version").toLowerCase(Locale.ENGLISH);

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

0 comments on commit 1029de6

Please sign in to comment.