1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.exec;
18
19 import java.io.File;
20 import java.util.Locale;
21
22
23
24
25
26
27 public final class OS {
28
29
30
31
32 public static final String FAMILY_9X = "win9x";
33
34
35
36 public static final String FAMILY_DOS = "dos";
37
38
39
40 public static final String FAMILY_MAC = "mac";
41
42
43
44 public static final String FAMILY_NETWARE = "netware";
45
46
47
48 public static final String FAMILY_NT = "winnt";
49
50
51
52 public static final String FAMILY_OS2 = "os/2";
53
54
55
56 public static final String FAMILY_OS400 = "os/400";
57
58
59
60 public static final String FAMILY_TANDEM = "tandem";
61
62
63
64 public static final String FAMILY_UNIX = "unix";
65
66
67
68 public static final String FAMILY_VMS = "openvms";
69
70
71
72 public static final String FAMILY_WINDOWS = "windows";
73
74
75
76 public static final String FAMILY_ZOS = "z/os";
77
78 private static final String DARWIN = "darwin";
79
80 private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
81 private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
82 private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ENGLISH);
83 private static final String PATH_SEP = File.pathSeparator;
84
85
86
87
88
89
90
91 public static boolean isArch(final String arch) {
92 return isOs(null, null, arch, null);
93 }
94
95
96
97
98
99
100
101 private static boolean isFamily(final String family) {
102 return isOs(family, null, null, null);
103 }
104
105
106
107
108
109
110 public static boolean isFamilyDOS() {
111 return isFamily(FAMILY_DOS);
112 }
113
114
115
116
117
118
119 public static boolean isFamilyMac() {
120 return isFamily(FAMILY_MAC);
121 }
122
123
124
125
126
127
128 public static boolean isFamilyNetware() {
129 return isFamily(FAMILY_NETWARE);
130 }
131
132
133
134
135
136
137 public static boolean isFamilyOpenVms() {
138 return isFamily(FAMILY_VMS);
139 }
140
141
142
143
144
145
146 public static boolean isFamilyOS2() {
147 return isFamily(FAMILY_OS2);
148 }
149
150
151
152
153
154
155 public static boolean isFamilyOS400() {
156 return isFamily(FAMILY_OS400);
157 }
158
159
160
161
162
163
164 public static boolean isFamilyTandem() {
165 return isFamily(FAMILY_TANDEM);
166 }
167
168
169
170
171
172
173 public static boolean isFamilyUnix() {
174 return isFamily(FAMILY_UNIX);
175 }
176
177
178
179
180
181
182 public static boolean isFamilyWin9x() {
183 return isFamily(FAMILY_9X);
184 }
185
186
187
188
189
190
191 public static boolean isFamilyWindows() {
192 return isFamily(FAMILY_WINDOWS);
193 }
194
195
196
197
198
199
200 public static boolean isFamilyWinNT() {
201 return isFamily(FAMILY_NT);
202 }
203
204
205
206
207
208
209 public static boolean isFamilyZOS() {
210 return isFamily(FAMILY_ZOS);
211 }
212
213
214
215
216
217
218
219 public static boolean isName(final String name) {
220 return isOs(null, name, null, null);
221 }
222
223
224
225
226
227
228
229
230
231
232 public static boolean isOs(final String family, final String name, final String arch, final String version) {
233 boolean retValue = false;
234 if (family != null || name != null || arch != null || version != null) {
235 boolean isFamily = true;
236 boolean isName = true;
237 boolean isArch = true;
238 boolean isVersion = true;
239 if (family != null) {
240
241 final boolean isWindows = OS_NAME.contains(FAMILY_WINDOWS);
242 boolean is9x = false;
243 boolean isNT = false;
244 if (isWindows) {
245
246 is9x = OS_NAME.contains("95") || OS_NAME.contains("98") || OS_NAME.contains("me")
247
248
249 || OS_NAME.contains("ce");
250 isNT = !is9x;
251 }
252 switch (family) {
253 case FAMILY_WINDOWS:
254 isFamily = isWindows;
255 break;
256 case FAMILY_9X:
257 isFamily = isWindows && is9x;
258 break;
259 case FAMILY_NT:
260 isFamily = isWindows && isNT;
261 break;
262 case FAMILY_OS2:
263 isFamily = OS_NAME.contains(FAMILY_OS2);
264 break;
265 case FAMILY_NETWARE:
266 isFamily = OS_NAME.contains(FAMILY_NETWARE);
267 break;
268 case FAMILY_DOS:
269 isFamily = PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE);
270 break;
271 case FAMILY_MAC:
272 isFamily = OS_NAME.contains(FAMILY_MAC) || OS_NAME.contains(DARWIN);
273 break;
274 case FAMILY_TANDEM:
275 isFamily = OS_NAME.contains("nonstop_kernel");
276 break;
277 case FAMILY_UNIX:
278 isFamily = PATH_SEP.equals(":") && !isFamily(FAMILY_VMS) && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x") || OS_NAME.contains(DARWIN));
279 break;
280 case FAMILY_ZOS:
281 isFamily = OS_NAME.contains(FAMILY_ZOS) || OS_NAME.contains("os/390");
282 break;
283 case FAMILY_OS400:
284 isFamily = OS_NAME.contains(FAMILY_OS400);
285 break;
286 case FAMILY_VMS:
287 isFamily = OS_NAME.contains(FAMILY_VMS);
288 break;
289 default:
290 throw new IllegalArgumentException("Don\'t know how to detect OS family \"" + family + "\"");
291 }
292 }
293 if (name != null) {
294 isName = name.equals(OS_NAME);
295 }
296 if (arch != null) {
297 isArch = arch.equals(OS_ARCH);
298 }
299 if (version != null) {
300 isVersion = version.equals(OS_VERSION);
301 }
302 retValue = isFamily && isName && isArch && isVersion;
303 }
304 return retValue;
305 }
306
307
308
309
310
311
312
313 public static boolean isVersion(final String version) {
314 return isOs(null, null, null, version);
315 }
316
317
318
319
320 private OS() {
321 }
322 }