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 public static final String FAMILY_DOS = "dos";
73
74 public static final String FAMILY_MAC = "mac";
75
76 public static final String FAMILY_NETWARE = "netware";
77
78 public static final String FAMILY_OS2 = "os/2";
79
80 public static final String FAMILY_TANDEM = "tandem";
81
82 public static final String FAMILY_UNIX = "unix";
83
84 public static final String FAMILY_WINDOWS = "windows";
85
86 public static final String FAMILY_WIN9X = "win9x";
87
88 public static final String FAMILY_ZOS = "z/os";
89
90 public static final String FAMILY_OS400 = "os/400";
91
92 public static final String FAMILY_OPENVMS = "openvms";
93
94
95 private static final Set<String> validFamilies = setValidFamilies();
96
97
98 private static final String PATH_SEP = System.getProperty("path.separator");
99
100 public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
101
102 public static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.US);
103
104 public static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.US);
105
106
107 public static final String OS_FAMILY = getOsFamily();
108
109 private String family;
110
111 private String name;
112
113 private String version;
114
115 private String arch;
116
117
118
119
120 public Os() {}
121
122
123
124
125
126
127 public Os(String family) {
128 setFamily(family);
129 }
130
131
132
133
134 private static Set<String> setValidFamilies() {
135 Set<String> valid = new HashSet<String>();
136 valid.add(FAMILY_DOS);
137 valid.add(FAMILY_MAC);
138 valid.add(FAMILY_NETWARE);
139 valid.add(FAMILY_OS2);
140 valid.add(FAMILY_TANDEM);
141 valid.add(FAMILY_UNIX);
142 valid.add(FAMILY_WINDOWS);
143 valid.add(FAMILY_WIN9X);
144 valid.add(FAMILY_ZOS);
145 valid.add(FAMILY_OS400);
146 valid.add(FAMILY_OPENVMS);
147
148 return valid;
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public void setFamily(String f) {
171 family = f.toLowerCase(Locale.US);
172 }
173
174
175
176
177
178
179 public void setName(String name) {
180 this.name = name.toLowerCase(Locale.US);
181 }
182
183
184
185
186
187
188 public void setArch(String arch) {
189 this.arch = arch.toLowerCase(Locale.US);
190 }
191
192
193
194
195
196
197 public void setVersion(String version) {
198 this.version = version.toLowerCase(Locale.US);
199 }
200
201
202
203
204
205
206
207 public boolean eval() throws Exception {
208 return isOs(family, name, arch, version);
209 }
210
211
212
213
214
215
216
217
218 public static boolean isFamily(String family) {
219 return isOs(family, null, null, null);
220 }
221
222
223
224
225
226
227
228
229 public static boolean isName(String name) {
230 return isOs(null, name, null, null);
231 }
232
233
234
235
236
237
238
239
240 public static boolean isArch(String arch) {
241 return isOs(null, null, arch, null);
242 }
243
244
245
246
247
248
249
250
251 public static boolean isVersion(String version) {
252 return isOs(null, null, null, version);
253 }
254
255
256
257
258
259
260
261
262
263
264
265
266 public static boolean isOs(String family, String name, String arch, String version) {
267 boolean retValue = false;
268
269 if (family != null || name != null || arch != null || version != null) {
270
271 boolean isFamily = true;
272 boolean isName = true;
273 boolean isArch = true;
274 boolean isVersion = true;
275
276 if (family != null) {
277 if (family.equalsIgnoreCase(FAMILY_WINDOWS)) {
278 isFamily = OS_NAME.contains(FAMILY_WINDOWS);
279 } else if (family.equalsIgnoreCase(FAMILY_OS2)) {
280 isFamily = OS_NAME.contains(FAMILY_OS2);
281 } else if (family.equalsIgnoreCase(FAMILY_NETWARE)) {
282 isFamily = OS_NAME.contains(FAMILY_NETWARE);
283 } else if (family.equalsIgnoreCase(FAMILY_DOS)) {
284 isFamily = PATH_SEP.equals(";")
285 && !isFamily(FAMILY_NETWARE)
286 && !isFamily(FAMILY_WINDOWS)
287 && !isFamily(FAMILY_WIN9X);
288
289 } else if (family.equalsIgnoreCase(FAMILY_MAC)) {
290 isFamily = OS_NAME.contains(FAMILY_MAC);
291 } else if (family.equalsIgnoreCase(FAMILY_TANDEM)) {
292 isFamily = OS_NAME.contains("nonstop_kernel");
293 } else if (family.equalsIgnoreCase(FAMILY_UNIX)) {
294 isFamily = PATH_SEP.equals(":")
295 && !isFamily(FAMILY_OPENVMS)
296 && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x"));
297 } else if (family.equalsIgnoreCase(FAMILY_WIN9X)) {
298 isFamily = isFamily(FAMILY_WINDOWS)
299 && (OS_NAME.contains("95")
300 || OS_NAME.contains("98")
301 || OS_NAME.contains("me")
302 || OS_NAME.contains("ce"));
303 } else if (family.equalsIgnoreCase(FAMILY_ZOS)) {
304 isFamily = OS_NAME.contains(FAMILY_ZOS) || OS_NAME.contains("os/390");
305 } else if (family.equalsIgnoreCase(FAMILY_OS400)) {
306 isFamily = OS_NAME.contains(FAMILY_OS400);
307 } else if (family.equalsIgnoreCase(FAMILY_OPENVMS)) {
308 isFamily = OS_NAME.contains(FAMILY_OPENVMS);
309 } else {
310 isFamily = OS_NAME.contains(family.toLowerCase(Locale.US));
311 }
312 }
313 if (name != null) {
314 isName = name.toLowerCase(Locale.US).equals(OS_NAME);
315 }
316 if (arch != null) {
317 isArch = arch.toLowerCase(Locale.US).equals(OS_ARCH);
318 }
319 if (version != null) {
320 isVersion = version.toLowerCase(Locale.US).equals(OS_VERSION);
321 }
322 retValue = isFamily && isName && isArch && isVersion;
323 }
324 return retValue;
325 }
326
327
328
329
330
331
332
333 private static String getOsFamily() {
334
335
336
337 Set<String> families = null;
338 if (!validFamilies.isEmpty()) {
339 families = validFamilies;
340 } else {
341 families = setValidFamilies();
342 }
343 for (String fam : families) {
344 if (Os.isFamily(fam)) {
345 return fam;
346 }
347 }
348 return null;
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371 public static boolean isValidFamily(String theFamily) {
372 return (validFamilies.contains(theFamily));
373 }
374
375
376
377
378
379 public static Set<String> getValidFamilies() {
380 return new HashSet<String>(validFamilies);
381 }
382 }