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