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 @Deprecated(since = "4.0.0")
42 public class OperatingSystemProfileActivator implements ProfileActivator {
43
44 private static final String REGEX_PREFIX = "regex:";
45
46 @Override
47 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
48 Activation activation = profile.getActivation();
49
50 if (activation == null) {
51 return false;
52 }
53
54 ActivationOS os = activation.getOs();
55
56 if (os == null) {
57 return false;
58 }
59
60 boolean active = ensureAtLeastOneNonNull(os);
61
62 String actualOsName = context.getSystemProperties()
63 .getOrDefault("os.name", Os.OS_NAME)
64 .toLowerCase(Locale.ENGLISH);
65 String actualOsArch = context.getSystemProperties()
66 .getOrDefault("os.arch", Os.OS_ARCH)
67 .toLowerCase(Locale.ENGLISH);
68 String actualOsVersion = context.getSystemProperties()
69 .getOrDefault("os.version", Os.OS_VERSION)
70 .toLowerCase(Locale.ENGLISH);
71
72 if (active && os.getFamily() != null) {
73 active = determineFamilyMatch(os.getFamily(), actualOsName);
74 }
75 if (active && os.getName() != null) {
76 active = determineNameMatch(os.getName(), actualOsName);
77 }
78 if (active && os.getArch() != null) {
79 active = determineArchMatch(os.getArch(), actualOsArch);
80 }
81 if (active && os.getVersion() != null) {
82 active = determineVersionMatch(os.getVersion(), actualOsVersion);
83 }
84
85 return active;
86 }
87
88 @Override
89 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
90 Activation activation = profile.getActivation();
91
92 if (activation == null) {
93 return false;
94 }
95
96 ActivationOS os = activation.getOs();
97
98 if (os == null) {
99 return false;
100 }
101 return true;
102 }
103
104 private boolean ensureAtLeastOneNonNull(ActivationOS os) {
105 return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
106 }
107
108 private boolean determineVersionMatch(String expectedVersion, String actualVersion) {
109 String test = expectedVersion;
110 boolean reverse = false;
111 final boolean result;
112 if (test.startsWith(REGEX_PREFIX)) {
113 result = actualVersion.matches(test.substring(REGEX_PREFIX.length()));
114 } else {
115 if (test.startsWith("!")) {
116 reverse = true;
117 test = test.substring(1);
118 }
119 result = actualVersion.equalsIgnoreCase(test);
120 }
121
122 return reverse != result;
123 }
124
125 private boolean determineArchMatch(String expectedArch, String actualArch) {
126 String test = expectedArch.toLowerCase(Locale.ENGLISH);
127 boolean reverse = false;
128
129 if (test.startsWith("!")) {
130 reverse = true;
131 test = test.substring(1);
132 }
133
134 boolean result = actualArch.equals(test);
135
136 return reverse ? !result : result;
137 }
138
139 private boolean determineNameMatch(String expectedName, String actualName) {
140 String test = expectedName.toLowerCase(Locale.ENGLISH);
141 boolean reverse = false;
142
143 if (test.startsWith("!")) {
144 reverse = true;
145 test = test.substring(1);
146 }
147
148 boolean result = actualName.equals(test);
149
150 return reverse ? !result : result;
151 }
152
153 private boolean determineFamilyMatch(String family, String actualName) {
154 String test = family.toLowerCase(Locale.ENGLISH);
155 boolean reverse = false;
156
157 if (test.startsWith("!")) {
158 reverse = true;
159 test = test.substring(1);
160 }
161
162 boolean result = Os.isFamily(test, actualName);
163
164 return reverse ? !result : result;
165 }
166 }