1 /* 2 * Copyright (c) 1997, 2014, 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 jdk.testlibrary; 27 28 import java.security.PrivilegedAction; 29 import java.util.HashMap; 30 import java.util.Map; 31 import java.io.BufferedReader; 32 import java.io.FileReader; 33 import java.io.InputStreamReader; 34 35 import static jdk.testlibrary.OSInfo.OSType.*; 36 37 /** 38 * @author Pavel Porvatov 39 * copied to testlibrary by yan 40 */ 41 public class OSInfo { 42 public static enum OSType { 43 WINDOWS, 44 LINUX, 45 SOLARIS, 46 MACOSX, 47 UNKNOWN 48 } 49 50 /* 51 The map windowsVersionMap must contain all windows version constants except WINDOWS_UNKNOWN, 52 and so the method getWindowsVersion() will return the constant for known OS. 53 It allows compare objects by "==" instead of "equals". 54 */ 55 public static final WindowsVersion WINDOWS_UNKNOWN = new WindowsVersion(-1, -1); 56 public static final WindowsVersion WINDOWS_95 = new WindowsVersion(4, 0); 57 public static final WindowsVersion WINDOWS_98 = new WindowsVersion(4, 10); 58 public static final WindowsVersion WINDOWS_ME = new WindowsVersion(4, 90); 59 public static final WindowsVersion WINDOWS_2000 = new WindowsVersion(5, 0); 60 public static final WindowsVersion WINDOWS_XP = new WindowsVersion(5, 1); 61 public static final WindowsVersion WINDOWS_2003 = new WindowsVersion(5, 2); 62 public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(6, 0); 63 64 private static final String OS_NAME = "os.name"; 65 private static final String OS_VERSION = "os.version"; 66 67 private final static Map<String, WindowsVersion> windowsVersionMap = new HashMap<String, OSInfo.WindowsVersion>(); 68 69 static { 70 windowsVersionMap.put(WINDOWS_95.toString(), WINDOWS_95); 71 windowsVersionMap.put(WINDOWS_98.toString(), WINDOWS_98); 72 windowsVersionMap.put(WINDOWS_ME.toString(), WINDOWS_ME); 73 windowsVersionMap.put(WINDOWS_2000.toString(), WINDOWS_2000); 74 windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP); 75 windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003); 76 windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA); 77 } 78 79 private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() { 80 public OSType run() { 81 return getOSType(); 82 } 83 }; 84 85 private OSInfo() { 86 // Don't allow to create instances 87 } 88 89 /** 90 * Returns type of operating system. 91 */ 92 public static OSType getOSType() throws SecurityException { 93 String osName = System.getProperty(OS_NAME); 94 95 if (osName != null) { 96 if (osName.contains("Windows")) { 97 return WINDOWS; 98 } 99 100 if (osName.contains("Linux")) { 101 return LINUX; 102 } 103 104 if (osName.contains("Solaris") || osName.contains("SunOS")) { 105 return SOLARIS; 106 } 107 108 if (osName.contains("OS X")) { 109 return MACOSX; 110 } 111 112 // determine another OS here 113 } 114 115 return UNKNOWN; 116 } 117 118 public static PrivilegedAction<OSType> getOSTypeAction() { 119 return osTypeAction; 120 } 121 122 public static WindowsVersion getWindowsVersion() throws SecurityException { 123 String osVersion = System.getProperty(OS_VERSION); 124 125 if (osVersion == null) { 126 return WINDOWS_UNKNOWN; 127 } 128 129 synchronized (windowsVersionMap) { 130 WindowsVersion result = windowsVersionMap.get(osVersion); 131 132 if (result == null) { 133 // Try parse version and put object into windowsVersionMap 134 String[] arr = osVersion.split("\\."); 135 136 if (arr.length == 2) { 137 try { 138 result = new WindowsVersion(Integer.parseInt(arr[0]), Integer.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 double getSolarisVersion() { 154 try { 155 OutputAnalyzer output = ProcessTools.executeProcess("uname", "-v"); 156 System.out.println("'uname -v' finished with code " 157 + output.getExitValue()); 158 return Double.parseDouble(output.getOutput()); 159 } catch (Exception e) { 160 System.out.println("First attempt failed with: " + e.getMessage()); 161 } 162 163 //Try to get Solaris version from /etc/release 164 try (BufferedReader in = 165 new BufferedReader(new FileReader("/etc/release"))) { 166 String line = in.readLine().trim().split(" ")[2]; 167 return Double.parseDouble(line); 168 } catch (Exception e) { 169 System.out.println("Second attempt failed with: " + e.getMessage()); 170 } 171 172 throw new RuntimeException("Unable to get Solaris version"); 173 } 174 175 public static class WindowsVersion implements Comparable<WindowsVersion> { 176 private final int major; 177 178 private final int minor; 179 180 private WindowsVersion(int major, int minor) { 181 this.major = major; 182 this.minor = minor; 183 } 184 185 public int getMajor() { 186 return major; 187 } 188 189 public int getMinor() { 190 return minor; 191 } 192 193 public int compareTo(WindowsVersion o) { 194 int result = major - o.getMajor(); 195 196 if (result == 0) { 197 result = minor - o.getMinor(); 198 } 199 200 return result; 201 } 202 203 public boolean equals(Object obj) { 204 return obj instanceof WindowsVersion && compareTo((WindowsVersion) obj) == 0; 205 } 206 207 public int hashCode() { 208 return 31 * major + minor; 209 } 210 211 public String toString() { 212 return major + "." + minor; 213 } 214 } 215 } 216