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 theFamily) {
238
239
240 theFamily = StringUtils.stripStart(theFamily, "!");
241
242 return (theFamily == null || theFamily.isEmpty())
243 || Os.getValidFamilies().contains(theFamily);
244 }
245
246
247
248
249
250
251 public void setArch(String theArch) {
252 this.arch = theArch;
253 }
254
255
256
257
258
259
260 public void setFamily(String theFamily) {
261 this.family = theFamily;
262 }
263
264
265
266
267
268
269 public void setName(String theName) {
270 this.name = theName;
271 }
272
273
274
275
276
277
278 public void setVersion(String theVersion) {
279 this.version = theVersion;
280 }
281
282
283
284
285 public void setDisplay(boolean display) {
286 this.display = display;
287 }
288
289 @Override
290 public String getCacheId() {
291
292 StringBuilder b = new StringBuilder();
293 if (version != null && !version.isEmpty()) {
294 b.append(version.hashCode());
295 }
296 if (name != null && !name.isEmpty()) {
297 b.append(name.hashCode());
298 }
299 if (arch != null && !arch.isEmpty()) {
300 b.append(arch.hashCode());
301 }
302 if (family != null && !family.isEmpty()) {
303 b.append(family.hashCode());
304 }
305 return b.toString();
306 }
307
308 @Override
309 public String toString() {
310 return String.format(
311 "RequireOS[message=%s, arch=%s, family=%s, name=%s, version=%s, display=%b]",
312 getMessage(), arch, family, name, version, display);
313 }
314 }