PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/util/src/com/intellij/openapi/util/SystemInfo.java

https://github.com/chikadance/intellij-community
Java | 233 lines | 167 code | 34 blank | 32 comment | 25 complexity | 145b0c8754619b77684ca0b675e918c9 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, AGPL-1.0
  1. /*
  2. * Copyright 2000-2013 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.util;
  17. import com.intellij.openapi.util.text.StringUtil;
  18. import com.intellij.util.SystemProperties;
  19. import org.jetbrains.annotations.NotNull;
  20. import java.io.File;
  21. import java.util.List;
  22. @SuppressWarnings({"HardCodedStringLiteral", "UtilityClassWithoutPrivateConstructor", "UnusedDeclaration"})
  23. public class SystemInfo extends SystemInfoRt {
  24. public static final String OS_NAME = SystemInfoRt.OS_NAME;
  25. public static final String OS_VERSION = SystemInfoRt.OS_VERSION;
  26. public static final String OS_ARCH = System.getProperty("os.arch");
  27. public static final String JAVA_VERSION = System.getProperty("java.version");
  28. public static final String JAVA_RUNTIME_VERSION = System.getProperty("java.runtime.version");
  29. public static final String ARCH_DATA_MODEL = System.getProperty("sun.arch.data.model");
  30. public static final String SUN_DESKTOP = System.getProperty("sun.desktop", "");
  31. public static final boolean isWindows = SystemInfoRt.isWindows;
  32. public static final boolean isMac = SystemInfoRt.isMac;
  33. public static final boolean isOS2 = SystemInfoRt.isOS2;
  34. public static final boolean isLinux = SystemInfoRt.isLinux;
  35. public static final boolean isFreeBSD = _OS_NAME.startsWith("freebsd");
  36. public static final boolean isSolaris = _OS_NAME.startsWith("sunos");
  37. public static final boolean isUnix = SystemInfoRt.isUnix;
  38. public static final boolean isAppleJvm = isAppleJvm();
  39. public static final boolean isOracleJvm = isOracleJvm();
  40. public static boolean isOsVersionAtLeast(@NotNull String version) {
  41. return StringUtil.compareVersionNumbers(OS_VERSION, version) >= 0;
  42. }
  43. // version numbers from http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx
  44. public static final boolean isWin2kOrNewer = isWindows && isOsVersionAtLeast("5.0");
  45. public static final boolean isWinVistaOrNewer = isWindows && isOsVersionAtLeast("6.0");
  46. public static final boolean isWin7OrNewer = isWindows && isOsVersionAtLeast("6.1");
  47. /** @deprecated unsupported (to remove in IDEA 13) */
  48. public static final boolean isWindows9x = _OS_NAME.startsWith("windows 9") || _OS_NAME.startsWith("windows me");
  49. /** @deprecated unsupported (to remove in IDEA 13) */
  50. public static final boolean isWindowsNT = _OS_NAME.startsWith("windows nt");
  51. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  52. public static final boolean isWindows2000 = _OS_NAME.startsWith("windows 2000");
  53. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  54. public static final boolean isWindows2003 = _OS_NAME.startsWith("windows 2003");
  55. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  56. public static final boolean isWindowsXP = _OS_NAME.startsWith("windows xp");
  57. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  58. public static final boolean isWindowsVista = _OS_NAME.startsWith("windows vista");
  59. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  60. public static final boolean isWindows7 = _OS_NAME.startsWith("windows 7");
  61. /** @deprecated inaccurate (to remove in IDEA 13) */
  62. public static final boolean isKDE = SUN_DESKTOP.toLowerCase().contains("kde");
  63. /** @deprecated inaccurate (to remove in IDEA 13) */
  64. public static final boolean isGnome = SUN_DESKTOP.toLowerCase().contains("gnome");
  65. public static final boolean isXWindow = isUnix && !isMac;
  66. public static final boolean isMacSystemMenu = isMac && "true".equals(System.getProperty("apple.laf.useScreenMenuBar"));
  67. public static final boolean isFileSystemCaseSensitive = SystemInfoRt.isFileSystemCaseSensitive;
  68. public static final boolean areSymLinksSupported = isUnix || isWinVistaOrNewer;
  69. public static final boolean is32Bit = ARCH_DATA_MODEL == null || ARCH_DATA_MODEL.equals("32");
  70. public static final boolean is64Bit = !is32Bit;
  71. public static final boolean isAMD64 = "amd64".equals(OS_ARCH);
  72. public static final boolean isMacIntel64 = isMac && "x86_64".equals(OS_ARCH);
  73. /** @deprecated use {@linkplain #hasXdgOpen()} (to remove in IDEA 13) */
  74. public static final boolean hasXdgOpen = isXWindow;
  75. private static final NotNullLazyValue<Boolean> ourHasXdgOpen = new AtomicNotNullLazyValue<Boolean>() {
  76. @NotNull
  77. @Override
  78. protected Boolean compute() {
  79. return isUnix && new File("/usr/bin/xdg-open").canExecute();
  80. }
  81. };
  82. public static boolean hasXdgOpen() {
  83. return ourHasXdgOpen.getValue();
  84. }
  85. private static final NotNullLazyValue<Boolean> ourHasXdgMime = new AtomicNotNullLazyValue<Boolean>() {
  86. @NotNull
  87. @Override
  88. protected Boolean compute() {
  89. return isUnix && new File("/usr/bin/xdg-mime").canExecute();
  90. }
  91. };
  92. public static boolean hasXdgMime() {
  93. return ourHasXdgOpen.getValue();
  94. }
  95. private static final NotNullLazyValue<Boolean> hasNautilus = new AtomicNotNullLazyValue<Boolean>() {
  96. @NotNull
  97. @Override
  98. protected Boolean compute() {
  99. return isUnix && new File("/usr/bin/nautilus").canExecute();
  100. }
  101. };
  102. /** @deprecated implementation details (to remove in IDEA 13) */
  103. public static boolean hasNautilus() {
  104. return hasNautilus.getValue();
  105. }
  106. /** @deprecated implementation details (to remove in IDEA 13) */
  107. public static final String nativeFileManagerName = "File Manager";
  108. private static final NotNullLazyValue<String> ourFileManagerName = new AtomicNotNullLazyValue<String>() {
  109. @NotNull
  110. @Override
  111. protected String compute() {
  112. return isMac ? "Finder" :
  113. isWindows ? "Explorer" :
  114. "File Manager";
  115. }
  116. };
  117. /** @deprecated implementation details (to remove in IDEA 13) */
  118. public static String getFileManagerName() {
  119. return ourFileManagerName.getValue();
  120. }
  121. /** @deprecated use {@linkplain #isXWindow} (to remove in IDEA 13) */
  122. public static boolean X11PasteEnabledSystem = isXWindow;
  123. /** @deprecated useless (to remove in IDEA 14) */
  124. public static final boolean isIntelMac = isMac && "i386".equals(OS_ARCH);
  125. public static final boolean isMacOSTiger = isMac && isOsVersionAtLeast("10.4");
  126. public static final boolean isMacOSLeopard = isMac && isOsVersionAtLeast("10.5");
  127. public static final boolean isMacOSSnowLeopard = isMac && isOsVersionAtLeast("10.6");
  128. public static final boolean isMacOSLion = isMac && isOsVersionAtLeast("10.7");
  129. public static final boolean isMacOSMountainLion = isMac && isOsVersionAtLeast("10.8");
  130. public static final boolean isMacOSMavericks = isMac && isOsVersionAtLeast("10.9");
  131. @NotNull
  132. public static String getMacOSMajorVersion() {
  133. return getMacOSMajorVersion(OS_VERSION);
  134. }
  135. public static String getMacOSMajorVersion(String version) {
  136. int[] parts = getMacOSVersionParts(version);
  137. return String.format("%d.%d", parts[0], parts[1]);
  138. }
  139. @NotNull
  140. public static String getMacOSVersionCode() {
  141. return getMacOSVersionCode(OS_VERSION);
  142. }
  143. @NotNull
  144. public static String getMacOSMajorVersionCode() {
  145. return getMacOSMajorVersionCode(OS_VERSION);
  146. }
  147. @NotNull
  148. public static String getMacOSMinorVersionCode() {
  149. return getMacOSMinorVersionCode(OS_VERSION);
  150. }
  151. @NotNull
  152. public static String getMacOSVersionCode(@NotNull String version) {
  153. int[] parts = getMacOSVersionParts(version);
  154. return String.format("%02d%d%d", parts[0], normalize(parts[1]), normalize(parts[2]));
  155. }
  156. @NotNull
  157. public static String getMacOSMajorVersionCode(@NotNull String version) {
  158. int[] parts = getMacOSVersionParts(version);
  159. return String.format("%02d%d%d", parts[0], normalize(parts[1]), 0);
  160. }
  161. @NotNull
  162. public static String getMacOSMinorVersionCode(@NotNull String version) {
  163. int[] parts = getMacOSVersionParts(version);
  164. return String.format("%02d%02d", parts[1], parts[2]);
  165. }
  166. private static int[] getMacOSVersionParts(@NotNull String version) {
  167. List<String> parts = StringUtil.split(version, ".");
  168. while (parts.size() < 3) {
  169. parts.add("0");
  170. }
  171. return new int[]{toInt(parts.get(0)), toInt(parts.get(1)), toInt(parts.get(2))};
  172. }
  173. private static int normalize(int number) {
  174. return number > 9 ? 9 : number;
  175. }
  176. private static int toInt(String string) {
  177. try {
  178. return Integer.valueOf(string);
  179. }
  180. catch (NumberFormatException e) {
  181. return 0;
  182. }
  183. }
  184. public static boolean isJavaVersionAtLeast(String v) {
  185. return StringUtil.compareVersionNumbers(JAVA_RUNTIME_VERSION, v) >= 0;
  186. }
  187. /** @deprecated use {@linkplain SystemProperties#getIntProperty(String, int)} (to remove in IDEA 13) */
  188. public static int getIntProperty(@NotNull final String key, final int defaultValue) {
  189. return SystemProperties.getIntProperty(key, defaultValue);
  190. }
  191. private static boolean isOracleJvm() {
  192. final String vendor = SystemProperties.getJavaVmVendor();
  193. return vendor != null && StringUtil.containsIgnoreCase(vendor, "Oracle");
  194. }
  195. private static boolean isAppleJvm() {
  196. final String vendor = SystemProperties.getJavaVmVendor();
  197. return vendor != null && StringUtil.containsIgnoreCase(vendor, "Apple");
  198. }
  199. }