1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import java.util.HashSet;
58 import java.util.Locale;
59 import java.util.Set;
60
61
62
63
64
65
66
67
68
69
70 public class Os
71 {
72
73 public static final String FAMILY_DOS = "dos";
74
75 public static final String FAMILY_MAC = "mac";
76
77 public static final String FAMILY_NETWARE = "netware";
78
79 public static final String FAMILY_OS2 = "os/2";
80
81 public static final String FAMILY_TANDEM = "tandem";
82
83 public static final String FAMILY_UNIX = "unix";
84
85 public static final String FAMILY_WINDOWS = "windows";
86
87 public static final String FAMILY_WIN9X = "win9x";
88
89 public static final String FAMILY_ZOS = "z/os";
90
91 public static final String FAMILY_OS400 = "os/400";
92
93 public static final String FAMILY_OPENVMS = "openvms";
94
95
96 private static final Set<String> validFamilies = setValidFamilies();
97
98
99 private static final String PATH_SEP = System.getProperty( "path.separator" );
100
101 public static final String OS_NAME = System.getProperty( "os.name" ).toLowerCase( Locale.US );
102
103 public static final String OS_ARCH = System.getProperty( "os.arch" ).toLowerCase( Locale.US );
104
105 public static final String OS_VERSION = System.getProperty( "os.version" ).toLowerCase( Locale.US );
106
107
108 public static final String OS_FAMILY = getOsFamily();
109
110 private String family;
111
112 private String name;
113
114 private String version;
115
116 private String arch;
117
118
119
120
121 public Os()
122 {
123 }
124
125
126
127
128
129
130 public Os( String family )
131 {
132 setFamily( family );
133 }
134
135
136
137
138 private static Set<String> setValidFamilies()
139 {
140 Set<String> valid = new HashSet<String>();
141 valid.add( FAMILY_DOS );
142 valid.add( FAMILY_MAC );
143 valid.add( FAMILY_NETWARE );
144 valid.add( FAMILY_OS2 );
145 valid.add( FAMILY_TANDEM );
146 valid.add( FAMILY_UNIX );
147 valid.add( FAMILY_WINDOWS );
148 valid.add( FAMILY_WIN9X );
149 valid.add( FAMILY_ZOS );
150 valid.add( FAMILY_OS400 );
151 valid.add( FAMILY_OPENVMS );
152
153 return valid;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public void setFamily( String f )
176 {
177 family = f.toLowerCase( Locale.US );
178 }
179
180
181
182
183
184
185 public void setName( String name )
186 {
187 this.name = name.toLowerCase( Locale.US );
188 }
189
190
191
192
193
194
195 public void setArch( String arch )
196 {
197 this.arch = arch.toLowerCase( Locale.US );
198 }
199
200
201
202
203
204
205 public void setVersion( String version )
206 {
207 this.version = version.toLowerCase( Locale.US );
208 }
209
210
211
212
213
214
215
216 public boolean eval()
217 throws Exception
218 {
219 return isOs( family, name, arch, version );
220 }
221
222
223
224
225
226
227
228
229 public static boolean isFamily( String family )
230 {
231 return isOs( family, null, null, null );
232 }
233
234
235
236
237
238
239
240
241 public static boolean isName( String name )
242 {
243 return isOs( null, name, null, null );
244 }
245
246
247
248
249
250
251
252
253 public static boolean isArch( String arch )
254 {
255 return isOs( null, null, arch, null );
256 }
257
258
259
260
261
262
263
264
265 public static boolean isVersion( String version )
266 {
267 return isOs( null, null, null, version );
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281 public static boolean isOs( String family, String name, String arch, String version )
282 {
283 boolean retValue = false;
284
285 if ( family != null || name != null || arch != null || version != null )
286 {
287
288 boolean isFamily = true;
289 boolean isName = true;
290 boolean isArch = true;
291 boolean isVersion = true;
292
293 if ( family != null )
294 {
295 if ( family.equalsIgnoreCase( FAMILY_WINDOWS ) )
296 {
297 isFamily = OS_NAME.contains( FAMILY_WINDOWS );
298 }
299 else if ( family.equalsIgnoreCase( FAMILY_OS2 ) )
300 {
301 isFamily = OS_NAME.contains( FAMILY_OS2 );
302 }
303 else if ( family.equalsIgnoreCase( FAMILY_NETWARE ) )
304 {
305 isFamily = OS_NAME.contains( FAMILY_NETWARE );
306 }
307 else if ( family.equalsIgnoreCase( FAMILY_DOS ) )
308 {
309 isFamily = PATH_SEP.equals( ";" ) && !isFamily( FAMILY_NETWARE ) && !isFamily( FAMILY_WINDOWS )
310 && !isFamily( FAMILY_WIN9X );
311
312 }
313 else if ( family.equalsIgnoreCase( FAMILY_MAC ) )
314 {
315 isFamily = OS_NAME.contains( FAMILY_MAC );
316 }
317 else if ( family.equalsIgnoreCase( FAMILY_TANDEM ) )
318 {
319 isFamily = OS_NAME.contains( "nonstop_kernel" );
320 }
321 else if ( family.equalsIgnoreCase( FAMILY_UNIX ) )
322 {
323 isFamily = PATH_SEP.equals( ":" ) && !isFamily( FAMILY_OPENVMS )
324 && ( !isFamily( FAMILY_MAC ) || OS_NAME.endsWith( "x" ) );
325 }
326 else if ( family.equalsIgnoreCase( FAMILY_WIN9X ) )
327 {
328 isFamily = isFamily( FAMILY_WINDOWS ) && ( OS_NAME.contains( "95" ) || OS_NAME.contains( "98" )
329 || OS_NAME.contains( "me" ) || OS_NAME.contains( "ce" ) );
330 }
331 else if ( family.equalsIgnoreCase( FAMILY_ZOS ) )
332 {
333 isFamily = OS_NAME.contains( FAMILY_ZOS ) || OS_NAME.contains( "os/390" );
334 }
335 else if ( family.equalsIgnoreCase( FAMILY_OS400 ) )
336 {
337 isFamily = OS_NAME.contains( FAMILY_OS400 );
338 }
339 else if ( family.equalsIgnoreCase( FAMILY_OPENVMS ) )
340 {
341 isFamily = OS_NAME.contains( FAMILY_OPENVMS );
342 }
343 else
344 {
345 isFamily = OS_NAME.contains( family.toLowerCase( Locale.US ) );
346 }
347 }
348 if ( name != null )
349 {
350 isName = name.toLowerCase( Locale.US ).equals( OS_NAME );
351 }
352 if ( arch != null )
353 {
354 isArch = arch.toLowerCase( Locale.US ).equals( OS_ARCH );
355 }
356 if ( version != null )
357 {
358 isVersion = version.toLowerCase( Locale.US ).equals( OS_VERSION );
359 }
360 retValue = isFamily && isName && isArch && isVersion;
361 }
362 return retValue;
363 }
364
365
366
367
368
369
370
371 private static String getOsFamily()
372 {
373
374
375
376 Set<String> families = null;
377 if ( !validFamilies.isEmpty() )
378 {
379 families = validFamilies;
380 }
381 else
382 {
383 families = setValidFamilies();
384 }
385 for ( String fam : families )
386 {
387 if ( Os.isFamily( fam ) )
388 {
389 return fam;
390 }
391 }
392 return null;
393 }
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415 public static boolean isValidFamily( String theFamily )
416 {
417 return ( validFamilies.contains( theFamily ) );
418 }
419
420
421
422
423
424 public static Set<String> getValidFamilies()
425 {
426 return new HashSet<String>( validFamilies );
427 }
428 }