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
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
|
1 /*
2 * Copyright (c) 1997, 2020, 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
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 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 @Override
65 public int compareTo(OSVersion o) {
66 return Arrays.compare(this.versionTokens, o.versionTokens);
67 }
68
69 @Override
70 public int hashCode() {
71 return Arrays.hashCode(versionTokens);
72 }
73
74 @Override
75 public boolean equals(Object o) {
76 if (this == o) return true;
77 if (o == null || getClass() != o.getClass()) return false;
78 OSVersion osVersion = (OSVersion) o;
79 return Arrays.equals(versionTokens, osVersion.versionTokens);
80 }
81
|