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