1 16 package org.apache.commons.vfs.util; 17 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Locale ; 22 import java.util.Set ; 23 24 31 public final class Os 32 { 33 private static final String OS_NAME = 34 System.getProperty("os.name").toLowerCase(Locale.US); 35 private static final String OS_ARCH = 36 System.getProperty("os.arch").toLowerCase(Locale.US); 37 private static final String OS_VERSION = 38 System.getProperty("os.version").toLowerCase(Locale.US); 39 private static final String PATH_SEP = 40 System.getProperty("path.separator"); 41 private static final OsFamily OS_FAMILY; 42 private static final OsFamily[] OS_ALL_FAMILIES; 43 44 47 public static final OsFamily OS_FAMILY_WINDOWS = new OsFamily("windows"); 48 49 52 public static final OsFamily OS_FAMILY_DOS = new OsFamily("dos"); 53 54 57 public static final OsFamily OS_FAMILY_WINNT = 58 new OsFamily("nt", new OsFamily[]{OS_FAMILY_WINDOWS}); 59 60 63 public static final OsFamily OS_FAMILY_WIN9X = 64 new OsFamily("win9x", new OsFamily[]{OS_FAMILY_WINDOWS, OS_FAMILY_DOS}); 65 66 69 public static final OsFamily OS_FAMILY_OS2 = 70 new OsFamily("os/2", new OsFamily[]{OS_FAMILY_DOS}); 71 72 75 public static final OsFamily OS_FAMILY_NETWARE = 76 new OsFamily("netware"); 77 78 81 public static final OsFamily OS_FAMILY_UNIX = new OsFamily("unix"); 82 83 86 public static final OsFamily OS_FAMILY_MAC = new OsFamily("mac"); 87 88 91 public static final OsFamily OS_FAMILY_OSX = 92 new OsFamily("osx", new OsFamily[]{OS_FAMILY_UNIX, OS_FAMILY_MAC}); 93 94 private static final OsFamily[] ALL_FAMILIES = new OsFamily[] 95 { 96 OS_FAMILY_DOS, 97 OS_FAMILY_MAC, 98 OS_FAMILY_NETWARE, 99 OS_FAMILY_OS2, 100 OS_FAMILY_OSX, 101 OS_FAMILY_UNIX, 102 OS_FAMILY_WINDOWS, 103 OS_FAMILY_WINNT, 104 OS_FAMILY_WIN9X 105 }; 106 107 static 108 { 109 OS_FAMILY = determineOsFamily(); 110 OS_ALL_FAMILIES = determineAllFamilies(); 111 } 112 113 116 private Os() 117 { 118 } 119 120 124 public static boolean isVersion(final String version) 125 { 126 return isOs((OsFamily) null, null, null, version); 127 } 128 129 133 public static boolean isArch(final String arch) 134 { 135 return isOs((OsFamily) null, null, arch, null); 136 } 137 138 142 public static boolean isFamily(final String family) 143 { 144 return isOs(family, null, null, null); 145 } 146 147 151 public static boolean isFamily(final OsFamily family) 152 { 153 return isOs(family, null, null, null); 154 } 155 156 163 public static boolean isName(final String name) 164 { 165 return isOs((OsFamily) null, name, null, null); 166 } 167 168 178 public static boolean isOs(final String family, 179 final String name, 180 final String arch, 181 final String version) 182 { 183 return isOs(getFamily(family), name, arch, version); 184 } 185 186 196 public static boolean isOs(final OsFamily family, 197 final String name, 198 final String arch, 199 final String version) 200 { 201 if (family != null || name != null || arch != null || version != null) 202 { 203 final boolean isFamily = familyMatches(family); 204 final boolean isName = nameMatches(name); 205 final boolean isArch = archMatches(arch); 206 final boolean isVersion = versionMatches(version); 207 208 return isFamily && isName && isArch && isVersion; 209 } 210 else 211 { 212 return false; 213 } 214 } 215 216 221 public static OsFamily getFamily(final String name) 222 { 223 for (int i = 0; i < ALL_FAMILIES.length; i++) 224 { 225 final OsFamily osFamily = ALL_FAMILIES[i]; 226 if (osFamily.getName().equalsIgnoreCase(name)) 227 { 228 return osFamily; 229 } 230 } 231 232 return null; 233 } 234 235 private static boolean versionMatches(final String version) 236 { 237 boolean isVersion = true; 238 if (version != null) 239 { 240 isVersion = version.equalsIgnoreCase(OS_VERSION); 241 } 242 return isVersion; 243 } 244 245 private static boolean archMatches(final String arch) 246 { 247 boolean isArch = true; 248 if (arch != null) 249 { 250 isArch = arch.equalsIgnoreCase(OS_ARCH); 251 } 252 return isArch; 253 } 254 255 private static boolean nameMatches(final String name) 256 { 257 boolean isName = true; 258 if (name != null) 259 { 260 isName = name.equalsIgnoreCase(OS_NAME); 261 } 262 return isName; 263 } 264 265 private static boolean familyMatches(final OsFamily family) 266 { 267 if (family == null) 268 { 269 return false; 270 } 271 for (int i = 0; i < OS_ALL_FAMILIES.length; i++) 272 { 273 final OsFamily osFamily = OS_ALL_FAMILIES[i]; 274 if (family == osFamily) 275 { 276 return true; 277 } 278 } 279 return false; 280 } 281 282 private static OsFamily[] determineAllFamilies() 283 { 284 Set allFamilies = new HashSet (); 286 if (OS_FAMILY != null) 287 { 288 List queue = new ArrayList (); 289 queue.add(OS_FAMILY); 290 while (queue.size() > 0) 291 { 292 final OsFamily family = (OsFamily) queue.remove(0); 293 allFamilies.add(family); 294 final OsFamily[] families = family.getFamilies(); 295 for (int i = 0; i < families.length; i++) 296 { 297 OsFamily parent = families[i]; 298 queue.add(parent); 299 } 300 } 301 } 302 return (OsFamily[]) allFamilies.toArray(new OsFamily[allFamilies.size()]); 303 } 304 305 private static OsFamily determineOsFamily() 306 { 307 if (OS_NAME.indexOf("windows") > -1) 309 { 310 if (OS_NAME.indexOf("xp") > -1 311 || OS_NAME.indexOf("2000") > -1 312 || OS_NAME.indexOf("nt") > -1) 313 { 314 return OS_FAMILY_WINNT; 315 } 316 else 317 { 318 return OS_FAMILY_WIN9X; 319 } 320 } 321 else if (OS_NAME.indexOf("os/2") > -1) 322 { 323 return OS_FAMILY_OS2; 324 } 325 else if (OS_NAME.indexOf("netware") > -1) 326 { 327 return OS_FAMILY_NETWARE; 328 } 329 else if (OS_NAME.indexOf("mac") > -1) 330 { 331 if (OS_NAME.endsWith("x")) 332 { 333 return OS_FAMILY_OSX; 334 } 335 else 336 { 337 return OS_FAMILY_MAC; 338 } 339 } 340 else if (PATH_SEP.equals(":")) 341 { 342 return OS_FAMILY_UNIX; 343 } 344 else 345 { 346 return null; 347 } 348 } 349 } 350 | Popular Tags |