1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.enforcer.rules;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23
24 import java.util.Objects;
25
26 import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
27 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
28 import org.apache.maven.enforcer.rules.utils.OSUtil;
29 import org.apache.maven.model.Activation;
30 import org.apache.maven.model.ActivationOS;
31 import org.apache.maven.model.Profile;
32 import org.apache.maven.model.profile.activation.ProfileActivator;
33 import org.codehaus.plexus.util.Os;
34 import org.codehaus.plexus.util.StringUtils;
35
36
37
38
39
40
41
42 @Named("requireOS")
43 public final class RequireOS extends AbstractStandardEnforcerRule {
44 private final ProfileActivator activator;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 private String family = null;
63
64
65
66
67 private String name = null;
68
69
70
71
72 private String version = null;
73
74
75
76
77 private String arch = null;
78
79
80
81
82 private boolean display = false;
83
84
85
86
87 @Inject
88 RequireOS(@Named("os") ProfileActivator activator) {
89 this.activator = Objects.requireNonNull(activator);
90 }
91
92 @Override
93 public void execute() throws EnforcerRuleException {
94
95 displayOSInfo();
96
97 if (allParamsEmpty()) {
98 throw new EnforcerRuleError("All parameters can not be empty. "
99 + "You must pick at least one of (family, name, version, arch), "
100 + "you can use mvn --version to see the current OS information.");
101 }
102
103 if (isValidFamily(this.family)) {
104 if (!isAllowed()) {
105 String message = getMessage();
106 if (message == null || message.isEmpty()) {
107
108 message = "OS Arch: "
109 + Os.OS_ARCH + " Family: "
110 + Os.OS_FAMILY + " Name: "
111 + Os.OS_NAME + " Version: "
112 + Os.OS_VERSION + " is not allowed by" + (arch != null ? " Arch=" + arch : "")
113 + (family != null ? " Family=" + family : "")
114 + (name != null ? " Name=" + name : "")
115 + (version != null ? " Version=" + version : "");
116
117 }
118 throw new EnforcerRuleException(message);
119 }
120 } else {
121 String validFamilies = String.join(",", Os.getValidFamilies());
122 throw new EnforcerRuleError("Invalid Family type used. Valid family types are: " + validFamilies);
123 }
124 }
125
126
127
128
129 private void displayOSInfo() {
130 String string = OSUtil.getOSInfo();
131
132 if (!display) {
133 getLog().debug(string);
134 } else {
135 getLog().info(string);
136 }
137 }
138
139
140
141
142
143
144
145 public boolean isAllowed() {
146 return activator.isActive(createProfile(), null, null);
147 }
148
149
150
151
152
153
154 public boolean allParamsEmpty() {
155 return (family == null || family.isEmpty())
156 && (arch == null || arch.isEmpty())
157 && (name == null || name.isEmpty())
158 && (version == null || version.isEmpty());
159 }
160
161
162
163
164
165
166 private Profile createProfile() {
167 Profile profile = new Profile();
168 profile.setActivation(createActivation());
169 return profile;
170 }
171
172
173
174
175
176
177 private Activation createActivation() {
178 Activation activation = new Activation();
179 activation.setActiveByDefault(false);
180 activation.setOs(createOsBean());
181 return activation;
182 }
183
184
185
186
187
188
189 private ActivationOS createOsBean() {
190 ActivationOS os = new ActivationOS();
191
192 os.setArch(arch);
193 os.setFamily(family);
194 os.setName(name);
195 os.setVersion(version);
196
197 return os;
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 public boolean isValidFamily(String theFamily) {
220
221
222 theFamily = StringUtils.stripStart(theFamily, "!");
223
224 return (theFamily == null || theFamily.isEmpty())
225 || Os.getValidFamilies().contains(theFamily);
226 }
227
228
229
230
231
232
233 public void setArch(String theArch) {
234 this.arch = theArch;
235 }
236
237
238
239
240
241
242 public void setFamily(String theFamily) {
243 this.family = theFamily;
244 }
245
246
247
248
249
250
251 public void setName(String theName) {
252 this.name = theName;
253 }
254
255
256
257
258
259
260 public void setVersion(String theVersion) {
261 this.version = theVersion;
262 }
263
264
265
266
267 public void setDisplay(boolean display) {
268 this.display = display;
269 }
270
271 @Override
272 public String getCacheId() {
273
274 StringBuilder b = new StringBuilder();
275 if (version != null && !version.isEmpty()) {
276 b.append(version.hashCode());
277 }
278 if (name != null && !name.isEmpty()) {
279 b.append(name.hashCode());
280 }
281 if (arch != null && !arch.isEmpty()) {
282 b.append(arch.hashCode());
283 }
284 if (family != null && !family.isEmpty()) {
285 b.append(family.hashCode());
286 }
287 return b.toString();
288 }
289
290 @Override
291 public String toString() {
292 return String.format(
293 "RequireOS[message=%s, arch=%s, family=%s, name=%s, version=%s, display=%b]",
294 getMessage(), arch, family, name, version, display);
295 }
296 }