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 */
018
019package org.apache.commons.exec;
020
021import java.util.Locale;
022
023/**
024 * Condition that tests the OS type.
025 *
026 * @version $Id: OS.java 1556869 2014-01-09 16:51:11Z britter $
027 */
028public final class OS {
029    private static final String FAMILY_OS_400 = "os/400";
030
031    private static final String FAMILY_Z_OS = "z/os";
032
033    private static final String FAMILY_WIN9X = "win9x";
034
035    private static final String FAMILY_OPENVMS = "openvms";
036
037    private static final String FAMILY_UNIX = "unix";
038
039    private static final String FAMILY_TANDEM = "tandem";
040
041    private static final String FAMILY_MAC = "mac";
042
043    private static final String FAMILY_DOS = "dos";
044
045    private static final String FAMILY_NETWARE = "netware";
046
047    private static final String FAMILY_OS_2 = "os/2";
048
049    private static final String FAMILY_WINDOWS = "windows";
050
051    private static final String OS_NAME = System.getProperty("os.name")
052            .toLowerCase(Locale.US);
053
054    private static final String OS_ARCH = System.getProperty("os.arch")
055            .toLowerCase(Locale.US);
056
057    private static final String OS_VERSION = System.getProperty("os.version")
058            .toLowerCase(Locale.US);
059
060    private static final String PATH_SEP = System.getProperty("path.separator");
061
062    /**
063     * Default constructor
064     */
065    private OS() {
066    }
067
068    /**
069     * Determines if the OS on which Ant is executing matches the given OS
070     * family. * Possible values:<br />
071     * <ul>
072     * <li>dos</li>
073     * <li>mac</li>
074     * <li>netware</li>
075     * <li>os/2</li>
076     * <li>tandem</li>
077     * <li>unix</li>
078     * <li>windows</li>
079     * <li>win9x</li>
080     * <li>z/os</li>
081     * <li>os/400</li>
082     * </ul>
083     * 
084     * @param family
085     *            the family to check for
086     * @return true if the OS matches
087     */
088    private static boolean isFamily(final String family) {
089        return isOs(family, null, null, null);
090    }
091
092    public static boolean isFamilyDOS() {
093        return isFamily(FAMILY_DOS);
094    }
095
096    public static boolean isFamilyMac() {
097        return isFamily(FAMILY_MAC);
098    }
099
100    public static boolean isFamilyNetware() {
101        return isFamily(FAMILY_NETWARE);
102    }
103
104    public static boolean isFamilyOS2() {
105        return isFamily(FAMILY_OS_2);
106    }
107
108    public static boolean isFamilyTandem() {
109        return isFamily(FAMILY_TANDEM);
110    }
111
112    public static boolean isFamilyUnix() {
113        return isFamily(FAMILY_UNIX);
114    }
115
116    public static boolean isFamilyWindows() {
117        return isFamily(FAMILY_WINDOWS);
118    }
119
120    public static boolean isFamilyWin9x() {
121        return isFamily(FAMILY_WIN9X);
122    }
123
124    public static boolean isFamilyZOS() {
125        return isFamily(FAMILY_Z_OS);
126    }
127
128    public static boolean isFamilyOS400() {
129        return isFamily(FAMILY_OS_400);
130    }
131
132    public static boolean isFamilyOpenVms() {
133        return isFamily(FAMILY_OPENVMS);
134    }
135
136    /**
137     * Determines if the OS on which Ant is executing matches the given OS name.
138     * 
139     * @param name
140     *            the OS name to check for
141     * @return true if the OS matches
142     */
143    public static boolean isName(final String name) {
144        return isOs(null, name, null, null);
145    }
146
147    /**
148     * Determines if the OS on which Ant is executing matches the given OS
149     * architecture.
150     * 
151     * @param arch
152     *            the OS architecture to check for
153     * @return true if the OS matches
154     */
155    public static boolean isArch(final String arch) {
156        return isOs(null, null, arch, null);
157    }
158
159    /**
160     * Determines if the OS on which Ant is executing matches the given OS
161     * version.
162     * 
163     * @param version
164     *            the OS version to check for
165     * @return true if the OS matches
166     */
167    public static boolean isVersion(final String version) {
168        return isOs(null, null, null, version);
169    }
170
171    /**
172     * Determines if the OS on which Ant is executing matches the given OS
173     * family, name, architecture and version
174     * 
175     * @param family
176     *            The OS family
177     * @param name
178     *            The OS name
179     * @param arch
180     *            The OS architecture
181     * @param version
182     *            The OS version
183     * @return true if the OS matches
184     */
185    public static boolean isOs(final String family, final String name,
186            final String arch, final String version) {
187        boolean retValue = false;
188
189        if (family != null || name != null || arch != null || version != null) {
190
191            boolean isFamily = true;
192            boolean isName = true;
193            boolean isArch = true;
194            boolean isVersion = true;
195
196            if (family != null) {
197                if (family.equals(FAMILY_WINDOWS)) {
198                    isFamily = OS_NAME.indexOf(FAMILY_WINDOWS) > -1;
199                } else if (family.equals(FAMILY_OS_2)) {
200                    isFamily = OS_NAME.indexOf(FAMILY_OS_2) > -1;
201                } else if (family.equals(FAMILY_NETWARE)) {
202                    isFamily = OS_NAME.indexOf(FAMILY_NETWARE) > -1;
203                } else if (family.equals(FAMILY_DOS)) {
204                    isFamily = PATH_SEP.equals(";")
205                            && !isFamily(FAMILY_NETWARE);
206                } else if (family.equals(FAMILY_MAC)) {
207                    isFamily = OS_NAME.indexOf(FAMILY_MAC) > -1;
208                } else if (family.equals(FAMILY_TANDEM)) {
209                    isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
210                } else if (family.equals(FAMILY_UNIX)) {
211                    isFamily = PATH_SEP.equals(":")
212                            && !isFamily(FAMILY_OPENVMS)
213                            && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x"));
214                } else if (family.equals(FAMILY_WIN9X)) {
215                    isFamily = isFamily(FAMILY_WINDOWS)
216                            && (OS_NAME.indexOf("95") >= 0
217                                    || OS_NAME.indexOf("98") >= 0
218                                    || OS_NAME.indexOf("me") >= 0 || OS_NAME
219                                    .indexOf("ce") >= 0);
220                } else if (family.equals(FAMILY_Z_OS)) {
221                    isFamily = OS_NAME.indexOf(FAMILY_Z_OS) > -1
222                            || OS_NAME.indexOf("os/390") > -1;
223                } else if (family.equals(FAMILY_OS_400)) {
224                    isFamily = OS_NAME.indexOf(FAMILY_OS_400) > -1;
225                } else if (family.equals(FAMILY_OPENVMS)) {
226                    isFamily = OS_NAME.indexOf(FAMILY_OPENVMS) > -1;
227                } else {
228                    throw new IllegalArgumentException(
229                            "Don\'t know how to detect os family \"" + family
230                                    + "\"");
231                }
232            }
233            if (name != null) {
234                isName = name.equals(OS_NAME);
235            }
236            if (arch != null) {
237                isArch = arch.equals(OS_ARCH);
238            }
239            if (version != null) {
240                isVersion = version.equals(OS_VERSION);
241            }
242            retValue = isFamily && isName && isArch && isVersion;
243        }
244        return retValue;
245    }
246}