KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > 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  */

18
19 package org.apache.tools.ant.taskdefs.condition;
20
21 import java.util.Locale JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24
25 /**
26  * Condition that tests the OS type.
27  *
28  * @since Ant 1.4
29  */

30 public class Os implements Condition {
31     private static final String JavaDoc OS_NAME =
32         System.getProperty("os.name").toLowerCase(Locale.US);
33     private static final String JavaDoc OS_ARCH =
34         System.getProperty("os.arch").toLowerCase(Locale.US);
35     private static final String JavaDoc OS_VERSION =
36         System.getProperty("os.version").toLowerCase(Locale.US);
37     private static final String JavaDoc PATH_SEP =
38         System.getProperty("path.separator");
39
40     /**
41      * OS family to look for
42      */

43     private String JavaDoc family;
44     /**
45      * Name of OS
46      */

47     private String JavaDoc name;
48     /**
49      * version of OS
50      */

51     private String JavaDoc version;
52     /**
53      * OS architecture
54      */

55     private String JavaDoc arch;
56     /**
57      * OS family that can be tested for. {@value}
58      */

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

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

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

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

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

79     public static final String JavaDoc FAMILY_DOS = "dos";
80     /**
81      * OS family that can be tested for. {@value}
82      */

83     public static final String JavaDoc FAMILY_MAC = "mac";
84     /**
85      * OS family that can be tested for. {@value}
86      */

87     public static final String JavaDoc FAMILY_TANDEM = "tandem";
88     /**
89      * OS family that can be tested for. {@value}
90      */

91     public static final String JavaDoc FAMILY_UNIX = "unix";
92     /**
93      * OS family that can be tested for. {@value}
94      */

95     public static final String JavaDoc FAMILY_VMS = "openvms";
96     /**
97      * OS family that can be tested for. {@value}
98      */

99     public static final String JavaDoc FAMILY_ZOS = "z/os";
100     /** OS family that can be tested for. {@value} */
101     public static final String JavaDoc FAMILY_OS400 = "os/400";
102
103     /**
104      * Default constructor
105      *
106      */

107     public Os() {
108         //default
109
}
110
111     /**
112      * Constructor that sets the family attribute
113      * @param family a String value
114      */

115     public Os(String JavaDoc family) {
116         setFamily(family);
117     }
118
119     /**
120      * Sets the desired OS family type
121      *
122      * @param f The OS family type desired<br />
123      * Possible values:<br />
124      * <ul>
125      * <li>dos</li>
126      * <li>mac</li>
127      * <li>netware</li>
128      * <li>os/2</li>
129      * <li>tandem</li>
130      * <li>unix</li>
131      * <li>windows</li>
132      * <li>win9x</li>
133      * <li>z/os</li>
134      * <li>os/400</li>
135      * </ul>
136      */

137     public void setFamily(String JavaDoc f) {
138         family = f.toLowerCase(Locale.US);
139     }
140
141     /**
142      * Sets the desired OS name
143      *
144      * @param name The OS name
145      */

146     public void setName(String JavaDoc name) {
147         this.name = name.toLowerCase(Locale.US);
148     }
149
150     /**
151      * Sets the desired OS architecture
152      *
153      * @param arch The OS architecture
154      */

155     public void setArch(String JavaDoc arch) {
156         this.arch = arch.toLowerCase(Locale.US);
157     }
158
159     /**
160      * Sets the desired OS version
161      *
162      * @param version The OS version
163      */

164     public void setVersion(String JavaDoc version) {
165         this.version = version.toLowerCase(Locale.US);
166     }
167
168     /**
169      * Determines if the OS on which Ant is executing matches the type of
170      * that set in setFamily.
171      * @return true if the os matches.
172      * @throws BuildException if there is an error.
173      * @see Os#setFamily(String)
174      */

175     public boolean eval() throws BuildException {
176         return isOs(family, name, arch, version);
177     }
178
179     /**
180      * Determines if the OS on which Ant is executing matches the
181      * given OS family.
182      * @param family the family to check for
183      * @return true if the OS matches
184      * @since 1.5
185      */

186     public static boolean isFamily(String JavaDoc family) {
187         return isOs(family, null, null, null);
188     }
189
190     /**
191      * Determines if the OS on which Ant is executing matches the
192      * given OS name.
193      *
194      * @param name the OS name to check for
195      * @return true if the OS matches
196      * @since 1.7
197      */

198     public static boolean isName(String JavaDoc name) {
199         return isOs(null, name, null, null);
200     }
201
202     /**
203      * Determines if the OS on which Ant is executing matches the
204      * given OS architecture.
205      *
206      * @param arch the OS architecture to check for
207      * @return true if the OS matches
208      * @since 1.7
209      */

210     public static boolean isArch(String JavaDoc arch) {
211         return isOs(null, null, arch, null);
212     }
213
214     /**
215      * Determines if the OS on which Ant is executing matches the
216      * given OS version.
217      *
218      * @param version the OS version to check for
219      * @return true if the OS matches
220      * @since 1.7
221      */

222     public static boolean isVersion(String JavaDoc version) {
223         return isOs(null, null, null, version);
224     }
225
226     /**
227      * Determines if the OS on which Ant is executing matches the
228      * given OS family, name, architecture and version
229      *
230      * @param family The OS family
231      * @param name The OS name
232      * @param arch The OS architecture
233      * @param version The OS version
234      * @return true if the OS matches
235      * @since 1.7
236      */

237     public static boolean isOs(String JavaDoc family, String JavaDoc name, String JavaDoc arch,
238                                String JavaDoc version) {
239         boolean retValue = false;
240
241         if (family != null || name != null || arch != null
242             || version != null) {
243
244             boolean isFamily = true;
245             boolean isName = true;
246             boolean isArch = true;
247             boolean isVersion = true;
248
249             if (family != null) {
250
251                 //windows probing logic relies on the word 'windows' in
252
//the OS
253
boolean isWindows = OS_NAME.indexOf(FAMILY_WINDOWS) > -1;
254                 boolean is9x = false;
255                 boolean isNT = false;
256                 if (isWindows) {
257                     //there are only four 9x platforms that we look for
258
is9x = (OS_NAME.indexOf("95") >= 0
259                             || OS_NAME.indexOf("98") >= 0
260                             || OS_NAME.indexOf("me") >= 0
261                             //wince isn't really 9x, but crippled enough to
262
//be a muchness. Ant doesnt run on CE, anyway.
263
|| OS_NAME.indexOf("ce") >= 0);
264                     isNT = !is9x;
265                 }
266                 if (family.equals(FAMILY_WINDOWS)) {
267                     isFamily = isWindows;
268                 } else if (family.equals(FAMILY_9X)) {
269                     isFamily = isWindows && is9x;
270                 } else if (family.equals(FAMILY_NT)) {
271                     isFamily = isWindows && isNT;
272                 } else if (family.equals(FAMILY_OS2)) {
273                     isFamily = OS_NAME.indexOf(FAMILY_OS2) > -1;
274                 } else if (family.equals(FAMILY_NETWARE)) {
275                     isFamily = OS_NAME.indexOf(FAMILY_NETWARE) > -1;
276                 } else if (family.equals(FAMILY_DOS)) {
277                     isFamily = PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE);
278                 } else if (family.equals(FAMILY_MAC)) {
279                     isFamily = OS_NAME.indexOf(FAMILY_MAC) > -1;
280                 } else if (family.equals(FAMILY_TANDEM)) {
281                     isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
282                 } else if (family.equals(FAMILY_UNIX)) {
283                     isFamily = PATH_SEP.equals(":")
284                         && !isFamily(FAMILY_VMS)
285                         && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x"));
286                 } else if (family.equals(FAMILY_ZOS)) {
287                     isFamily = OS_NAME.indexOf(FAMILY_ZOS) > -1
288                         || OS_NAME.indexOf("os/390") > -1;
289                 } else if (family.equals(FAMILY_OS400)) {
290                     isFamily = OS_NAME.indexOf(FAMILY_OS400) > -1;
291                 } else if (family.equals(FAMILY_VMS)) {
292                     isFamily = OS_NAME.indexOf(FAMILY_VMS) > -1;
293                 } else {
294                     throw new BuildException(
295                         "Don\'t know how to detect os family \""
296                         + family + "\"");
297                 }
298             }
299             if (name != null) {
300                 isName = name.equals(OS_NAME);
301             }
302             if (arch != null) {
303                 isArch = arch.equals(OS_ARCH);
304             }
305             if (version != null) {
306                 isVersion = version.equals(OS_VERSION);
307             }
308             retValue = isFamily && isName && isArch && isVersion;
309         }
310         return retValue;
311     }
312 }
313
Popular Tags