KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > util > Os


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.util;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Set JavaDoc;
23
24 /**
25  * Class to help determining the OS.
26  *
27  * @author <a HREF="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
28  * @author <a HREF="mailto:umagesh@apache.org">Magesh Umasankar</a>
29  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
30  */

31 public final class Os
32 {
33     private static final String JavaDoc OS_NAME =
34         System.getProperty("os.name").toLowerCase(Locale.US);
35     private static final String JavaDoc OS_ARCH =
36         System.getProperty("os.arch").toLowerCase(Locale.US);
37     private static final String JavaDoc OS_VERSION =
38         System.getProperty("os.version").toLowerCase(Locale.US);
39     private static final String JavaDoc PATH_SEP =
40         System.getProperty("path.separator");
41     private static final OsFamily OS_FAMILY;
42     private static final OsFamily[] OS_ALL_FAMILIES;
43
44     /**
45      * All Windows based OSes.
46      */

47     public static final OsFamily OS_FAMILY_WINDOWS = new OsFamily("windows");
48
49     /**
50      * All DOS based OSes.
51      */

52     public static final OsFamily OS_FAMILY_DOS = new OsFamily("dos");
53
54     /**
55      * All Windows NT based OSes.
56      */

57     public static final OsFamily OS_FAMILY_WINNT =
58         new OsFamily("nt", new OsFamily[]{OS_FAMILY_WINDOWS});
59
60     /**
61      * All Windows 9x based OSes.
62      */

63     public static final OsFamily OS_FAMILY_WIN9X =
64         new OsFamily("win9x", new OsFamily[]{OS_FAMILY_WINDOWS, OS_FAMILY_DOS});
65
66     /**
67      * OS/2
68      */

69     public static final OsFamily OS_FAMILY_OS2 =
70         new OsFamily("os/2", new OsFamily[]{OS_FAMILY_DOS});
71
72     /**
73      * Netware
74      */

75     public static final OsFamily OS_FAMILY_NETWARE =
76         new OsFamily("netware");
77
78     /**
79      * All UNIX based OSes.
80      */

81     public static final OsFamily OS_FAMILY_UNIX = new OsFamily("unix");
82
83     /**
84      * All Mac based OSes.
85      */

86     public static final OsFamily OS_FAMILY_MAC = new OsFamily("mac");
87
88     /**
89      * OSX
90      */

91     public static final OsFamily OS_FAMILY_OSX =
92         new OsFamily("osx", new OsFamily[]{OS_FAMILY_UNIX, OS_FAMILY_MAC});
93
94     private static final OsFamily[] ALL_FAMILIES = new OsFamily[]
95     {
96         OS_FAMILY_DOS,
97         OS_FAMILY_MAC,
98         OS_FAMILY_NETWARE,
99         OS_FAMILY_OS2,
100         OS_FAMILY_OSX,
101         OS_FAMILY_UNIX,
102         OS_FAMILY_WINDOWS,
103         OS_FAMILY_WINNT,
104         OS_FAMILY_WIN9X
105     };
106
107     static
108     {
109         OS_FAMILY = determineOsFamily();
110         OS_ALL_FAMILIES = determineAllFamilies();
111     }
112
113     /**
114      * Private constructor to block instantiation.
115      */

116     private Os()
117     {
118     }
119
120     /**
121      * Determines if the OS on which Ant is executing matches the given OS
122      * version.
123      */

124     public static boolean isVersion(final String JavaDoc version)
125     {
126         return isOs((OsFamily) null, null, null, version);
127     }
128
129     /**
130      * Determines if the OS on which Ant is executing matches the given OS
131      * architecture.
132      */

133     public static boolean isArch(final String JavaDoc arch)
134     {
135         return isOs((OsFamily) null, null, arch, null);
136     }
137
138     /**
139      * Determines if the OS on which Ant is executing matches the given OS
140      * family.
141      */

142     public static boolean isFamily(final String JavaDoc family)
143     {
144         return isOs(family, null, null, null);
145     }
146
147     /**
148      * Determines if the OS on which Ant is executing matches the given OS
149      * family.
150      */

151     public static boolean isFamily(final OsFamily family)
152     {
153         return isOs(family, null, null, null);
154     }
155
156     /**
157      * Determines if the OS on which Ant is executing matches the given OS name.
158      *
159      * @param name Description of Parameter
160      * @return The Name value
161      * @since 1.7
162      */

163     public static boolean isName(final String JavaDoc name)
164     {
165         return isOs((OsFamily) null, name, null, null);
166     }
167
168     /**
169      * Determines if the OS on which Ant is executing matches the given OS
170      * family, name, architecture and version.
171      *
172      * @param family The OS family
173      * @param name The OS name
174      * @param arch The OS architecture
175      * @param version The OS version
176      * @return The Os value
177      */

178     public static boolean isOs(final String JavaDoc family,
179                                final String JavaDoc name,
180                                final String JavaDoc arch,
181                                final String JavaDoc version)
182     {
183         return isOs(getFamily(family), name, arch, version);
184     }
185
186     /**
187      * Determines if the OS on which Ant is executing matches the given OS
188      * family, name, architecture and version
189      *
190      * @param family The OS family
191      * @param name The OS name
192      * @param arch The OS architecture
193      * @param version The OS version
194      * @return The Os value
195      */

196     public static boolean isOs(final OsFamily family,
197                                final String JavaDoc name,
198                                final String JavaDoc arch,
199                                final String JavaDoc version)
200     {
201         if (family != null || name != null || arch != null || version != null)
202         {
203             final boolean isFamily = familyMatches(family);
204             final boolean isName = nameMatches(name);
205             final boolean isArch = archMatches(arch);
206             final boolean isVersion = versionMatches(version);
207
208             return isFamily && isName && isArch && isVersion;
209         }
210         else
211         {
212             return false;
213         }
214     }
215
216     /**
217      * Locates an OsFamily by name (case-insensitive).
218      *
219      * @return the OS family, or null if not found.
220      */

221     public static OsFamily getFamily(final String JavaDoc name)
222     {
223         for (int i = 0; i < ALL_FAMILIES.length; i++)
224         {
225             final OsFamily osFamily = ALL_FAMILIES[i];
226             if (osFamily.getName().equalsIgnoreCase(name))
227             {
228                 return osFamily;
229             }
230         }
231
232         return null;
233     }
234
235     private static boolean versionMatches(final String JavaDoc version)
236     {
237         boolean isVersion = true;
238         if (version != null)
239         {
240             isVersion = version.equalsIgnoreCase(OS_VERSION);
241         }
242         return isVersion;
243     }
244
245     private static boolean archMatches(final String JavaDoc arch)
246     {
247         boolean isArch = true;
248         if (arch != null)
249         {
250             isArch = arch.equalsIgnoreCase(OS_ARCH);
251         }
252         return isArch;
253     }
254
255     private static boolean nameMatches(final String JavaDoc name)
256     {
257         boolean isName = true;
258         if (name != null)
259         {
260             isName = name.equalsIgnoreCase(OS_NAME);
261         }
262         return isName;
263     }
264
265     private static boolean familyMatches(final OsFamily family)
266     {
267         if (family == null)
268         {
269             return false;
270         }
271         for (int i = 0; i < OS_ALL_FAMILIES.length; i++)
272         {
273             final OsFamily osFamily = OS_ALL_FAMILIES[i];
274             if (family == osFamily)
275             {
276                 return true;
277             }
278         }
279         return false;
280     }
281
282     private static OsFamily[] determineAllFamilies()
283     {
284         // Determine all families the current OS belongs to
285
Set JavaDoc allFamilies = new HashSet JavaDoc();
286         if (OS_FAMILY != null)
287         {
288             List JavaDoc queue = new ArrayList JavaDoc();
289             queue.add(OS_FAMILY);
290             while (queue.size() > 0)
291             {
292                 final OsFamily family = (OsFamily) queue.remove(0);
293                 allFamilies.add(family);
294                 final OsFamily[] families = family.getFamilies();
295                 for (int i = 0; i < families.length; i++)
296                 {
297                     OsFamily parent = families[i];
298                     queue.add(parent);
299                 }
300             }
301         }
302         return (OsFamily[]) allFamilies.toArray(new OsFamily[allFamilies.size()]);
303     }
304
305     private static OsFamily determineOsFamily()
306     {
307         // Determine the most specific OS family
308
if (OS_NAME.indexOf("windows") > -1)
309         {
310             if (OS_NAME.indexOf("xp") > -1
311                 || OS_NAME.indexOf("2000") > -1
312                 || OS_NAME.indexOf("nt") > -1)
313             {
314                 return OS_FAMILY_WINNT;
315             }
316             else
317             {
318                 return OS_FAMILY_WIN9X;
319             }
320         }
321         else if (OS_NAME.indexOf("os/2") > -1)
322         {
323             return OS_FAMILY_OS2;
324         }
325         else if (OS_NAME.indexOf("netware") > -1)
326         {
327             return OS_FAMILY_NETWARE;
328         }
329         else if (OS_NAME.indexOf("mac") > -1)
330         {
331             if (OS_NAME.endsWith("x"))
332             {
333                 return OS_FAMILY_OSX;
334             }
335             else
336             {
337                 return OS_FAMILY_MAC;
338             }
339         }
340         else if (PATH_SEP.equals(":"))
341         {
342             return OS_FAMILY_UNIX;
343         }
344         else
345         {
346             return null;
347         }
348     }
349 }
350
Popular Tags