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