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