PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/rfreedman/intellij-community
Java | 280 lines | 178 code | 41 blank | 61 comment | 23 complexity | bf647267139cf2335ba1e2d7f058bcc0 MD5 | raw file
Possible License(s): Apache-2.0, 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 boolean isOsVersionAtLeast(@NotNull String version) {
  39. return StringUtil.compareVersionNumbers(OS_VERSION, version) >= 0;
  40. }
  41. // version numbers from http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx
  42. public static final boolean isWin2kOrNewer = isWindows && isOsVersionAtLeast("5.0");
  43. public static final boolean isWinVistaOrNewer = isWindows && isOsVersionAtLeast("6.0");
  44. public static final boolean isWin7OrNewer = isWindows && isOsVersionAtLeast("6.1");
  45. /** @deprecated unsupported (to remove in IDEA 13) */
  46. public static final boolean isWindows9x = _OS_NAME.startsWith("windows 9") || _OS_NAME.startsWith("windows me");
  47. /** @deprecated unsupported (to remove in IDEA 13) */
  48. public static final boolean isWindowsNT = _OS_NAME.startsWith("windows nt");
  49. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  50. public static final boolean isWindows2000 = _OS_NAME.startsWith("windows 2000");
  51. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  52. public static final boolean isWindows2003 = _OS_NAME.startsWith("windows 2003");
  53. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  54. public static final boolean isWindowsXP = _OS_NAME.startsWith("windows xp");
  55. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  56. public static final boolean isWindowsVista = _OS_NAME.startsWith("windows vista");
  57. /** @deprecated use {@linkplain #OS_VERSION} (to remove in IDEA 13) */
  58. public static final boolean isWindows7 = _OS_NAME.startsWith("windows 7");
  59. /** @deprecated inaccurate (to remove in IDEA 13) */
  60. public static final boolean isKDE = SUN_DESKTOP.toLowerCase().contains("kde");
  61. /** @deprecated inaccurate (to remove in IDEA 13) */
  62. public static final boolean isGnome = SUN_DESKTOP.toLowerCase().contains("gnome");
  63. public static final boolean isXWindow = isUnix && !isMac;
  64. public static final boolean isMacSystemMenu = isMac && "true".equals(System.getProperty("apple.laf.useScreenMenuBar"));
  65. public static final boolean isFileSystemCaseSensitive = SystemInfoRt.isFileSystemCaseSensitive;
  66. public static final boolean areSymLinksSupported = isUnix || isWinVistaOrNewer;
  67. public static final boolean is32Bit = ARCH_DATA_MODEL == null || ARCH_DATA_MODEL.equals("32");
  68. public static final boolean is64Bit = !is32Bit;
  69. public static final boolean isAMD64 = "amd64".equals(OS_ARCH);
  70. public static final boolean isMacIntel64 = isMac && "x86_64".equals(OS_ARCH);
  71. /** @deprecated use {@linkplain #hasXdgOpen()} (to remove in IDEA 13) */
  72. public static final boolean hasXdgOpen = isXWindow;
  73. private static final NotNullLazyValue<Boolean> ourHasXdgOpen = new AtomicNotNullLazyValue<Boolean>() {
  74. @NotNull
  75. @Override
  76. protected Boolean compute() {
  77. return isUnix && new File("/usr/bin/xdg-open").canExecute();
  78. }
  79. };
  80. public static boolean hasXdgOpen() {
  81. return ourHasXdgOpen.getValue();
  82. }
  83. private static final NotNullLazyValue<Boolean> ourHasXdgMime = new AtomicNotNullLazyValue<Boolean>() {
  84. @NotNull
  85. @Override
  86. protected Boolean compute() {
  87. return isUnix && new File("/usr/bin/xdg-mime").canExecute();
  88. }
  89. };
  90. public static boolean hasXdgMime() {
  91. return ourHasXdgOpen.getValue();
  92. }
  93. private static final NotNullLazyValue<Boolean> hasNautilus = new AtomicNotNullLazyValue<Boolean>() {
  94. @NotNull
  95. @Override
  96. protected Boolean compute() {
  97. return isUnix && new File("/usr/bin/nautilus").canExecute();
  98. }
  99. };
  100. /** @deprecated implementation details (to remove in IDEA 13) */
  101. public static boolean hasNautilus() {
  102. return hasNautilus.getValue();
  103. }
  104. /** @deprecated implementation details (to remove in IDEA 13) */
  105. public static final String nativeFileManagerName = "File Manager";
  106. private static final NotNullLazyValue<String> ourFileManagerName = new AtomicNotNullLazyValue<String>() {
  107. @NotNull
  108. @Override
  109. protected String compute() {
  110. return isMac ? "Finder" :
  111. isWindows ? "Explorer" :
  112. "File Manager";
  113. }
  114. };
  115. /** @deprecated implementation details (to remove in IDEA 13) */
  116. public static String getFileManagerName() {
  117. return ourFileManagerName.getValue();
  118. }
  119. /**
  120. * Whether IDEA is running under MacOS X version 10.4 or later.
  121. *
  122. * @since 5.0.2
  123. */
  124. public static final boolean isMacOSTiger = isTiger();
  125. /**
  126. * Whether IDEA is running under MacOS X on an Intel Machine
  127. *
  128. * @since 5.0.2
  129. */
  130. public static final boolean isIntelMac = isIntelMac();
  131. /**
  132. * Running under MacOS X version 10.5 or later;
  133. *
  134. * @since 7.0.2
  135. */
  136. public static final boolean isMacOSLeopard = isLeopard();
  137. /**
  138. * Running under MacOS X version 10.6 or later;
  139. *
  140. * @since 9.0
  141. */
  142. public static final boolean isMacOSSnowLeopard = isSnowLeopard();
  143. /**
  144. * Running under MacOS X version 10.7 or later;
  145. *
  146. * @since 11.0
  147. */
  148. public static final boolean isMacOSLion = isLion();
  149. /**
  150. * Running under MacOS X version 10.8 or later;
  151. *
  152. * @since 11.1
  153. */
  154. public static final boolean isMacOSMountainLion = isMountainLion();
  155. /** @deprecated use {@linkplain #isXWindow} (to remove in IDEA 13) */
  156. public static boolean X11PasteEnabledSystem = isXWindow;
  157. private static boolean isIntelMac() {
  158. return isMac && "i386".equals(OS_ARCH);
  159. }
  160. private static boolean isTiger() {
  161. return isMac &&
  162. !OS_VERSION.startsWith("10.0") &&
  163. !OS_VERSION.startsWith("10.1") &&
  164. !OS_VERSION.startsWith("10.2") &&
  165. !OS_VERSION.startsWith("10.3");
  166. }
  167. private static boolean isLeopard() {
  168. return isMac && isTiger() && !OS_VERSION.startsWith("10.4");
  169. }
  170. private static boolean isSnowLeopard() {
  171. return isMac && isLeopard() && !OS_VERSION.startsWith("10.5");
  172. }
  173. private static boolean isLion() {
  174. return isMac && isSnowLeopard() && !OS_VERSION.startsWith("10.6");
  175. }
  176. private static boolean isMountainLion() {
  177. return isMac && isLion() && !OS_VERSION.startsWith("10.7");
  178. }
  179. @NotNull
  180. public static String getMacOSMajorVersion() {
  181. return getMacOSMajorVersion(OS_VERSION);
  182. }
  183. public static String getMacOSMajorVersion(String version) {
  184. int[] parts = getMacOSVersionParts(version);
  185. return String.format("%d.%d", parts[0], parts[1]);
  186. }
  187. @NotNull
  188. public static String getMacOSVersionCode() {
  189. return getMacOSVersionCode(OS_VERSION);
  190. }
  191. @NotNull
  192. public static String getMacOSMajorVersionCode() {
  193. return getMacOSMajorVersionCode(OS_VERSION);
  194. }
  195. @NotNull
  196. public static String getMacOSMinorVersionCode() {
  197. return getMacOSMinorVersionCode(OS_VERSION);
  198. }
  199. @NotNull
  200. public static String getMacOSVersionCode(@NotNull String version) {
  201. int[] parts = getMacOSVersionParts(version);
  202. return String.format("%02d%d%d", parts[0], normalize(parts[1]), normalize(parts[2]));
  203. }
  204. @NotNull
  205. public static String getMacOSMajorVersionCode(@NotNull String version) {
  206. int[] parts = getMacOSVersionParts(version);
  207. return String.format("%02d%d%d", parts[0], normalize(parts[1]), 0);
  208. }
  209. @NotNull
  210. public static String getMacOSMinorVersionCode(@NotNull String version) {
  211. int[] parts = getMacOSVersionParts(version);
  212. return String.format("%02d%02d", parts[1], parts[2]);
  213. }
  214. private static int[] getMacOSVersionParts(@NotNull String version) {
  215. List<String> parts = StringUtil.split(version, ".");
  216. while (parts.size() < 3) {
  217. parts.add("0");
  218. }
  219. return new int[]{toInt(parts.get(0)), toInt(parts.get(1)), toInt(parts.get(2))};
  220. }
  221. private static int normalize(int number) {
  222. return number > 9 ? 9 : number;
  223. }
  224. private static int toInt(String string) {
  225. try {
  226. return Integer.valueOf(string);
  227. }
  228. catch (NumberFormatException e) {
  229. return 0;
  230. }
  231. }
  232. public static boolean isJavaVersionAtLeast(String v) {
  233. return StringUtil.compareVersionNumbers(JAVA_RUNTIME_VERSION, v) >= 0;
  234. }
  235. /** @deprecated use {@linkplain SystemProperties#getIntProperty(String, int)} (to remove in IDEA 13) */
  236. public static int getIntProperty(@NotNull final String key, final int defaultValue) {
  237. return SystemProperties.getIntProperty(key, defaultValue);
  238. }
  239. }