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.tools.ant.taskdefs.condition;
020:
021: import java.util.Locale;
022:
023: import org.apache.tools.ant.BuildException;
024:
025: /**
026: * Condition that tests the OS type.
027: *
028: * @since Ant 1.4
029: */
030: public class Os implements Condition {
031: private static final String OS_NAME = System.getProperty("os.name")
032: .toLowerCase(Locale.US);
033: private static final String OS_ARCH = System.getProperty("os.arch")
034: .toLowerCase(Locale.US);
035: private static final String OS_VERSION = System.getProperty(
036: "os.version").toLowerCase(Locale.US);
037: private static final String PATH_SEP = System
038: .getProperty("path.separator");
039:
040: /**
041: * OS family to look for
042: */
043: private String family;
044: /**
045: * Name of OS
046: */
047: private String name;
048: /**
049: * version of OS
050: */
051: private String version;
052: /**
053: * OS architecture
054: */
055: private String arch;
056: /**
057: * OS family that can be tested for. {@value}
058: */
059: public static final String FAMILY_WINDOWS = "windows";
060: /**
061: * OS family that can be tested for. {@value}
062: */
063: public static final String FAMILY_9X = "win9x";
064: /**
065: * OS family that can be tested for. {@value}
066: */
067: public static final String FAMILY_NT = "winnt";
068: /**
069: * OS family that can be tested for. {@value}
070: */
071: public static final String FAMILY_OS2 = "os/2";
072: /**
073: * OS family that can be tested for. {@value}
074: */
075: public static final String FAMILY_NETWARE = "netware";
076: /**
077: * OS family that can be tested for. {@value}
078: */
079: public static final String FAMILY_DOS = "dos";
080: /**
081: * OS family that can be tested for. {@value}
082: */
083: public static final String FAMILY_MAC = "mac";
084: /**
085: * OS family that can be tested for. {@value}
086: */
087: public static final String FAMILY_TANDEM = "tandem";
088: /**
089: * OS family that can be tested for. {@value}
090: */
091: public static final String FAMILY_UNIX = "unix";
092: /**
093: * OS family that can be tested for. {@value}
094: */
095: public static final String FAMILY_VMS = "openvms";
096: /**
097: * OS family that can be tested for. {@value}
098: */
099: public static final String FAMILY_ZOS = "z/os";
100: /** OS family that can be tested for. {@value} */
101: public static final String 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 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 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 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 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 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 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 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 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 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 family, String name, String arch,
238: String 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(";")
278: && !isFamily(FAMILY_NETWARE);
279: } else if (family.equals(FAMILY_MAC)) {
280: isFamily = OS_NAME.indexOf(FAMILY_MAC) > -1;
281: } else if (family.equals(FAMILY_TANDEM)) {
282: isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
283: } else if (family.equals(FAMILY_UNIX)) {
284: isFamily = PATH_SEP.equals(":")
285: && !isFamily(FAMILY_VMS)
286: && (!isFamily(FAMILY_MAC) || OS_NAME
287: .endsWith("x"));
288: } else if (family.equals(FAMILY_ZOS)) {
289: isFamily = OS_NAME.indexOf(FAMILY_ZOS) > -1
290: || OS_NAME.indexOf("os/390") > -1;
291: } else if (family.equals(FAMILY_OS400)) {
292: isFamily = OS_NAME.indexOf(FAMILY_OS400) > -1;
293: } else if (family.equals(FAMILY_VMS)) {
294: isFamily = OS_NAME.indexOf(FAMILY_VMS) > -1;
295: } else {
296: throw new BuildException(
297: "Don\'t know how to detect os family \""
298: + family + "\"");
299: }
300: }
301: if (name != null) {
302: isName = name.equals(OS_NAME);
303: }
304: if (arch != null) {
305: isArch = arch.equals(OS_ARCH);
306: }
307: if (version != null) {
308: isVersion = version.equals(OS_VERSION);
309: }
310: retValue = isFamily && isName && isArch && isVersion;
311: }
312: return retValue;
313: }
314: }
|