Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/awt/OSInfo.java
38827 views
/*1* Copyright (c) 1997, 2012, 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,40SOLARIS,41MACOSX,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);5859private static final String OS_NAME = "os.name";60private static final String OS_VERSION = "os.version";6162private final static Map<String, WindowsVersion> windowsVersionMap = new HashMap<String, OSInfo.WindowsVersion>();6364static {65windowsVersionMap.put(WINDOWS_95.toString(), WINDOWS_95);66windowsVersionMap.put(WINDOWS_98.toString(), WINDOWS_98);67windowsVersionMap.put(WINDOWS_ME.toString(), WINDOWS_ME);68windowsVersionMap.put(WINDOWS_2000.toString(), WINDOWS_2000);69windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP);70windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003);71windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA);72}7374private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() {75public OSType run() {76return getOSType();77}78};7980private OSInfo() {81// Don't allow to create instances82}8384/**85* Returns type of operating system.86*/87public static OSType getOSType() throws SecurityException {88String osName = System.getProperty(OS_NAME);8990if (osName != null) {91if (osName.contains("Windows")) {92return WINDOWS;93}9495if (osName.contains("Linux")) {96return LINUX;97}9899if (osName.contains("Solaris") || osName.contains("SunOS")) {100return SOLARIS;101}102103if (osName.contains("OS X")) {104return MACOSX;105}106107// determine another OS here108}109110return UNKNOWN;111}112113public static PrivilegedAction<OSType> getOSTypeAction() {114return osTypeAction;115}116117public static WindowsVersion getWindowsVersion() throws SecurityException {118String osVersion = System.getProperty(OS_VERSION);119120if (osVersion == null) {121return WINDOWS_UNKNOWN;122}123124synchronized (windowsVersionMap) {125WindowsVersion result = windowsVersionMap.get(osVersion);126127if (result == null) {128// Try parse version and put object into windowsVersionMap129String[] arr = osVersion.split("\\.");130131if (arr.length == 2) {132try {133result = new WindowsVersion(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]));134} catch (NumberFormatException e) {135return WINDOWS_UNKNOWN;136}137} else {138return WINDOWS_UNKNOWN;139}140141windowsVersionMap.put(osVersion, result);142}143144return result;145}146}147148public static class WindowsVersion implements Comparable<WindowsVersion> {149private final int major;150151private final int minor;152153private WindowsVersion(int major, int minor) {154this.major = major;155this.minor = minor;156}157158public int getMajor() {159return major;160}161162public int getMinor() {163return minor;164}165166public int compareTo(WindowsVersion o) {167int result = major - o.getMajor();168169if (result == 0) {170result = minor - o.getMinor();171}172173return result;174}175176public boolean equals(Object obj) {177return obj instanceof WindowsVersion && compareTo((WindowsVersion) obj) == 0;178}179180public int hashCode() {181return 31 * major + minor;182}183184public String toString() {185return major + "." + minor;186}187}188}189190191