001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.util;
018:
019: import java.util.ArrayList;
020: import java.util.HashSet;
021: import java.util.List;
022: import java.util.Locale;
023: import java.util.Set;
024:
025: /**
026: * Class to help determining the OS.
027: *
028: * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
029: * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
030: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
031: */
032: public final class Os {
033: private static final String OS_NAME = System.getProperty("os.name")
034: .toLowerCase(Locale.US);
035: private static final String OS_ARCH = System.getProperty("os.arch")
036: .toLowerCase(Locale.US);
037: private static final String OS_VERSION = System.getProperty(
038: "os.version").toLowerCase(Locale.US);
039: private static final String PATH_SEP = System
040: .getProperty("path.separator");
041: private static final OsFamily OS_FAMILY;
042: private static final OsFamily[] OS_ALL_FAMILIES;
043:
044: /**
045: * All Windows based OSes.
046: */
047: public static final OsFamily OS_FAMILY_WINDOWS = new OsFamily(
048: "windows");
049:
050: /**
051: * All DOS based OSes.
052: */
053: public static final OsFamily OS_FAMILY_DOS = new OsFamily("dos");
054:
055: /**
056: * All Windows NT based OSes.
057: */
058: public static final OsFamily OS_FAMILY_WINNT = new OsFamily("nt",
059: new OsFamily[] { OS_FAMILY_WINDOWS });
060:
061: /**
062: * All Windows 9x based OSes.
063: */
064: public static final OsFamily OS_FAMILY_WIN9X = new OsFamily(
065: "win9x",
066: new OsFamily[] { OS_FAMILY_WINDOWS, OS_FAMILY_DOS });
067:
068: /**
069: * OS/2
070: */
071: public static final OsFamily OS_FAMILY_OS2 = new OsFamily("os/2",
072: new OsFamily[] { OS_FAMILY_DOS });
073:
074: /**
075: * Netware
076: */
077: public static final OsFamily OS_FAMILY_NETWARE = new OsFamily(
078: "netware");
079:
080: /**
081: * All UNIX based OSes.
082: */
083: public static final OsFamily OS_FAMILY_UNIX = new OsFamily("unix");
084:
085: /**
086: * All Mac based OSes.
087: */
088: public static final OsFamily OS_FAMILY_MAC = new OsFamily("mac");
089:
090: /**
091: * OSX
092: */
093: public static final OsFamily OS_FAMILY_OSX = new OsFamily("osx",
094: new OsFamily[] { OS_FAMILY_UNIX, OS_FAMILY_MAC });
095:
096: private static final OsFamily[] ALL_FAMILIES = new OsFamily[] {
097: OS_FAMILY_DOS, OS_FAMILY_MAC, OS_FAMILY_NETWARE,
098: OS_FAMILY_OS2, OS_FAMILY_OSX, OS_FAMILY_UNIX,
099: OS_FAMILY_WINDOWS, OS_FAMILY_WINNT, OS_FAMILY_WIN9X };
100:
101: static {
102: OS_FAMILY = determineOsFamily();
103: OS_ALL_FAMILIES = determineAllFamilies();
104: }
105:
106: /**
107: * Private constructor to block instantiation.
108: */
109: private Os() {
110: }
111:
112: /**
113: * Determines if the OS on which Ant is executing matches the given OS
114: * version.
115: */
116: public static boolean isVersion(final String version) {
117: return isOs((OsFamily) null, null, null, version);
118: }
119:
120: /**
121: * Determines if the OS on which Ant is executing matches the given OS
122: * architecture.
123: */
124: public static boolean isArch(final String arch) {
125: return isOs((OsFamily) null, null, arch, null);
126: }
127:
128: /**
129: * Determines if the OS on which Ant is executing matches the given OS
130: * family.
131: */
132: public static boolean isFamily(final String family) {
133: return isOs(family, null, null, null);
134: }
135:
136: /**
137: * Determines if the OS on which Ant is executing matches the given OS
138: * family.
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
157: * family, name, architecture and version.
158: *
159: * @param family The OS family
160: * @param name The OS name
161: * @param arch The OS architecture
162: * @param version The OS version
163: * @return The Os value
164: */
165: public static boolean isOs(final String family, final String name,
166: final String arch, final String version) {
167: return isOs(getFamily(family), name, arch, version);
168: }
169:
170: /**
171: * Determines if the OS on which Ant is executing matches the given OS
172: * family, name, architecture and version
173: *
174: * @param family The OS family
175: * @param name The OS name
176: * @param arch The OS architecture
177: * @param version The OS version
178: * @return The Os value
179: */
180: public static boolean isOs(final OsFamily family,
181: final String name, final String arch, final String version) {
182: if (family != null || name != null || arch != null
183: || version != null) {
184: final boolean isFamily = familyMatches(family);
185: final boolean isName = nameMatches(name);
186: final boolean isArch = archMatches(arch);
187: final boolean isVersion = versionMatches(version);
188:
189: return isFamily && isName && isArch && isVersion;
190: } else {
191: return false;
192: }
193: }
194:
195: /**
196: * Locates an OsFamily by name (case-insensitive).
197: *
198: * @return the OS family, or null if not found.
199: */
200: public static OsFamily getFamily(final String name) {
201: for (int i = 0; i < ALL_FAMILIES.length; i++) {
202: final OsFamily osFamily = ALL_FAMILIES[i];
203: if (osFamily.getName().equalsIgnoreCase(name)) {
204: return osFamily;
205: }
206: }
207:
208: return null;
209: }
210:
211: private static boolean versionMatches(final String version) {
212: boolean isVersion = true;
213: if (version != null) {
214: isVersion = version.equalsIgnoreCase(OS_VERSION);
215: }
216: return isVersion;
217: }
218:
219: private static boolean archMatches(final String arch) {
220: boolean isArch = true;
221: if (arch != null) {
222: isArch = arch.equalsIgnoreCase(OS_ARCH);
223: }
224: return isArch;
225: }
226:
227: private static boolean nameMatches(final String name) {
228: boolean isName = true;
229: if (name != null) {
230: isName = name.equalsIgnoreCase(OS_NAME);
231: }
232: return isName;
233: }
234:
235: private static boolean familyMatches(final OsFamily family) {
236: if (family == null) {
237: return false;
238: }
239: for (int i = 0; i < OS_ALL_FAMILIES.length; i++) {
240: final OsFamily osFamily = OS_ALL_FAMILIES[i];
241: if (family == osFamily) {
242: return true;
243: }
244: }
245: return false;
246: }
247:
248: private static OsFamily[] determineAllFamilies() {
249: // Determine all families the current OS belongs to
250: Set allFamilies = new HashSet();
251: if (OS_FAMILY != null) {
252: List queue = new ArrayList();
253: queue.add(OS_FAMILY);
254: while (queue.size() > 0) {
255: final OsFamily family = (OsFamily) queue.remove(0);
256: allFamilies.add(family);
257: final OsFamily[] families = family.getFamilies();
258: for (int i = 0; i < families.length; i++) {
259: OsFamily parent = families[i];
260: queue.add(parent);
261: }
262: }
263: }
264: return (OsFamily[]) allFamilies
265: .toArray(new OsFamily[allFamilies.size()]);
266: }
267:
268: private static OsFamily determineOsFamily() {
269: // Determine the most specific OS family
270: if (OS_NAME.indexOf("windows") > -1) {
271: if (OS_NAME.indexOf("xp") > -1
272: || OS_NAME.indexOf("2000") > -1
273: || OS_NAME.indexOf("nt") > -1) {
274: return OS_FAMILY_WINNT;
275: } else {
276: return OS_FAMILY_WIN9X;
277: }
278: } else if (OS_NAME.indexOf("os/2") > -1) {
279: return OS_FAMILY_OS2;
280: } else if (OS_NAME.indexOf("netware") > -1) {
281: return OS_FAMILY_NETWARE;
282: } else if (OS_NAME.indexOf("mac") > -1) {
283: if (OS_NAME.endsWith("x")) {
284: return OS_FAMILY_OSX;
285: } else {
286: return OS_FAMILY_MAC;
287: }
288: } else if (PATH_SEP.equals(":")) {
289: return OS_FAMILY_UNIX;
290: } else {
291: return null;
292: }
293: }
294: }
|