1 /*
   2  * Copyright (c) 1997, 2019, 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 sun.awt;
  27 
  28 import java.security.PrivilegedAction;
  29 import java.util.HashMap;
  30 import java.util.Map;
  31 
  32 import static sun.awt.OSInfo.OSType.*;
  33 
  34 /**
  35  * @author Pavel Porvatov
  36  */
  37 public class OSInfo {
  38     public static enum OSType {
  39         WINDOWS,
  40         LINUX,
  41         SOLARIS,
  42         MACOSX,
  43         AIX,
  44         UNKNOWN
  45     }
  46 
  47     /*
  48        The map windowsVersionMap must contain all windows version constants except WINDOWS_UNKNOWN,
  49        and so the method getWindowsVersion() will return the constant for known OS.
  50        It allows compare objects by "==" instead of "equals".
  51      */
  52     public static final WindowsVersion WINDOWS_UNKNOWN = new WindowsVersion(-1, -1);
  53     public static final WindowsVersion WINDOWS_95 = new WindowsVersion(4, 0);
  54     public static final WindowsVersion WINDOWS_98 = new WindowsVersion(4, 10);
  55     public static final WindowsVersion WINDOWS_ME = new WindowsVersion(4, 90);
  56     public static final WindowsVersion WINDOWS_2000 = new WindowsVersion(5, 0);
  57     public static final WindowsVersion WINDOWS_XP = new WindowsVersion(5, 1);
  58     public static final WindowsVersion WINDOWS_2003 = new WindowsVersion(5, 2);
  59     public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(6, 0);
  60     public static final WindowsVersion WINDOWS_7 = new WindowsVersion(6, 1);
  61 
  62     private static final String OS_NAME = "os.name";
  63     private static final String OS_VERSION = "os.version";
  64 
  65     private static final Map<String, WindowsVersion> windowsVersionMap = new HashMap<String, OSInfo.WindowsVersion>();
  66 
  67     static {
  68         windowsVersionMap.put(WINDOWS_95.toString(), WINDOWS_95);
  69         windowsVersionMap.put(WINDOWS_98.toString(), WINDOWS_98);
  70         windowsVersionMap.put(WINDOWS_ME.toString(), WINDOWS_ME);
  71         windowsVersionMap.put(WINDOWS_2000.toString(), WINDOWS_2000);
  72         windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP);
  73         windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003);
  74         windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA);
  75         windowsVersionMap.put(WINDOWS_7.toString(), WINDOWS_7);
  76     }
  77 
  78     private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() {
  79         public OSType run() {
  80             return getOSType();
  81         }
  82     };
  83 
  84     private OSInfo() {
  85         // Don't allow to create instances
  86     }
  87 
  88     /**
  89      * Returns type of operating system.
  90      */
  91     public static OSType getOSType() throws SecurityException {
  92         String osName = System.getProperty(OS_NAME);
  93 
  94         if (osName != null) {
  95             if (osName.contains("Windows")) {
  96                 return WINDOWS;
  97             }
  98 
  99             if (osName.contains("Linux")) {
 100                 return LINUX;
 101             }
 102 
 103             if (osName.contains("Solaris") || osName.contains("SunOS")) {
 104                 return SOLARIS;
 105             }
 106 
 107             if (osName.contains("OS X")) {
 108                 return MACOSX;
 109             }
 110 
 111             if (osName.contains("AIX")) {
 112                 return AIX;
 113             }
 114 
 115             // determine another OS here
 116         }
 117 
 118         return UNKNOWN;
 119     }
 120 
 121     public static PrivilegedAction<OSType> getOSTypeAction() {
 122         return osTypeAction;
 123     }
 124 
 125     public static WindowsVersion getWindowsVersion() throws SecurityException {
 126         String osVersion = System.getProperty(OS_VERSION);
 127 
 128         if (osVersion == null) {
 129             return WINDOWS_UNKNOWN;
 130         }
 131 
 132         synchronized (windowsVersionMap) {
 133             WindowsVersion result = windowsVersionMap.get(osVersion);
 134 
 135             if (result == null) {
 136                 // Try parse version and put object into windowsVersionMap
 137                 String[] arr = osVersion.split("\\.");
 138 
 139                 if (arr.length == 2) {
 140                     try {
 141                         result = new WindowsVersion(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]));
 142                     } catch (NumberFormatException e) {
 143                         return WINDOWS_UNKNOWN;
 144                     }
 145                 } else {
 146                     return WINDOWS_UNKNOWN;
 147                 }
 148 
 149                 windowsVersionMap.put(osVersion, result);
 150             }
 151 
 152             return result;
 153         }
 154     }
 155 
 156     public static class WindowsVersion implements Comparable<WindowsVersion> {
 157         private final int major;
 158 
 159         private final int minor;
 160 
 161         private WindowsVersion(int major, int minor) {
 162             this.major = major;
 163             this.minor = minor;
 164         }
 165 
 166         public int getMajor() {
 167             return major;
 168         }
 169 
 170         public int getMinor() {
 171             return minor;
 172         }
 173 
 174         public int compareTo(WindowsVersion o) {
 175             int result = major - o.getMajor();
 176 
 177             if (result == 0) {
 178                 result = minor - o.getMinor();
 179             }
 180 
 181             return result;
 182         }
 183 
 184         public boolean equals(Object obj) {
 185             return obj instanceof WindowsVersion && compareTo((WindowsVersion) obj) == 0;
 186         }
 187 
 188         public int hashCode() {
 189             return 31 * major + minor;
 190         }
 191 
 192         public String toString() {
 193             return major + "." + minor;
 194         }
 195     }
 196 }