Path: blob/master/src/java.desktop/share/classes/sun/awt/OSInfo.java
41152 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt;2627import java.security.PrivilegedAction;28import java.util.HashMap;29import java.util.Map;3031import static sun.awt.OSInfo.OSType.*;3233/**34* @author Pavel Porvatov35*/36public class OSInfo {37public static enum OSType {38WINDOWS,39LINUX,40MACOSX,41AIX,42UNKNOWN43}4445/*46The map windowsVersionMap must contain all windows version constants except WINDOWS_UNKNOWN,47and so the method getWindowsVersion() will return the constant for known OS.48It allows compare objects by "==" instead of "equals".49*/50public static final WindowsVersion WINDOWS_UNKNOWN = new WindowsVersion(-1, -1);51public static final WindowsVersion WINDOWS_95 = new WindowsVersion(4, 0);52public static final WindowsVersion WINDOWS_98 = new WindowsVersion(4, 10);53public static final WindowsVersion WINDOWS_ME = new WindowsVersion(4, 90);54public static final WindowsVersion WINDOWS_2000 = new WindowsVersion(5, 0);55public static final WindowsVersion WINDOWS_XP = new WindowsVersion(5, 1);56public static final WindowsVersion WINDOWS_2003 = new WindowsVersion(5, 2);57public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(6, 0);58public static final WindowsVersion WINDOWS_7 = new WindowsVersion(6, 1);5960private static final String OS_NAME = "os.name";61private static final String OS_VERSION = "os.version";6263private static final Map<String, WindowsVersion> windowsVersionMap = new HashMap<String, OSInfo.WindowsVersion>();6465static {66windowsVersionMap.put(WINDOWS_95.toString(), WINDOWS_95);67windowsVersionMap.put(WINDOWS_98.toString(), WINDOWS_98);68windowsVersionMap.put(WINDOWS_ME.toString(), WINDOWS_ME);69windowsVersionMap.put(WINDOWS_2000.toString(), WINDOWS_2000);70windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP);71windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003);72windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA);73windowsVersionMap.put(WINDOWS_7.toString(), WINDOWS_7);74}7576private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() {77public OSType run() {78return getOSType();79}80};8182private OSInfo() {83// Don't allow to create instances84}8586/**87* Returns type of operating system.88*/89public static OSType getOSType() throws SecurityException {90String osName = System.getProperty(OS_NAME);9192if (osName != null) {93if (osName.contains("Windows")) {94return WINDOWS;95}9697if (osName.contains("Linux")) {98return LINUX;99}100101if (osName.contains("OS X")) {102return MACOSX;103}104105if (osName.contains("AIX")) {106return AIX;107}108109// determine another OS here110}111112return UNKNOWN;113}114115public static PrivilegedAction<OSType> getOSTypeAction() {116return osTypeAction;117}118119public static WindowsVersion getWindowsVersion() throws SecurityException {120String osVersion = System.getProperty(OS_VERSION);121122if (osVersion == null) {123return WINDOWS_UNKNOWN;124}125126synchronized (windowsVersionMap) {127WindowsVersion result = windowsVersionMap.get(osVersion);128129if (result == null) {130// Try parse version and put object into windowsVersionMap131String[] arr = osVersion.split("\\.");132133if (arr.length == 2) {134try {135result = new WindowsVersion(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]));136} catch (NumberFormatException e) {137return WINDOWS_UNKNOWN;138}139} else {140return WINDOWS_UNKNOWN;141}142143windowsVersionMap.put(osVersion, result);144}145146return result;147}148}149150public static class WindowsVersion implements Comparable<WindowsVersion> {151private final int major;152153private final int minor;154155private WindowsVersion(int major, int minor) {156this.major = major;157this.minor = minor;158}159160public int getMajor() {161return major;162}163164public int getMinor() {165return minor;166}167168public int compareTo(WindowsVersion o) {169int result = major - o.getMajor();170171if (result == 0) {172result = minor - o.getMinor();173}174175return result;176}177178public boolean equals(Object obj) {179return obj instanceof WindowsVersion && compareTo((WindowsVersion) obj) == 0;180}181182public int hashCode() {183return 31 * major + minor;184}185186public String toString() {187return major + "." + minor;188}189}190}191192193