1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Iterator;
23
24 import org.apache.maven.enforcer.rule.api.EnforcerRule;
25 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
26 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
27 import org.apache.maven.model.Activation;
28 import org.apache.maven.model.ActivationOS;
29 import org.apache.maven.model.Profile;
30 import org.apache.maven.plugin.logging.Log;
31 import org.apache.maven.profiles.activation.OperatingSystemProfileActivator;
32 import org.codehaus.plexus.util.Os;
33 import org.codehaus.plexus.util.StringUtils;
34
35
36
37
38
39
40
41
42 public class RequireOS
43 extends AbstractStandardEnforcerRule
44 {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 private String family = null;
66
67
68
69
70
71
72
73 private String name = null;
74
75
76
77
78
79
80
81 private String version = null;
82
83
84
85
86
87
88
89 private String arch = null;
90
91
92
93
94
95
96
97 private boolean display = false;
98
99
100
101
102 public RequireOS()
103 {
104
105 }
106
107
108
109
110 public void execute( EnforcerRuleHelper helper )
111 throws EnforcerRuleException
112 {
113
114 displayOSInfo( helper.getLog(), display );
115
116 if ( allParamsEmpty() )
117 {
118 throw new EnforcerRuleException( "All parameters can not be empty. "
119 + "You must pick at least one of (family, name, version, arch) "
120 + "or use -Denforcer.os.display=true to see the current OS information." );
121 }
122
123 if ( isValidFamily( this.family ) )
124 {
125 if ( !isAllowed() )
126 {
127 String message = getMessage();
128 if ( StringUtils.isEmpty( message ) )
129 {
130
131 message =
132 ( "OS Arch: "
133 + Os.OS_ARCH + " Family: "
134 + Os.OS_FAMILY + " Name: "
135 + Os.OS_NAME + " Version: "
136 + Os.OS_VERSION + " is not allowed by" + ( arch != null ? " Arch=" + arch : "" )
137 + ( family != null ? " Family=" + family : "" )
138 + ( name != null ? " Name=" + name : "" )
139 + ( version != null ? " Version=" + version : "" ) );
140
141 }
142 throw new EnforcerRuleException( message );
143 }
144 }
145 else
146 {
147 final int minimumBufferSize = 50;
148 StringBuilder buffer = new StringBuilder( minimumBufferSize );
149 Iterator<?> iter = Os.getValidFamilies().iterator();
150 while ( iter.hasNext() )
151 {
152 buffer.append( iter.next() );
153 buffer.append( ", " );
154 }
155 String help = StringUtils.stripEnd( buffer.toString().trim(), "." );
156 throw new EnforcerRuleException( "Invalid Family type used. Valid family types are: " + help );
157 }
158 }
159
160
161
162
163
164
165
166 public void displayOSInfo( Log log, boolean info )
167 {
168 String string =
169 "OS Info: Arch: " + Os.OS_ARCH + " Family: " + Os.OS_FAMILY + " Name: " + Os.OS_NAME + " Version: "
170 + Os.OS_VERSION;
171
172 if ( !info )
173 {
174 log.debug( string );
175 }
176 else
177 {
178 log.info( string );
179 }
180 }
181
182
183
184
185
186
187
188 public boolean isAllowed()
189 {
190 OperatingSystemProfileActivator activator = new OperatingSystemProfileActivator();
191
192 return activator.isActive( createProfile() );
193 }
194
195
196
197
198
199
200 public boolean allParamsEmpty()
201 {
202
203 return ( StringUtils.isEmpty( family ) && StringUtils.isEmpty( arch ) && StringUtils.isEmpty( name ) && StringUtils.isEmpty( version ) );
204
205 }
206
207
208
209
210
211
212 private Profile createProfile()
213 {
214 Profile profile = new Profile();
215 profile.setActivation( createActivation() );
216 return profile;
217 }
218
219
220
221
222
223
224 private Activation createActivation()
225 {
226 Activation activation = new Activation();
227 activation.setActiveByDefault( false );
228 activation.setOs( createOsBean() );
229 return activation;
230 }
231
232
233
234
235
236
237 private ActivationOS createOsBean()
238 {
239 ActivationOS os = new ActivationOS();
240
241 os.setArch( arch );
242 os.setFamily( family );
243 os.setName( name );
244 os.setVersion( version );
245
246 return os;
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 public boolean isValidFamily( String theFamily )
269 {
270
271
272 theFamily = StringUtils.stripStart( theFamily, "!" );
273
274 return ( StringUtils.isEmpty( theFamily ) || Os.getValidFamilies().contains( theFamily ) );
275 }
276
277
278
279
280
281
282 public String getArch()
283 {
284 return this.arch;
285 }
286
287
288
289
290
291
292 public void setArch( String theArch )
293 {
294 this.arch = theArch;
295 }
296
297
298
299
300
301
302 public String getFamily()
303 {
304 return this.family;
305 }
306
307
308
309
310
311
312 public void setFamily( String theFamily )
313 {
314 this.family = theFamily;
315 }
316
317
318
319
320
321
322 public String getName()
323 {
324 return this.name;
325 }
326
327
328
329
330
331
332 public void setName( String theName )
333 {
334 this.name = theName;
335 }
336
337
338
339
340
341
342 public String getVersion()
343 {
344 return this.version;
345 }
346
347
348
349
350
351
352 public void setVersion( String theVersion )
353 {
354 this.version = theVersion;
355 }
356
357
358
359
360 public final void setDisplay( boolean display )
361 {
362 this.display = display;
363 }
364
365 public final boolean isDisplay()
366 {
367 return display;
368 }
369
370
371
372
373 public String getCacheId()
374 {
375
376 StringBuffer b = new StringBuffer();
377 if ( StringUtils.isNotEmpty( version ) )
378 {
379 b.append( version.hashCode() );
380 }
381 if ( StringUtils.isNotEmpty( name ) )
382 {
383 b.append( name.hashCode() );
384 }
385 if ( StringUtils.isNotEmpty( arch ) )
386 {
387 b.append( arch.hashCode() );
388 }
389 if ( StringUtils.isNotEmpty( family ) )
390 {
391 b.append( family.hashCode() );
392 }
393 return b.toString();
394 }
395
396
397
398
399 public boolean isCacheable()
400 {
401
402 return true;
403 }
404
405
406
407
408 public boolean isResultValid( EnforcerRule theCachedRule )
409 {
410
411
412 return true;
413 }
414 }