1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.util;
18
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Locale;
23 import java.util.Set;
24
25 /***
26 * Class to help determining the OS.
27 *
28 * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
29 * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
30 * @author <a href="mailto:peter@apache.org">Peter Donald</a>
31 */
32 public final class Os
33 {
34 private static final String OS_NAME =
35 System.getProperty("os.name").toLowerCase(Locale.US);
36 private static final String OS_ARCH =
37 System.getProperty("os.arch").toLowerCase(Locale.US);
38 private static final String OS_VERSION =
39 System.getProperty("os.version").toLowerCase(Locale.US);
40 private static final String PATH_SEP =
41 System.getProperty("path.separator");
42 private static final OsFamily OS_FAMILY;
43 private static final OsFamily[] OS_ALL_FAMILIES;
44
45 /***
46 * All Windows based OSes.
47 */
48 public static final OsFamily OS_FAMILY_WINDOWS = new OsFamily("windows");
49
50 /***
51 * All DOS based OSes.
52 */
53 public static final OsFamily OS_FAMILY_DOS = new OsFamily("dos");
54
55 /***
56 * All Windows NT based OSes.
57 */
58 public static final OsFamily OS_FAMILY_WINNT =
59 new OsFamily("nt", new OsFamily[]{OS_FAMILY_WINDOWS});
60
61 /***
62 * All Windows 9x based OSes.
63 */
64 public static final OsFamily OS_FAMILY_WIN9X =
65 new OsFamily("win9x", new OsFamily[]{OS_FAMILY_WINDOWS, OS_FAMILY_DOS});
66
67 /***
68 * OS/2
69 */
70 public static final OsFamily OS_FAMILY_OS2 =
71 new OsFamily("os/2", new OsFamily[]{OS_FAMILY_DOS});
72
73 /***
74 * Netware
75 */
76 public static final OsFamily OS_FAMILY_NETWARE =
77 new OsFamily("netware");
78
79 /***
80 * All UNIX based OSes.
81 */
82 public static final OsFamily OS_FAMILY_UNIX = new OsFamily("unix");
83
84 /***
85 * All Mac based OSes.
86 */
87 public static final OsFamily OS_FAMILY_MAC = new OsFamily("mac");
88
89 /***
90 * OSX
91 */
92 public static final OsFamily OS_FAMILY_OSX =
93 new OsFamily("osx", new OsFamily[]{OS_FAMILY_UNIX, OS_FAMILY_MAC});
94
95 private static final OsFamily[] ALL_FAMILIES = new OsFamily[]
96 {
97 OS_FAMILY_DOS,
98 OS_FAMILY_MAC,
99 OS_FAMILY_NETWARE,
100 OS_FAMILY_OS2,
101 OS_FAMILY_OSX,
102 OS_FAMILY_UNIX,
103 OS_FAMILY_WINDOWS,
104 OS_FAMILY_WINNT,
105 OS_FAMILY_WIN9X
106 };
107
108 static
109 {
110 OS_FAMILY = determineOsFamily();
111 OS_ALL_FAMILIES = determineAllFamilies();
112 }
113
114 /***
115 * Private constructor to block instantiation.
116 */
117 private Os()
118 {
119 }
120
121 /***
122 * Determines if the OS on which Ant is executing matches the given OS
123 * version.
124 */
125 public static boolean isVersion(final String version)
126 {
127 return isOs((OsFamily) null, null, null, version);
128 }
129
130 /***
131 * Determines if the OS on which Ant is executing matches the given OS
132 * architecture.
133 */
134 public static boolean isArch(final String arch)
135 {
136 return isOs((OsFamily) null, null, arch, null);
137 }
138
139 /***
140 * Determines if the OS on which Ant is executing matches the given OS
141 * family.
142 */
143 public static boolean isFamily(final String family)
144 {
145 return isOs(family, null, null, null);
146 }
147
148 /***
149 * Determines if the OS on which Ant is executing matches the given OS
150 * family.
151 */
152 public static boolean isFamily(final OsFamily family)
153 {
154 return isOs(family, null, null, null);
155 }
156
157 /***
158 * Determines if the OS on which Ant is executing matches the given OS name.
159 *
160 * @param name Description of Parameter
161 * @return The Name value
162 * @since 1.7
163 */
164 public static boolean isName(final String name)
165 {
166 return isOs((OsFamily) null, name, null, null);
167 }
168
169 /***
170 * Determines if the OS on which Ant is executing matches the given OS
171 * family, name, architecture and version.
172 *
173 * @param family The OS family
174 * @param name The OS name
175 * @param arch The OS architecture
176 * @param version The OS version
177 * @return The Os value
178 */
179 public static boolean isOs(final String family,
180 final String name,
181 final String arch,
182 final String version)
183 {
184 return isOs(getFamily(family), name, arch, version);
185 }
186
187 /***
188 * Determines if the OS on which Ant is executing matches the given OS
189 * family, name, architecture and version
190 *
191 * @param family The OS family
192 * @param name The OS name
193 * @param arch The OS architecture
194 * @param version The OS version
195 * @return The Os value
196 */
197 public static boolean isOs(final OsFamily family,
198 final String name,
199 final String arch,
200 final String version)
201 {
202 if (family != null || name != null || arch != null || version != null)
203 {
204 final boolean isFamily = familyMatches(family);
205 final boolean isName = nameMatches(name);
206 final boolean isArch = archMatches(arch);
207 final boolean isVersion = versionMatches(version);
208
209 return isFamily && isName && isArch && isVersion;
210 }
211 else
212 {
213 return false;
214 }
215 }
216
217 /***
218 * Locates an OsFamily by name (case-insensitive).
219 *
220 * @return the OS family, or null if not found.
221 */
222 public static OsFamily getFamily(final String name)
223 {
224 for (int i = 0; i < ALL_FAMILIES.length; i++)
225 {
226 final OsFamily osFamily = ALL_FAMILIES[i];
227 if (osFamily.getName().equalsIgnoreCase(name))
228 {
229 return osFamily;
230 }
231 }
232
233 return null;
234 }
235
236 private static boolean versionMatches(final String version)
237 {
238 boolean isVersion = true;
239 if (version != null)
240 {
241 isVersion = version.equalsIgnoreCase(OS_VERSION);
242 }
243 return isVersion;
244 }
245
246 private static boolean archMatches(final String arch)
247 {
248 boolean isArch = true;
249 if (arch != null)
250 {
251 isArch = arch.equalsIgnoreCase(OS_ARCH);
252 }
253 return isArch;
254 }
255
256 private static boolean nameMatches(final String name)
257 {
258 boolean isName = true;
259 if (name != null)
260 {
261 isName = name.equalsIgnoreCase(OS_NAME);
262 }
263 return isName;
264 }
265
266 private static boolean familyMatches(final OsFamily family)
267 {
268 if (family == null)
269 {
270 return false;
271 }
272 for (int i = 0; i < OS_ALL_FAMILIES.length; i++)
273 {
274 final OsFamily osFamily = OS_ALL_FAMILIES[i];
275 if (family == osFamily)
276 {
277 return true;
278 }
279 }
280 return false;
281 }
282
283 private static OsFamily[] determineAllFamilies()
284 {
285
286 Set allFamilies = new HashSet();
287 if (OS_FAMILY != null)
288 {
289 List queue = new ArrayList();
290 queue.add(OS_FAMILY);
291 while (queue.size() > 0)
292 {
293 final OsFamily family = (OsFamily) queue.remove(0);
294 allFamilies.add(family);
295 final OsFamily[] families = family.getFamilies();
296 for (int i = 0; i < families.length; i++)
297 {
298 OsFamily parent = families[i];
299 queue.add(parent);
300 }
301 }
302 }
303 return (OsFamily[]) allFamilies.toArray(new OsFamily[allFamilies.size()]);
304 }
305
306 private static OsFamily determineOsFamily()
307 {
308
309 if (OS_NAME.indexOf("windows") > -1)
310 {
311 if (OS_NAME.indexOf("xp") > -1
312 || OS_NAME.indexOf("2000") > -1
313 || OS_NAME.indexOf("nt") > -1)
314 {
315 return OS_FAMILY_WINNT;
316 }
317 else
318 {
319 return OS_FAMILY_WIN9X;
320 }
321 }
322 else if (OS_NAME.indexOf("os/2") > -1)
323 {
324 return OS_FAMILY_OS2;
325 }
326 else if (OS_NAME.indexOf("netware") > -1)
327 {
328 return OS_FAMILY_NETWARE;
329 }
330 else if (OS_NAME.indexOf("mac") > -1)
331 {
332 if (OS_NAME.endsWith("x"))
333 {
334 return OS_FAMILY_OSX;
335 }
336 else
337 {
338 return OS_FAMILY_MAC;
339 }
340 }
341 else if (PATH_SEP.equals(":"))
342 {
343 return OS_FAMILY_UNIX;
344 }
345 else
346 {
347 return null;
348 }
349 }
350 }