1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.profile.activation;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.util.Locale;
25
26 import org.apache.maven.model.Activation;
27 import org.apache.maven.model.ActivationOS;
28 import org.apache.maven.model.Profile;
29 import org.apache.maven.model.building.ModelProblemCollector;
30 import org.apache.maven.model.profile.ProfileActivationContext;
31 import org.apache.maven.utils.Os;
32
33
34
35
36
37
38
39 @Named("os")
40 @Singleton
41 public class OperatingSystemProfileActivator implements ProfileActivator {
42
43 private static final String REGEX_PREFIX = "regex:";
44
45 @Override
46 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
47 Activation activation = profile.getActivation();
48
49 if (activation == null) {
50 return false;
51 }
52
53 ActivationOS os = activation.getOs();
54
55 if (os == null) {
56 return false;
57 }
58
59 boolean active = ensureAtLeastOneNonNull(os);
60
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);
64
65 if (active && os.getFamily() != null) {
66 active = determineFamilyMatch(os.getFamily(), actualOsName);
67 }
68 if (active && os.getName() != null) {
69 active = determineNameMatch(os.getName(), actualOsName);
70 }
71 if (active && os.getArch() != null) {
72 active = determineArchMatch(os.getArch(), actualOsArch);
73 }
74 if (active && os.getVersion() != null) {
75 active = determineVersionMatch(os.getVersion(), actualOsVersion);
76 }
77
78 return active;
79 }
80
81 @Override
82 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
83 Activation activation = profile.getActivation();
84
85 if (activation == null) {
86 return false;
87 }
88
89 ActivationOS os = activation.getOs();
90
91 if (os == null) {
92 return false;
93 }
94 return true;
95 }
96
97 private boolean ensureAtLeastOneNonNull(ActivationOS os) {
98 return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
99 }
100
101 private boolean determineVersionMatch(String expectedVersion, String actualVersion) {
102 String test = expectedVersion;
103 boolean reverse = false;
104 final boolean result;
105 if (test.startsWith(REGEX_PREFIX)) {
106 result = actualVersion.matches(test.substring(REGEX_PREFIX.length()));
107 } else {
108 if (test.startsWith("!")) {
109 reverse = true;
110 test = test.substring(1);
111 }
112 result = actualVersion.equalsIgnoreCase(test);
113 }
114
115 return reverse != result;
116 }
117
118 private boolean determineArchMatch(String expectedArch, String actualArch) {
119 String test = expectedArch.toLowerCase(Locale.ENGLISH);
120 boolean reverse = false;
121
122 if (test.startsWith("!")) {
123 reverse = true;
124 test = test.substring(1);
125 }
126
127 boolean result = actualArch.equals(test);
128
129 return reverse ? !result : result;
130 }
131
132 private boolean determineNameMatch(String expectedName, String actualName) {
133 String test = expectedName.toLowerCase(Locale.ENGLISH);
134 boolean reverse = false;
135
136 if (test.startsWith("!")) {
137 reverse = true;
138 test = test.substring(1);
139 }
140
141 boolean result = actualName.equals(test);
142
143 return reverse ? !result : result;
144 }
145
146 private boolean determineFamilyMatch(String family, String actualName) {
147 String test = family.toLowerCase(Locale.ENGLISH);
148 boolean reverse = false;
149
150 if (test.startsWith("!")) {
151 reverse = true;
152 test = test.substring(1);
153 }
154
155 boolean result = Os.isFamily(test, actualName);
156
157 return reverse ? !result : result;
158 }
159 }