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