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;
|
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;
|