jdk/src/share/classes/sun/awt/OSInfo.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 11667 b01021d82350
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import static sun.awt.OSInfo.OSType.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Pavel Porvatov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class OSInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static enum OSType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        WINDOWS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        LINUX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        SOLARIS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        UNKNOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
       The map windowsVersionMap must contain all windows version constants except WINDOWS_UNKNOWN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
       and so the method getWindowsVersion() will return the constant for known OS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
       It allows compare objects by "==" instead of "equals".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static final WindowsVersion WINDOWS_UNKNOWN = new WindowsVersion(-1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final WindowsVersion WINDOWS_95 = new WindowsVersion(4, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final WindowsVersion WINDOWS_98 = new WindowsVersion(4, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final WindowsVersion WINDOWS_ME = new WindowsVersion(4, 90);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final WindowsVersion WINDOWS_2000 = new WindowsVersion(5, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final WindowsVersion WINDOWS_XP = new WindowsVersion(5, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final WindowsVersion WINDOWS_2003 = new WindowsVersion(5, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(6, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final String OS_NAME = "os.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final String OS_VERSION = "os.version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static Map<String, WindowsVersion> windowsVersionMap = new HashMap<String, OSInfo.WindowsVersion>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        windowsVersionMap.put(WINDOWS_95.toString(), WINDOWS_95);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        windowsVersionMap.put(WINDOWS_98.toString(), WINDOWS_98);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        windowsVersionMap.put(WINDOWS_ME.toString(), WINDOWS_ME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        windowsVersionMap.put(WINDOWS_2000.toString(), WINDOWS_2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public OSType run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return getOSType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private OSInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // Don't allow to create instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Returns type of operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static OSType getOSType() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String osName = System.getProperty(OS_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (osName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (osName.contains("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return WINDOWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (osName.contains("Linux")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return LINUX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (osName.contains("Solaris") || osName.contains("SunOS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return SOLARIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // determine another OS here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static PrivilegedAction<OSType> getOSTypeAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return osTypeAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static WindowsVersion getWindowsVersion() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String osVersion = System.getProperty(OS_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (osVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return WINDOWS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        synchronized (windowsVersionMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            WindowsVersion result = windowsVersionMap.get(osVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // Try parse version and put object into windowsVersionMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                String[] arr = osVersion.split("\\.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (arr.length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        result = new WindowsVersion(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        return WINDOWS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    return WINDOWS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                windowsVersionMap.put(osVersion, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static class WindowsVersion implements Comparable<WindowsVersion> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        private final int major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        private final int minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        private WindowsVersion(int major, int minor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            this.major = major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            this.minor = minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public int getMajor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        public int getMinor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public int compareTo(WindowsVersion o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            int result = major - o.getMajor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                result = minor - o.getMinor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return obj instanceof WindowsVersion && compareTo((WindowsVersion) obj) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return 31 * major + minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return major + "." + minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}