1 /* 2 * Copyright (c) 1997, 2014, 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 34 public final class OSVersion implements Comparable<OSVersion> { 35 public static final OSVersion WINDOWS_95 = new OSVersion(4, 0); 36 public static final OSVersion WINDOWS_98 = new OSVersion(4, 10); 37 public static final OSVersion WINDOWS_ME = new OSVersion(4, 90); 38 public static final OSVersion WINDOWS_2000 = new OSVersion(5, 0); 39 public static final OSVersion WINDOWS_XP = new OSVersion(5, 1); 40 public static final OSVersion WINDOWS_2003 = new OSVersion(5, 2); 41 public static final OSVersion WINDOWS_VISTA = new OSVersion(6, 0); 42 43 private final int[] versionTokens; 44 45 public static OSVersion current() { 46 if (Platform.isSolaris()) { 47 return new OSVersion(getSolarisVersion()); 48 } 49 return new OSVersion(Platform.getOsVersion()); 50 } 51 52 public OSVersion(int major, int minor) { 53 versionTokens = new int[] {major, minor}; 54 } 55 56 public OSVersion(String version) { 57 Pattern onlyDigits = Pattern.compile("^\\d+$"); 58 this.versionTokens = Arrays.stream(version.split("-")[0].split("\\.")) 59 .filter(onlyDigits.asPredicate()) 60 .mapToInt(Integer::parseInt) 61 .toArray(); 62 } 63 64 private static String getSolarisVersion() { 65 try { 66 return Utils.distro(); 67 } catch (Throwable e) { 68 System.out.println("First attempt failed with: " + e.getMessage()); 69 } 70 71 // Try to get Solaris version from /etc/release 72 try (BufferedReader in = 73 new BufferedReader(new FileReader("/etc/release"))) { 74 return in.readLine().trim().split(" ")[2]; 75 } catch (Exception e) { 76 System.out.println("Second attempt failed with: " + e.getMessage()); 77 } 78 79 throw new RuntimeException("Unable to get Solaris version"); 80 } 81 82 @Override 83 public int compareTo(OSVersion o) { 84 return Arrays.compare(this.versionTokens, o.versionTokens); 85 } 86 87 @Override 88 public int hashCode() { 89 return Arrays.hashCode(versionTokens); 90 } 91 92 @Override 93 public boolean equals(Object o) { 94 if (this == o) return true; 95 if (o == null || getClass() != o.getClass()) return false; 96 OSVersion osVersion = (OSVersion) o; 97 return Arrays.equals(versionTokens, osVersion.versionTokens); 98 } 99 100 @Override 101 public String toString() { 102 return Arrays.stream(versionTokens) 103 .mapToObj(String::valueOf) 104 .collect(Collectors.joining(".")); 105 } 106 } 107