001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.commons.exec; 021 022import java.io.File; 023import java.util.Locale; 024 025/** 026 * Condition that tests the OS type. 027 * <p> 028 * Copied and adapted from Apache Ant 1.9.6 from {@code org.apache.tools.ant.taskdefs.condition.OS}. 029 * </p> 030 */ 031public final class OS { 032 033 /** 034 * OS family name to test: {@value} 035 */ 036 public static final String FAMILY_9X = "win9x"; 037 038 /** 039 * OS family name to test: {@value} 040 */ 041 public static final String FAMILY_DOS = "dos"; 042 043 /** 044 * OS family name to test: {@value} 045 */ 046 public static final String FAMILY_MAC = "mac"; 047 048 /** 049 * OS family name to test: {@value} 050 */ 051 public static final String FAMILY_NETWARE = "netware"; 052 053 /** 054 * OS family name to test: {@value} 055 */ 056 public static final String FAMILY_NT = "winnt"; 057 058 /** 059 * OS family name to test: {@value} 060 */ 061 public static final String FAMILY_OS2 = "os/2"; 062 063 /** 064 * OS family name to test: {@value} 065 */ 066 public static final String FAMILY_OS400 = "os/400"; 067 068 /** 069 * OS family name to test: {@value} 070 */ 071 public static final String FAMILY_TANDEM = "tandem"; 072 073 /** 074 * OS family name to test: {@value} 075 */ 076 public static final String FAMILY_UNIX = "unix"; 077 078 /** 079 * OS family name to test: {@value} 080 */ 081 public static final String FAMILY_VMS = "openvms"; 082 083 /** 084 * OS family name to test: {@value} 085 */ 086 public static final String FAMILY_WINDOWS = "windows"; 087 088 /** 089 * OS family name to test: {@value} 090 */ 091 public static final String FAMILY_ZOS = "z/os"; 092 093 /** 094 * Apple Darwin string: {@value} 095 */ 096 private static final String DARWIN = "darwin"; 097 098 /** 099 * The OS name. 100 */ 101 private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ROOT); 102 103 /** 104 * The OS architecture. 105 */ 106 private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ROOT); 107 108 /** 109 * The OS version. 110 */ 111 private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ROOT); 112 113 /** 114 * Tests whether the OS on which commons-exec is executing matches the given OS architecture. 115 * 116 * @param arch the OS architecture to check for. 117 * @return whether if the OS matches. 118 */ 119 public static boolean isArch(final String arch) { 120 return isOs(null, null, arch, null); 121 } 122 123 /** 124 * Tests whether the OS on which commons-exec is executing matches the given OS family. 125 * 126 * @param family the family to check for. 127 * @return whether if the OS matches. 128 */ 129 private static boolean isFamily(final String family) { 130 return isOs(family, null, null, null); 131 } 132 133 /** 134 * Tests whether the OS is in the DOS family. 135 * 136 * @return whether the OS is in the DOS family. 137 */ 138 public static boolean isFamilyDOS() { 139 return isFamily(FAMILY_DOS); 140 } 141 142 /** 143 * Tests whether the OS is in the Mac family. 144 * 145 * @return whether the OS is in the Mac family. 146 */ 147 public static boolean isFamilyMac() { 148 return isFamily(FAMILY_MAC); 149 } 150 151 /** 152 * Tests whether the OS is in the Netware family. 153 * 154 * @return whether the OS is in the Netware family. 155 */ 156 public static boolean isFamilyNetware() { 157 return isFamily(FAMILY_NETWARE); 158 } 159 160 /** 161 * Tests whether the OS is in the OpenVMS family. 162 * 163 * @return whether the OS is in the OpenVMS family. 164 */ 165 public static boolean isFamilyOpenVms() { 166 return isFamily(FAMILY_VMS); 167 } 168 169 /** 170 * Tests whether the OS is in the OS/2 family. 171 * 172 * @return whether the OS is in the OS/2 family. 173 */ 174 public static boolean isFamilyOS2() { 175 return isFamily(FAMILY_OS2); 176 } 177 178 /** 179 * Tests whether the OS is in the OS/400 family. 180 * 181 * @return whether the OS is in the OS/400 family. 182 */ 183 public static boolean isFamilyOS400() { 184 return isFamily(FAMILY_OS400); 185 } 186 187 /** 188 * Tests whether the OS is in the Tandem family. 189 * 190 * @return whether the OS is in the Tandem family. 191 */ 192 public static boolean isFamilyTandem() { 193 return isFamily(FAMILY_TANDEM); 194 } 195 196 /** 197 * Tests whether the OS is in the Unix family. 198 * 199 * @return whether the OS is in the Unix family. 200 */ 201 public static boolean isFamilyUnix() { 202 return isFamily(FAMILY_UNIX); 203 } 204 205 /** 206 * Tests whether the OS is in the Windows 9x family. 207 * 208 * @return whether the OS is in the Windows 9x family. 209 */ 210 public static boolean isFamilyWin9x() { 211 return isFamily(FAMILY_9X); 212 } 213 214 /** 215 * Tests whether the OS is in the Windows family. 216 * 217 * @return whether the OS is in the Windows family. 218 */ 219 public static boolean isFamilyWindows() { 220 return isFamily(FAMILY_WINDOWS); 221 } 222 223 /** 224 * Tests whether the OS is in the Windows NT family. 225 * 226 * @return whether the OS is in the Windows NT family. 227 */ 228 public static boolean isFamilyWinNT() { 229 return isFamily(FAMILY_NT); 230 } 231 232 /** 233 * Tests whether the OS is in the z/OS family. 234 * 235 * @return whether the OS is in the z/OS family. 236 */ 237 public static boolean isFamilyZOS() { 238 return isFamily(FAMILY_ZOS); 239 } 240 241 /** 242 * Tests whether if the OS on which commons-exec is executing matches the given OS name. 243 * 244 * @param name the OS name to check for. 245 * @return whether the OS matches. 246 */ 247 public static boolean isName(final String name) { 248 return isOs(null, name, null, null); 249 } 250 251 /** 252 * Tests whether the OS on which commons-exec is executing matches the given OS family, name, architecture and version. 253 * 254 * @param family The OS family. 255 * @param name The OS name. 256 * @param arch The OS architecture. 257 * @param version The OS version. 258 * @return whether the OS matches. 259 */ 260 public static boolean isOs(final String family, final String name, final String arch, final String version) { 261 boolean retValue = false; 262 if (family != null || name != null || arch != null || version != null) { 263 boolean isFamily = true; 264 boolean isName = true; 265 boolean isArch = true; 266 boolean isVersion = true; 267 if (family != null) { 268 // Windows probing logic relies on the word 'windows' in the OS 269 final boolean isWindows = OS_NAME.contains(FAMILY_WINDOWS); 270 boolean is9x = false; 271 boolean isNT = false; 272 if (isWindows) { 273 // there are only four 9x platforms that we look for 274 is9x = OS_NAME.contains("95") || OS_NAME.contains("98") || OS_NAME.contains("me") 275 // Windows CE isn't really 9x, but crippled enough to 276 // be a muchness. Ant doesn't run on CE, anyway. 277 || OS_NAME.contains("ce"); 278 isNT = !is9x; 279 } 280 switch (family) { 281 case FAMILY_WINDOWS: 282 isFamily = isWindows; 283 break; 284 case FAMILY_9X: 285 isFamily = isWindows && is9x; 286 break; 287 case FAMILY_NT: 288 isFamily = isWindows && isNT; 289 break; 290 case FAMILY_OS2: 291 isFamily = OS_NAME.contains(FAMILY_OS2); 292 break; 293 case FAMILY_NETWARE: 294 isFamily = OS_NAME.contains(FAMILY_NETWARE); 295 break; 296 case FAMILY_DOS: 297 isFamily = File.pathSeparator.equals(";") && !isFamily(FAMILY_NETWARE); 298 break; 299 case FAMILY_MAC: 300 isFamily = OS_NAME.contains(FAMILY_MAC) || OS_NAME.contains(DARWIN); 301 break; 302 case FAMILY_TANDEM: 303 isFamily = OS_NAME.contains("nonstop_kernel"); 304 break; 305 case FAMILY_UNIX: 306 isFamily = File.pathSeparator.equals(":") && !isFamily(FAMILY_VMS) 307 && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x") || OS_NAME.contains(DARWIN)); 308 break; 309 case FAMILY_ZOS: 310 isFamily = OS_NAME.contains(FAMILY_ZOS) || OS_NAME.contains("os/390"); 311 break; 312 case FAMILY_OS400: 313 isFamily = OS_NAME.contains(FAMILY_OS400); 314 break; 315 case FAMILY_VMS: 316 isFamily = OS_NAME.contains(FAMILY_VMS); 317 break; 318 default: 319 throw new IllegalArgumentException("Don\'t know how to detect OS family \"" + family + "\""); 320 } 321 } 322 if (name != null) { 323 isName = name.equals(OS_NAME); 324 } 325 if (arch != null) { 326 isArch = arch.equals(OS_ARCH); 327 } 328 if (version != null) { 329 isVersion = version.equals(OS_VERSION); 330 } 331 retValue = isFamily && isName && isArch && isVersion; 332 } 333 return retValue; 334 } 335 336 /** 337 * Tests whether the OS on which commons-exec is executing matches the given OS version. 338 * 339 * @param version the OS version to check for. 340 * @return whether if the OS matches. 341 */ 342 public static boolean isVersion(final String version) { 343 return isOs(null, null, null, version); 344 } 345 346 /** 347 * Avoids instances. 348 */ 349 private OS() { 350 } 351}