1 /* 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.test.lib; 27 28 import java.util.Arrays; 29 import java.io.BufferedReader; 30 import java.io.FileReader; 31 import java.util.regex.Pattern; 32 import java.util.stream.Collectors; 33 import java.security.AccessController; 34 import java.security.PrivilegedActionException; 35 import java.security.PrivilegedExceptionAction; 36 37 public final class OSVersion implements Comparable<OSVersion> { 38 public static final OSVersion WINDOWS_95 = new OSVersion(4, 0); 39 public static final OSVersion WINDOWS_98 = new OSVersion(4, 10); 40 public static final OSVersion WINDOWS_ME = new OSVersion(4, 90); 41 public static final OSVersion WINDOWS_2000 = new OSVersion(5, 0); 42 public static final OSVersion WINDOWS_XP = new OSVersion(5, 1); 43 public static final OSVersion WINDOWS_2003 = new OSVersion(5, 2); 44 public static final OSVersion WINDOWS_VISTA = new OSVersion(6, 0); 45 46 private final int[] versionTokens; 47 48 public static OSVersion current() { 49 if (Platform.isSolaris()) { 50 return new OSVersion(getSolarisVersion()); 51 } 52 return new OSVersion(Platform.getOsVersion()); 53 } 54 55 public OSVersion(int major, int minor) { 56 versionTokens = new int[] {major, minor}; 57 } 58 59 public OSVersion(String version) { 60 Pattern onlyDigits = Pattern.compile("^\\d+$"); 61 this.versionTokens = Arrays.stream(version.split("-")[0].split("\\.")) 62 .filter(onlyDigits.asPredicate()) 63 .mapToInt(Integer::parseInt) 64 .toArray(); 65 } 66 67 private static String getSolarisVersion() { 68 try { 69 return Utils.distro(); 70 } catch (Throwable e) { 71 System.out.println("First attempt failed with: " + e.getMessage()); 72 } 73 74 // Try to get Solaris version from /etc/release 75 try (BufferedReader in = new BufferedReader(AccessController.doPrivileged( 76 (PrivilegedExceptionAction<FileReader>) () -> new FileReader("/etc/release")))) { 77 return in.readLine().trim().split(" ")[2]; 78 } catch (PrivilegedActionException e) { 79 System.out.println("Second attempt failed with: " + e.getException().getMessage()); 80 } catch (Exception e) { 81 System.out.println("Second attempt failed with: " + e.getMessage()); 82 } 83 84 throw new RuntimeException("Unable to get Solaris version"); 85 } 86 87 @Override 88 public int compareTo(OSVersion o) { 89 return Arrays.compare(this.versionTokens, o.versionTokens); 90 } 91 92 @Override 93 public int hashCode() { 94 return Arrays.hashCode(versionTokens); 95 } 96 97 @Override 98 public boolean equals(Object o) { 99 if (this == o) return true; 100 if (o == null || getClass() != o.getClass()) return false; 101 OSVersion osVersion = (OSVersion) o; 102 return Arrays.equals(versionTokens, osVersion.versionTokens); 103 } 104 105 @Override 106 public String toString() { 107 return Arrays.stream(versionTokens) 108 .mapToObj(String::valueOf) 109 .collect(Collectors.joining(".")); 110 } 111 } 112