001: /*
002: * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.security.PrivilegedAction;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import static sun.awt.OSInfo.OSType.*;
033:
034: /**
035: * @author Pavel Porvatov
036: * @version 1.2 06/15/07
037: */
038: public class OSInfo {
039: public static enum OSType {
040: WINDOWS, LINUX, SOLARIS, UNKNOWN
041: }
042:
043: /*
044: The map windowsVersionMap must contain all windows version constants except WINDOWS_UNKNOWN,
045: and so the method getWindowsVersion() will return the constant for known OS.
046: It allows compare objects by "==" instead of "equals".
047: */
048: public static final WindowsVersion WINDOWS_UNKNOWN = new WindowsVersion(
049: -1, -1);
050: public static final WindowsVersion WINDOWS_95 = new WindowsVersion(
051: 4, 0);
052: public static final WindowsVersion WINDOWS_98 = new WindowsVersion(
053: 4, 10);
054: public static final WindowsVersion WINDOWS_ME = new WindowsVersion(
055: 4, 90);
056: public static final WindowsVersion WINDOWS_2000 = new WindowsVersion(
057: 5, 0);
058: public static final WindowsVersion WINDOWS_XP = new WindowsVersion(
059: 5, 1);
060: public static final WindowsVersion WINDOWS_2003 = new WindowsVersion(
061: 5, 2);
062: public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(
063: 6, 0);
064:
065: private static final String OS_NAME = "os.name";
066: private static final String OS_VERSION = "os.version";
067:
068: private final static Map<String, WindowsVersion> windowsVersionMap = new HashMap<String, OSInfo.WindowsVersion>();
069:
070: static {
071: windowsVersionMap.put(WINDOWS_95.toString(), WINDOWS_95);
072: windowsVersionMap.put(WINDOWS_98.toString(), WINDOWS_98);
073: windowsVersionMap.put(WINDOWS_ME.toString(), WINDOWS_ME);
074: windowsVersionMap.put(WINDOWS_2000.toString(), WINDOWS_2000);
075: windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP);
076: windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003);
077: windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA);
078: }
079:
080: private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() {
081: public OSType run() {
082: return getOSType();
083: }
084: };
085:
086: private OSInfo() {
087: // Don't allow to create instances
088: }
089:
090: /**
091: * Returns type of operating system.
092: */
093: public static OSType getOSType() throws SecurityException {
094: String osName = System.getProperty(OS_NAME);
095:
096: if (osName != null) {
097: if (osName.contains("Windows")) {
098: return WINDOWS;
099: }
100:
101: if (osName.contains("Linux")) {
102: return LINUX;
103: }
104:
105: if (osName.contains("Solaris") || osName.contains("SunOS")) {
106: return SOLARIS;
107: }
108:
109: // determine another OS here
110: }
111:
112: return UNKNOWN;
113: }
114:
115: public static PrivilegedAction<OSType> getOSTypeAction() {
116: return osTypeAction;
117: }
118:
119: public static WindowsVersion getWindowsVersion()
120: 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
137: .parseInt(arr[0]), Integer
138: .parseInt(arr[1]));
139: } catch (NumberFormatException e) {
140: return WINDOWS_UNKNOWN;
141: }
142: } else {
143: return WINDOWS_UNKNOWN;
144: }
145:
146: windowsVersionMap.put(osVersion, result);
147: }
148:
149: return result;
150: }
151: }
152:
153: public static class WindowsVersion implements
154: Comparable<WindowsVersion> {
155: private final int major;
156:
157: private final int minor;
158:
159: private WindowsVersion(int major, int minor) {
160: this .major = major;
161: this .minor = minor;
162: }
163:
164: public int getMajor() {
165: return major;
166: }
167:
168: public int getMinor() {
169: return minor;
170: }
171:
172: public int compareTo(WindowsVersion o) {
173: int result = major - o.getMajor();
174:
175: if (result == 0) {
176: result = minor - o.getMinor();
177: }
178:
179: return result;
180: }
181:
182: public boolean equals(Object obj) {
183: return obj instanceof WindowsVersion
184: && compareTo((WindowsVersion) obj) == 0;
185: }
186:
187: public int hashCode() {
188: return 31 * major + minor;
189: }
190:
191: public String toString() {
192: return major + "." + minor;
193: }
194: }
195: }
|