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