jdk/src/share/classes/sun/awt/OSInfo.java
author pchelko
Mon, 13 Jan 2014 19:09:24 +0400
changeset 23260 d68cbf54738f
parent 12538 211d6e82fe51
permissions -rw-r--r--
8029893: Remove reflection from DataTransferer.getInstance Reviewed-by: anthony, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 11667
diff changeset
     2
 * Copyright (c) 1997, 2012, 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,
11667
b01021d82350 7122149: [macosx] closed/javax/swing/UITest/UITest.java fails on MacOS
alexsch
parents: 5506
diff changeset
    42
        MACOSX,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        UNKNOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
       The map windowsVersionMap must contain all windows version constants except WINDOWS_UNKNOWN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
       and so the method getWindowsVersion() will return the constant for known OS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
       It allows compare objects by "==" instead of "equals".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final WindowsVersion WINDOWS_UNKNOWN = new WindowsVersion(-1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final WindowsVersion WINDOWS_95 = new WindowsVersion(4, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final WindowsVersion WINDOWS_98 = new WindowsVersion(4, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final WindowsVersion WINDOWS_ME = new WindowsVersion(4, 90);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final WindowsVersion WINDOWS_2000 = new WindowsVersion(5, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final WindowsVersion WINDOWS_XP = new WindowsVersion(5, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final WindowsVersion WINDOWS_2003 = new WindowsVersion(5, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(6, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final String OS_NAME = "os.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final String OS_VERSION = "os.version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static Map<String, WindowsVersion> windowsVersionMap = new HashMap<String, OSInfo.WindowsVersion>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        windowsVersionMap.put(WINDOWS_95.toString(), WINDOWS_95);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        windowsVersionMap.put(WINDOWS_98.toString(), WINDOWS_98);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        windowsVersionMap.put(WINDOWS_ME.toString(), WINDOWS_ME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        windowsVersionMap.put(WINDOWS_2000.toString(), WINDOWS_2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public OSType run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return getOSType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private OSInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Don't allow to create instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns type of operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static OSType getOSType() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        String osName = System.getProperty(OS_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (osName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (osName.contains("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                return WINDOWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (osName.contains("Linux")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return LINUX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (osName.contains("Solaris") || osName.contains("SunOS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                return SOLARIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 11667
diff changeset
   104
            if (osName.contains("OS X")) {
11667
b01021d82350 7122149: [macosx] closed/javax/swing/UITest/UITest.java fails on MacOS
alexsch
parents: 5506
diff changeset
   105
                return MACOSX;
b01021d82350 7122149: [macosx] closed/javax/swing/UITest/UITest.java fails on MacOS
alexsch
parents: 5506
diff changeset
   106
            }
b01021d82350 7122149: [macosx] closed/javax/swing/UITest/UITest.java fails on MacOS
alexsch
parents: 5506
diff changeset
   107
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // determine another OS here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static PrivilegedAction<OSType> getOSTypeAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return osTypeAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static WindowsVersion getWindowsVersion() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String osVersion = System.getProperty(OS_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (osVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return WINDOWS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        synchronized (windowsVersionMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            WindowsVersion result = windowsVersionMap.get(osVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                // Try parse version and put object into windowsVersionMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                String[] arr = osVersion.split("\\.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (arr.length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        result = new WindowsVersion(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        return WINDOWS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    return WINDOWS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                windowsVersionMap.put(osVersion, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public static class WindowsVersion implements Comparable<WindowsVersion> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private final int major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private final int minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private WindowsVersion(int major, int minor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            this.major = major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            this.minor = minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public int getMajor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public int getMinor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        public int compareTo(WindowsVersion o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int result = major - o.getMajor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                result = minor - o.getMinor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return obj instanceof WindowsVersion && compareTo((WindowsVersion) obj) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return 31 * major + minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return major + "." + minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}