View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.util;
18  
19  import java.io.File;
20  import java.util.ArrayList;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.Set;
25  
26  /**
27   * Class to help determining the OS.
28   */
29  public final class Os {
30  
31      /**
32       * All Windows based OSes.
33       */
34      public static final OsFamilyhtml#OsFamily">OsFamily OS_FAMILY_WINDOWS = new OsFamily("windows");
35  
36      /**
37       * All DOS based OSes.
38       */
39      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_DOS = new OsFamily("dos");
40  
41      /**
42       * All Windows NT based OSes.
43       */
44      public static final OsFamilyy.html#OsFamily">OsFamily OS_FAMILY_WINNT = new OsFamily("nt", new OsFamily[] { OS_FAMILY_WINDOWS });
45  
46      /**
47       * All Windows 9x based OSes.
48       */
49      public static final OsFamilyy.html#OsFamily">OsFamily OS_FAMILY_WIN9X = new OsFamily("win9x",
50              new OsFamily[] { OS_FAMILY_WINDOWS, OS_FAMILY_DOS });
51  
52      /**
53       * OS/2.
54       */
55      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_OS2 = new OsFamily("os/2", new OsFamily[] { OS_FAMILY_DOS });
56  
57      /**
58       * Netware.
59       */
60      public static final OsFamilyhtml#OsFamily">OsFamily OS_FAMILY_NETWARE = new OsFamily("netware");
61  
62      /**
63       * All UNIX based OSes.
64       */
65      public static final OsFamilyly.html#OsFamily">OsFamily OS_FAMILY_UNIX = new OsFamily("unix");
66  
67      /**
68       * All Mac based OSes.
69       */
70      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_MAC = new OsFamily("mac");
71  
72      /**
73       * OSX.
74       */
75      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_OSX = new OsFamily("osx", new OsFamily[] { OS_FAMILY_UNIX, OS_FAMILY_MAC });
76  
77      private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
78      private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.US);
79      private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.US);
80      private static final String PATH_SEP = File.pathSeparator;
81      private static final OsFamily OS_FAMILY;
82      private static final OsFamily[] OS_ALL_FAMILIES;
83  
84      private static final OsFamilyly.html#OsFamily">OsFamily[] ALL_FAMILIES = new OsFamily[] { OS_FAMILY_DOS, OS_FAMILY_MAC, OS_FAMILY_NETWARE,
85              OS_FAMILY_OS2, OS_FAMILY_OSX, OS_FAMILY_UNIX, OS_FAMILY_WINDOWS, OS_FAMILY_WINNT, OS_FAMILY_WIN9X };
86  
87      static {
88          OS_FAMILY = determineOsFamily();
89          OS_ALL_FAMILIES = determineAllFamilies();
90      }
91  
92      /**
93       * Private constructor to block instantiation.
94       */
95      private Os() {
96      }
97  
98      /**
99       * Determines if the OS on which Ant is executing matches the given OS version.
100      *
101      * @param version The version to check.
102      * @return true if the version matches.
103      */
104     public static boolean isVersion(final String version) {
105         return isOs((OsFamily) null, null, null, version);
106     }
107 
108     /**
109      * Determines if the OS on which Ant is executing matches the given OS architecture.
110      *
111      * @param arch The architecture to check.
112      * @return true if the architecture mataches.
113      */
114     public static boolean isArch(final String arch) {
115         return isOs((OsFamily) null, null, arch, null);
116     }
117 
118     /**
119      * Determines if the OS on which Ant is executing matches the given OS family.
120      *
121      * @param family The family to check.
122      * @return true if the family matches.
123      */
124     public static boolean isFamily(final String family) {
125         return isOs(family, null, null, null);
126     }
127 
128     /**
129      * Determines if the OS on which Ant is executing matches the given OS family.
130      *
131      * @param family The family to check.
132      * @return true if the family matches.
133      */
134     public static boolean isFamily(final OsFamily family) {
135         return isOs(family, null, null, null);
136     }
137 
138     /**
139      * Determines if the OS on which Ant is executing matches the given OS name.
140      *
141      * @param name Description of Parameter
142      * @return The Name value
143      * @since 1.7
144      */
145     public static boolean isName(final String name) {
146         return isOs((OsFamily) null, name, null, null);
147     }
148 
149     /**
150      * Determines if the OS on which Ant is executing matches the given OS family, name, architecture and version.
151      *
152      * @param family The OS family
153      * @param name The OS name
154      * @param arch The OS architecture
155      * @param version The OS version
156      * @return The Os value
157      */
158     public static boolean isOs(final String family, final String name, final String arch, final String version) {
159         return isOs(getFamily(family), name, arch, version);
160     }
161 
162     /**
163      * Determines if the OS on which Ant is executing matches the given OS family, name, architecture and version.
164      *
165      * @param family The OS family
166      * @param name The OS name
167      * @param arch The OS architecture
168      * @param version The OS version
169      * @return The Os value
170      */
171     public static boolean isOs(final OsFamily family, final String name, final String arch, final String version) {
172         if (family != null || name != null || arch != null || version != null) {
173             final boolean isFamily = familyMatches(family);
174             final boolean isName = nameMatches(name);
175             final boolean isArch = archMatches(arch);
176             final boolean isVersion = versionMatches(version);
177 
178             return isFamily && isName && isArch && isVersion;
179         }
180         return false;
181     }
182 
183     /**
184      * Locates an OsFamily by name (case-insensitive).
185      *
186      * @param name The family name to lookup.
187      * @return the OS family, or null if not found.
188      */
189     public static OsFamily getFamily(final String name) {
190         for (final OsFamily osFamily : ALL_FAMILIES) {
191             if (osFamily.getName().equalsIgnoreCase(name)) {
192                 return osFamily;
193             }
194         }
195 
196         return null;
197     }
198 
199     private static boolean versionMatches(final String version) {
200         boolean isVersion = true;
201         if (version != null) {
202             isVersion = version.equalsIgnoreCase(OS_VERSION);
203         }
204         return isVersion;
205     }
206 
207     private static boolean archMatches(final String arch) {
208         boolean isArch = true;
209         if (arch != null) {
210             isArch = arch.equalsIgnoreCase(OS_ARCH);
211         }
212         return isArch;
213     }
214 
215     private static boolean nameMatches(final String name) {
216         boolean isName = true;
217         if (name != null) {
218             isName = name.equalsIgnoreCase(OS_NAME);
219         }
220         return isName;
221     }
222 
223     private static boolean familyMatches(final OsFamily family) {
224         if (family == null) {
225             return false;
226         }
227         for (final OsFamily osFamily : OS_ALL_FAMILIES) {
228             if (family == osFamily) {
229                 return true;
230             }
231         }
232         return false;
233     }
234 
235     private static OsFamily[] determineAllFamilies() {
236         // Determine all families the current OS belongs to
237         final Set<OsFamily> allFamilies = new HashSet<>();
238         if (OS_FAMILY != null) {
239             final List<OsFamily> queue = new ArrayList<>();
240             queue.add(OS_FAMILY);
241             while (queue.size() > 0) {
242                 final OsFamily family = queue.remove(0);
243                 allFamilies.add(family);
244                 final OsFamily[] families = family.getFamilies();
245                 for (final OsFamily parent : families) {
246                     queue.add(parent);
247                 }
248             }
249         }
250         return allFamilies.toArray(new OsFamily[allFamilies.size()]);
251     }
252 
253     private static OsFamily determineOsFamily() {
254         // Determine the most specific OS family
255         if (OS_NAME.indexOf("windows") > -1) {
256             if (OS_NAME.indexOf("xp") > -1 || OS_NAME.indexOf("2000") > -1 || OS_NAME.indexOf("nt") > -1) {
257                 return OS_FAMILY_WINNT;
258             }
259             return OS_FAMILY_WIN9X;
260         } else if (OS_NAME.indexOf("os/2") > -1) {
261             return OS_FAMILY_OS2;
262         } else if (OS_NAME.indexOf("netware") > -1) {
263             return OS_FAMILY_NETWARE;
264         } else if (OS_NAME.indexOf("mac") > -1) {
265             if (OS_NAME.endsWith("x")) {
266                 return OS_FAMILY_OSX;
267             }
268             return OS_FAMILY_MAC;
269         } else if (PATH_SEP.equals(":")) {
270             return OS_FAMILY_UNIX;
271         } else {
272             return null;
273         }
274     }
275 }