KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > Os


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.geronimo.kernel.config;
18
19 import java.util.Locale JavaDoc;
20
21 /**
22  * Condition that tests the OS type.
23  *
24  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
25  */

26 public class Os {
27     private static final String JavaDoc OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
28     private static final String JavaDoc OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.US);
29     private static final String JavaDoc OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.US);
30     private static final String JavaDoc PATH_SEP = System.getProperty("path.separator");
31
32     /**
33      * OS family that can be tested for. {@value}
34      */

35     public static final String JavaDoc FAMILY_WINDOWS = "windows";
36     /**
37      * OS family that can be tested for. {@value}
38      */

39     public static final String JavaDoc FAMILY_9X = "win9x";
40     /**
41      * OS family that can be tested for. {@value}
42      */

43     public static final String JavaDoc FAMILY_NT = "winnt";
44     /**
45      * OS family that can be tested for. {@value}
46      */

47     public static final String JavaDoc FAMILY_OS2 = "os/2";
48     /**
49      * OS family that can be tested for. {@value}
50      */

51     public static final String JavaDoc FAMILY_NETWARE = "netware";
52     /**
53      * OS family that can be tested for. {@value}
54      */

55     public static final String JavaDoc FAMILY_DOS = "dos";
56     /**
57      * OS family that can be tested for. {@value}
58      */

59     public static final String JavaDoc FAMILY_MAC = "mac";
60     /**
61      * OS family that can be tested for. {@value}
62      */

63     public static final String JavaDoc FAMILY_TANDEM = "tandem";
64     /**
65      * OS family that can be tested for. {@value}
66      */

67     public static final String JavaDoc FAMILY_UNIX = "unix";
68     /**
69      * OS family that can be tested for. {@value}
70      */

71     public static final String JavaDoc FAMILY_VMS = "openvms";
72     /**
73      * OS family that can be tested for. {@value}
74      */

75     public static final String JavaDoc FAMILY_ZOS = "z/os";
76     /**
77      * OS family that can be tested for. {@value}
78      */

79     public static final String JavaDoc FAMILY_OS400 = "os/400";
80
81     private Os() {
82         //default
83
}
84
85     /**
86      * Determines if the OS on which Ant is executing matches the
87      * given OS family.
88      * @param family the family to check for
89      * @return true if the OS matches
90      * @since 1.5
91      */

92     public static boolean isFamily(String JavaDoc family) {
93         return isOs(family, null, null, null);
94     }
95
96     /**
97      * Determines if the OS on which Ant is executing matches the
98      * given OS name.
99      *
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc family, String JavaDoc name, String JavaDoc arch,
157                                String JavaDoc 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 JavaDoc(
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 }
Popular Tags