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.Collections;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Set;
26  
27  import org.apache.commons.lang3.SystemUtils;
28  
29  /**
30   * Class to help determining the OS.
31   *
32   * @deprecated Use Apache Commons Lang's {@link SystemUtils}. Remove in 3.0.
33   */
34  @Deprecated
35  public final class Os {
36  
37      /**
38       * All Windows based OSes.
39       */
40      public static final OsFamilyhtml#OsFamily">OsFamily OS_FAMILY_WINDOWS = new OsFamily("windows");
41  
42      /**
43       * All DOS based OSes.
44       */
45      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_DOS = new OsFamily("dos");
46  
47      /**
48       * All Windows NT based OSes.
49       */
50      public static final OsFamilyy.html#OsFamily">OsFamily OS_FAMILY_WINNT = new OsFamily("nt", new OsFamily[] { OS_FAMILY_WINDOWS });
51  
52      /**
53       * All Windows 9x based OSes.
54       */
55      public static final OsFamilyy.html#OsFamily">OsFamily OS_FAMILY_WIN9X = new OsFamily("win9x",
56              new OsFamily[] { OS_FAMILY_WINDOWS, OS_FAMILY_DOS });
57  
58      /**
59       * OS/2.
60       */
61      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_OS2 = new OsFamily("os/2", new OsFamily[] { OS_FAMILY_DOS });
62  
63      /**
64       * Netware.
65       */
66      public static final OsFamilyhtml#OsFamily">OsFamily OS_FAMILY_NETWARE = new OsFamily("netware");
67  
68      /**
69       * All UNIX based OSes.
70       */
71      public static final OsFamilyly.html#OsFamily">OsFamily OS_FAMILY_UNIX = new OsFamily("unix");
72  
73      /**
74       * All Mac based OSes.
75       */
76      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_MAC = new OsFamily("mac");
77  
78      /**
79       * OSX.
80       */
81      public static final OsFamilyily.html#OsFamily">OsFamily OS_FAMILY_OSX = new OsFamily("osx", new OsFamily[] { OS_FAMILY_UNIX, OS_FAMILY_MAC });
82  
83      private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
84      private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.US);
85      private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.US);
86      private static final String PATH_SEP = File.pathSeparator;
87      private static final OsFamily OS_FAMILY;
88      private static final OsFamily[] OS_ALL_FAMILIES;
89  
90      private static final OsFamilyly.html#OsFamily">OsFamily[] ALL_FAMILIES = new OsFamily[] { OS_FAMILY_DOS, OS_FAMILY_MAC, OS_FAMILY_NETWARE,
91              OS_FAMILY_OS2, OS_FAMILY_OSX, OS_FAMILY_UNIX, OS_FAMILY_WINDOWS, OS_FAMILY_WINNT, OS_FAMILY_WIN9X };
92  
93      static {
94          OS_FAMILY = determineOsFamily();
95          OS_ALL_FAMILIES = determineAllFamilies();
96      }
97  
98      /**
99       * Private constructor to block instantiation.
100      */
101     private Os() {
102     }
103 
104     /**
105      * Determines if the OS on which Ant is executing matches the given OS version.
106      *
107      * @param version The version to check.
108      * @return true if the version matches.
109      */
110     public static boolean isVersion(final String version) {
111         return isOs((OsFamily) null, null, null, version);
112     }
113 
114     /**
115      * Determines if the OS on which Ant is executing matches the given OS architecture.
116      *
117      * @param arch The architecture to check.
118      * @return true if the architecture mataches.
119      */
120     public static boolean isArch(final String arch) {
121         return isOs((OsFamily) null, null, arch, null);
122     }
123 
124     /**
125      * Determines if the OS on which Ant is executing matches the given OS family.
126      *
127      * @param family The family to check.
128      * @return true if the family matches.
129      */
130     public static boolean isFamily(final String family) {
131         return isOs(family, null, null, null);
132     }
133 
134     /**
135      * Determines if the OS on which Ant is executing matches the given OS family.
136      *
137      * @param family The family to check.
138      * @return true if the family matches.
139      */
140     public static boolean isFamily(final OsFamily family) {
141         return isOs(family, null, null, null);
142     }
143 
144     /**
145      * Determines if the OS on which Ant is executing matches the given OS name.
146      *
147      * @param name Description of Parameter
148      * @return The Name value
149      * @since 1.7
150      */
151     public static boolean isName(final String name) {
152         return isOs((OsFamily) null, name, null, null);
153     }
154 
155     /**
156      * Determines if the OS on which Ant is executing matches the given OS family, name, architecture and version.
157      *
158      * @param family The OS family
159      * @param name The OS name
160      * @param arch The OS architecture
161      * @param version The OS version
162      * @return The Os value
163      */
164     public static boolean isOs(final String family, final String name, final String arch, final String version) {
165         return isOs(getFamily(family), name, arch, version);
166     }
167 
168     /**
169      * Determines if the OS on which Ant is executing matches the given OS family, name, architecture and version.
170      *
171      * @param family The OS family
172      * @param name The OS name
173      * @param arch The OS architecture
174      * @param version The OS version
175      * @return The Os value
176      */
177     public static boolean isOs(final OsFamily family, final String name, final String arch, final String version) {
178         if (family != null || name != null || arch != null || version != null) {
179             final boolean isFamily = familyMatches(family);
180             final boolean isName = nameMatches(name);
181             final boolean isArch = archMatches(arch);
182             final boolean isVersion = versionMatches(version);
183 
184             return isFamily && isName && isArch && isVersion;
185         }
186         return false;
187     }
188 
189     /**
190      * Locates an OsFamily by name (case-insensitive).
191      *
192      * @param name The family name to lookup.
193      * @return the OS family, or null if not found.
194      */
195     public static OsFamily getFamily(final String name) {
196         for (final OsFamily osFamily : ALL_FAMILIES) {
197             if (osFamily.getName().equalsIgnoreCase(name)) {
198                 return osFamily;
199             }
200         }
201 
202         return null;
203     }
204 
205     private static boolean versionMatches(final String version) {
206         boolean isVersion = true;
207         if (version != null) {
208             isVersion = version.equalsIgnoreCase(OS_VERSION);
209         }
210         return isVersion;
211     }
212 
213     private static boolean archMatches(final String arch) {
214         boolean isArch = true;
215         if (arch != null) {
216             isArch = arch.equalsIgnoreCase(OS_ARCH);
217         }
218         return isArch;
219     }
220 
221     private static boolean nameMatches(final String name) {
222         boolean isName = true;
223         if (name != null) {
224             isName = name.equalsIgnoreCase(OS_NAME);
225         }
226         return isName;
227     }
228 
229     private static boolean familyMatches(final OsFamily family) {
230         if (family == null) {
231             return false;
232         }
233         for (final OsFamily osFamily : OS_ALL_FAMILIES) {
234             if (family == osFamily) {
235                 return true;
236             }
237         }
238         return false;
239     }
240 
241     private static OsFamily[] determineAllFamilies() {
242         // Determine all families the current OS belongs to
243         final Set<OsFamily> allFamilies = new HashSet<>();
244         if (OS_FAMILY != null) {
245             final List<OsFamily> queue = new ArrayList<>();
246             queue.add(OS_FAMILY);
247             while (!queue.isEmpty()) {
248                 final OsFamily family = queue.remove(0);
249                 allFamilies.add(family);
250                 final OsFamily[] families = family.getFamilies();
251                 Collections.addAll(queue, families);
252             }
253         }
254         return allFamilies.toArray(OsFamily.EMPTY_OS_FAMILY_ARRAY);
255     }
256 
257     private static OsFamily determineOsFamily() {
258         // Determine the most specific OS family
259         if (OS_NAME.contains("windows")) {
260             if (OS_NAME.contains("xp") || OS_NAME.contains("2000") || OS_NAME.contains("nt")) {
261                 return OS_FAMILY_WINNT;
262             }
263             return OS_FAMILY_WIN9X;
264         }
265         if (OS_NAME.contains("os/2")) {
266             return OS_FAMILY_OS2;
267         }
268         if (OS_NAME.contains("netware")) {
269             return OS_FAMILY_NETWARE;
270         }
271         if (OS_NAME.contains("mac")) {
272             if (OS_NAME.endsWith("x")) {
273                 return OS_FAMILY_OSX;
274             }
275             return OS_FAMILY_MAC;
276         }
277         if (PATH_SEP.equals(":")) {
278             return OS_FAMILY_UNIX;
279         }
280         return null;
281     }
282 }