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