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.geronimo.kernel.config;
018    
019    import java.util.Locale;
020    
021    /**
022     * Condition that tests the OS type.
023     *
024     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
025     */
026    public class Os {
027        private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
028        private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.US);
029        private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.US);
030        private static final String PATH_SEP = System.getProperty("path.separator");
031    
032        /**
033         * OS family that can be tested for. {@value}
034         */
035        public static final String FAMILY_WINDOWS = "windows";
036        /**
037         * OS family that can be tested for. {@value}
038         */
039        public static final String FAMILY_9X = "win9x";
040        /**
041         * OS family that can be tested for. {@value}
042         */
043        public static final String FAMILY_NT = "winnt";
044        /**
045         * OS family that can be tested for. {@value}
046         */
047        public static final String FAMILY_OS2 = "os/2";
048        /**
049         * OS family that can be tested for. {@value}
050         */
051        public static final String FAMILY_NETWARE = "netware";
052        /**
053         * OS family that can be tested for. {@value}
054         */
055        public static final String FAMILY_DOS = "dos";
056        /**
057         * OS family that can be tested for. {@value}
058         */
059        public static final String FAMILY_MAC = "mac";
060        /**
061         * OS family that can be tested for. {@value}
062         */
063        public static final String FAMILY_TANDEM = "tandem";
064        /**
065         * OS family that can be tested for. {@value}
066         */
067        public static final String FAMILY_UNIX = "unix";
068        /**
069         * OS family that can be tested for. {@value}
070         */
071        public static final String FAMILY_VMS = "openvms";
072        /**
073         * OS family that can be tested for. {@value}
074         */
075        public static final String FAMILY_ZOS = "z/os";
076        /**
077         * OS family that can be tested for. {@value}
078         */
079        public static final String FAMILY_OS400 = "os/400";
080    
081        private Os() {
082            //default
083        }
084    
085        /**
086         * Determines if the OS on which Ant is executing matches the
087         * given OS family.
088         * @param family the family to check for
089         * @return true if the OS matches
090         * @since 1.5
091         */
092        public static boolean isFamily(String family) {
093            return isOs(family, null, null, null);
094        }
095    
096        /**
097         * Determines if the OS on which Ant is executing matches the
098         * given OS name.
099         *
100         * @param name  The OS family type desired<br />
101         *               Possible values:<br />
102         *               <ul>
103         *               <li>dos</li>
104         *               <li>mac</li>
105         *               <li>netware</li>
106         *               <li>os/2</li>
107         *               <li>tandem</li>
108         *               <li>unix</li>
109         *               <li>windows</li>
110         *               <li>win9x</li>
111         *               <li>z/os</li>
112         *               <li>os/400</li>
113         *               </ul>
114         * @return true if the OS matches
115         * @since 1.7
116         */
117        public static boolean isName(String name) {
118            return isOs(null, name, null, null);
119        }
120    
121        /**
122         * Determines if the OS on which Ant is executing matches the
123         * given OS architecture.
124         *
125         * @param arch the OS architecture to check for
126         * @return true if the OS matches
127         * @since 1.7
128         */
129        public static boolean isArch(String arch) {
130            return isOs(null, null, arch, null);
131        }
132    
133        /**
134         * Determines if the OS on which Ant is executing matches the
135         * given OS version.
136         *
137         * @param version the OS version to check for
138         * @return true if the OS matches
139         * @since 1.7
140         */
141        public static boolean isVersion(String version) {
142            return isOs(null, null, null, version);
143        }
144    
145        /**
146         * Determines if the OS on which Ant is executing matches the
147         * given OS family, name, architecture and version
148         *
149         * @param family   The OS family
150         * @param name   The OS name
151         * @param arch   The OS architecture
152         * @param version   The OS version
153         * @return true if the OS matches
154         * @since 1.7
155         */
156        public static boolean isOs(String family, String name, String arch,
157                                   String version) {
158            boolean retValue = false;
159    
160            if (family != null || name != null || arch != null
161                || version != null) {
162    
163                boolean isFamily = true;
164                boolean isName = true;
165                boolean isArch = true;
166                boolean isVersion = true;
167    
168                if (family != null) {
169    
170                    //windows probing logic relies on the word 'windows' in
171                    //the OS
172                    boolean isWindows = OS_NAME.indexOf(FAMILY_WINDOWS) > -1;
173                    boolean is9x = false;
174                    boolean isNT = false;
175                    if(isWindows) {
176                        //there are only four 9x platforms that we look for
177                        is9x = (OS_NAME.indexOf("95") >= 0
178                                || OS_NAME.indexOf("98") >= 0
179                                || OS_NAME.indexOf("me") >= 0
180                                //wince isn't really 9x, but crippled enough to
181                                //be a muchness. Ant doesnt run on CE, anyway.
182                                || OS_NAME.indexOf("ce") >= 0);
183                        isNT = !is9x;
184                    }
185                    if (family.equals(FAMILY_WINDOWS)) {
186                        isFamily = isWindows;
187                    } else if (family.equals(FAMILY_9X)) {
188                        isFamily = isWindows && is9x;
189                    } else if (family.equals(FAMILY_NT)) {
190                        isFamily = isWindows && isNT;
191                    } else if (family.equals(FAMILY_OS2)) {
192                        isFamily = OS_NAME.indexOf(FAMILY_OS2) > -1;
193                    } else if (family.equals(FAMILY_NETWARE)) {
194                        isFamily = OS_NAME.indexOf(FAMILY_NETWARE) > -1;
195                    } else if (family.equals(FAMILY_DOS)) {
196                        isFamily = PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE);
197                    } else if (family.equals(FAMILY_MAC)) {
198                        isFamily = OS_NAME.indexOf(FAMILY_MAC) > -1;
199                    } else if (family.equals(FAMILY_TANDEM)) {
200                        isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
201                    } else if (family.equals(FAMILY_UNIX)) {
202                        isFamily = PATH_SEP.equals(":")
203                            && !isFamily(FAMILY_VMS)
204                            && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x"));
205                    } else if (family.equals(FAMILY_ZOS)) {
206                        isFamily = OS_NAME.indexOf(FAMILY_ZOS) > -1
207                            || OS_NAME.indexOf("os/390") > -1;
208                    } else if (family.equals(FAMILY_OS400)) {
209                        isFamily = OS_NAME.indexOf(FAMILY_OS400) > -1;
210                    } else if (family.equals(FAMILY_VMS)) {
211                        isFamily = OS_NAME.indexOf(FAMILY_VMS) > -1;
212                    } else {
213                        throw new IllegalArgumentException(
214                            "Don\'t know how to detect os family \""
215                            + family + "\"");
216                    }
217                }
218                if (name != null) {
219                    isName = name.equals(OS_NAME);
220                }
221                if (arch != null) {
222                    isArch = arch.equals(OS_ARCH);
223                }
224                if (version != null) {
225                    isVersion = version.equals(OS_VERSION);
226                }
227                retValue = isFamily && isName && isArch && isVersion;
228            }
229            return retValue;
230        }
231    }