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 @Override
108 public void execute( EnforcerRuleHelper helper )
109 throws EnforcerRuleException
110 {
111
112 displayOSInfo( helper.getLog(), display );
113
114 if ( allParamsEmpty() )
115 {
116 throw new EnforcerRuleException( "All parameters can not be empty. "
117 + "You must pick at least one of (family, name, version, arch) "
118 + "or use -Denforcer.os.display=true to see the current OS information." );
119 }
120
121 if ( isValidFamily( this.family ) )
122 {
123 if ( !isAllowed() )
124 {
125 String message = getMessage();
126 if ( StringUtils.isEmpty( message ) )
127 {
128
129 message =
130 ( "OS Arch: "
131 + Os.OS_ARCH + " Family: "
132 + Os.OS_FAMILY + " Name: "
133 + Os.OS_NAME + " Version: "
134 + Os.OS_VERSION + " is not allowed by" + ( arch != null ? " Arch=" + arch : "" )
135 + ( family != null ? " Family=" + family : "" )
136 + ( name != null ? " Name=" + name : "" )
137 + ( version != null ? " Version=" + version : "" ) );
138
139 }
140 throw new EnforcerRuleException( message );
141 }
142 }
143 else
144 {
145 final int minimumBufferSize = 50;
146 StringBuilder buffer = new StringBuilder( minimumBufferSize );
147 Iterator<?> iter = Os.getValidFamilies().iterator();
148 while ( iter.hasNext() )
149 {
150 buffer.append( iter.next() );
151 buffer.append( ", " );
152 }
153 String help = StringUtils.stripEnd( buffer.toString().trim(), "." );
154 throw new EnforcerRuleException( "Invalid Family type used. Valid family types are: " + help );
155 }
156 }
157
158
159
160
161
162
163
164 public void displayOSInfo( Log log, boolean info )
165 {
166 String string =
167 "OS Info: Arch: " + Os.OS_ARCH + " Family: " + Os.OS_FAMILY + " Name: " + Os.OS_NAME + " Version: "
168 + Os.OS_VERSION;
169
170 if ( !info )
171 {
172 log.debug( string );
173 }
174 else
175 {
176 log.info( string );
177 }
178 }
179
180
181
182
183
184
185
186 public boolean isAllowed()
187 {
188 OperatingSystemProfileActivator activator = new OperatingSystemProfileActivator();
189
190 return activator.isActive( createProfile() );
191 }
192
193
194
195
196
197
198 public boolean allParamsEmpty()
199 {
200
201 return ( StringUtils.isEmpty( family ) && StringUtils.isEmpty( arch ) && StringUtils.isEmpty( name ) && StringUtils.isEmpty( version ) );
202
203 }
204
205
206
207
208
209
210 private Profile createProfile()
211 {
212 Profile profile = new Profile();
213 profile.setActivation( createActivation() );
214 return profile;
215 }
216
217
218
219
220
221
222 private Activation createActivation()
223 {
224 Activation activation = new Activation();
225 activation.setActiveByDefault( false );
226 activation.setOs( createOsBean() );
227 return activation;
228 }
229
230
231
232
233
234
235 private ActivationOS createOsBean()
236 {
237 ActivationOS os = new ActivationOS();
238
239 os.setArch( arch );
240 os.setFamily( family );
241 os.setName( name );
242 os.setVersion( version );
243
244 return os;
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 public boolean isValidFamily( String theFamily )
267 {
268
269
270 theFamily = StringUtils.stripStart( theFamily, "!" );
271
272 return ( StringUtils.isEmpty( theFamily ) || Os.getValidFamilies().contains( theFamily ) );
273 }
274
275
276
277
278
279
280 public String getArch()
281 {
282 return this.arch;
283 }
284
285
286
287
288
289
290 public void setArch( String theArch )
291 {
292 this.arch = theArch;
293 }
294
295
296
297
298
299
300 public String getFamily()
301 {
302 return this.family;
303 }
304
305
306
307
308
309
310 public void setFamily( String theFamily )
311 {
312 this.family = theFamily;
313 }
314
315
316
317
318
319
320 public String getName()
321 {
322 return this.name;
323 }
324
325
326
327
328
329
330 public void setName( String theName )
331 {
332 this.name = theName;
333 }
334
335
336
337
338
339
340 public String getVersion()
341 {
342 return this.version;
343 }
344
345
346
347
348
349
350 public void setVersion( String theVersion )
351 {
352 this.version = theVersion;
353 }
354
355
356
357
358 public final void setDisplay( boolean display )
359 {
360 this.display = display;
361 }
362
363 public final boolean isDisplay()
364 {
365 return display;
366 }
367
368 @Override
369 public String getCacheId()
370 {
371
372 StringBuffer b = new StringBuffer();
373 if ( StringUtils.isNotEmpty( version ) )
374 {
375 b.append( version.hashCode() );
376 }
377 if ( StringUtils.isNotEmpty( name ) )
378 {
379 b.append( name.hashCode() );
380 }
381 if ( StringUtils.isNotEmpty( arch ) )
382 {
383 b.append( arch.hashCode() );
384 }
385 if ( StringUtils.isNotEmpty( family ) )
386 {
387 b.append( family.hashCode() );
388 }
389 return b.toString();
390 }
391
392 @Override
393 public boolean isCacheable()
394 {
395
396 return true;
397 }
398
399 @Override
400 public boolean isResultValid( EnforcerRule theCachedRule )
401 {
402
403
404 return true;
405 }
406 }