1 18 19 package org.apache.tools.ant.taskdefs.condition; 20 21 import java.util.Locale ; 22 23 import org.apache.tools.ant.BuildException; 24 25 30 public class Os implements Condition { 31 private static final String OS_NAME = 32 System.getProperty("os.name").toLowerCase(Locale.US); 33 private static final String OS_ARCH = 34 System.getProperty("os.arch").toLowerCase(Locale.US); 35 private static final String OS_VERSION = 36 System.getProperty("os.version").toLowerCase(Locale.US); 37 private static final String PATH_SEP = 38 System.getProperty("path.separator"); 39 40 43 private String family; 44 47 private String name; 48 51 private String version; 52 55 private String arch; 56 59 public static final String FAMILY_WINDOWS = "windows"; 60 63 public static final String FAMILY_9X = "win9x"; 64 67 public static final String FAMILY_NT = "winnt"; 68 71 public static final String FAMILY_OS2 = "os/2"; 72 75 public static final String FAMILY_NETWARE = "netware"; 76 79 public static final String FAMILY_DOS = "dos"; 80 83 public static final String FAMILY_MAC = "mac"; 84 87 public static final String FAMILY_TANDEM = "tandem"; 88 91 public static final String FAMILY_UNIX = "unix"; 92 95 public static final String FAMILY_VMS = "openvms"; 96 99 public static final String FAMILY_ZOS = "z/os"; 100 101 public static final String FAMILY_OS400 = "os/400"; 102 103 107 public Os() { 108 } 110 111 115 public Os(String family) { 116 setFamily(family); 117 } 118 119 137 public void setFamily(String f) { 138 family = f.toLowerCase(Locale.US); 139 } 140 141 146 public void setName(String name) { 147 this.name = name.toLowerCase(Locale.US); 148 } 149 150 155 public void setArch(String arch) { 156 this.arch = arch.toLowerCase(Locale.US); 157 } 158 159 164 public void setVersion(String version) { 165 this.version = version.toLowerCase(Locale.US); 166 } 167 168 175 public boolean eval() throws BuildException { 176 return isOs(family, name, arch, version); 177 } 178 179 186 public static boolean isFamily(String family) { 187 return isOs(family, null, null, null); 188 } 189 190 198 public static boolean isName(String name) { 199 return isOs(null, name, null, null); 200 } 201 202 210 public static boolean isArch(String arch) { 211 return isOs(null, null, arch, null); 212 } 213 214 222 public static boolean isVersion(String version) { 223 return isOs(null, null, null, version); 224 } 225 226 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 boolean isWindows = OS_NAME.indexOf(FAMILY_WINDOWS) > -1; 254 boolean is9x = false; 255 boolean isNT = false; 256 if (isWindows) { 257 is9x = (OS_NAME.indexOf("95") >= 0 259 || OS_NAME.indexOf("98") >= 0 260 || OS_NAME.indexOf("me") >= 0 261 || 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 |