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