PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/java/org/apache/zookeeper/inspector/ui/utils/OSUtils.java

https://bitbucket.org/meverett/zookeeper-swing-ui
Java | 377 lines | 161 code | 50 blank | 166 comment | 55 complexity | a7ccdc29cd293e0fab1be0c468a5629d MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.apache.zookeeper.inspector.ui.utils;
  2. import java.io.IOException;
  3. import java.util.Locale;
  4. import org.apache.zookeeper.inspector.logger.LoggerFactory;
  5. /**
  6. * Provides methods to get operating system properties, resources and versions,
  7. * and determine operating system criteria.
  8. */
  9. public class OSUtils {
  10. private static final org.slf4j.Logger LOG = LoggerFactory.getLogger();
  11. static {
  12. setOperatingSystems();
  13. }
  14. /**
  15. * Variable for whether or not we're on Windows.
  16. */
  17. private static boolean _isWindows;
  18. /**
  19. * Variable for whether or not we're on Windows NT.
  20. */
  21. private static boolean _isWindowsNT;
  22. /**
  23. * Variable for whether or not we're on Windows XP.
  24. */
  25. private static boolean _isWindowsXP;
  26. /**
  27. * Variable for whether or not we're on Windows 7.
  28. */
  29. private static boolean _isWindows7;
  30. /**
  31. * Variable for whether or not we're on Windows 95.
  32. */
  33. private static boolean _isWindows95;
  34. /**
  35. * Variable for whether or not we're on Windows 98.
  36. */
  37. private static boolean _isWindows98;
  38. /**
  39. * Variable for whether or not we're on Windows Me.
  40. */
  41. private static boolean _isWindowsMe;
  42. /**
  43. * Variable for whether or not we're on Windows Vista.
  44. */
  45. private static boolean _isWindowsVista;
  46. /**
  47. * The Windows service pack version - only applicable to XP and Vista.
  48. */
  49. private static int _windowsServicePack;
  50. /**
  51. * Variable for whether or not the operating system allows the application
  52. * to be reduced to the system tray.
  53. */
  54. private static boolean _supportsTray;
  55. /**
  56. * Variable for whether or not we're on Mac OS X.
  57. */
  58. private static boolean _isMacOSX;
  59. /**
  60. * Variable for whether or not we're on Linux.
  61. */
  62. private static boolean _isLinux;
  63. /**
  64. * Variable for whether or not we're on Solaris.
  65. */
  66. private static boolean _isSolaris;
  67. /**
  68. * Variable for whether or not we're on OS/2.
  69. */
  70. private static boolean _isOS2;
  71. /**
  72. * Sets the operating system variables.
  73. */
  74. public static void setOperatingSystems() {
  75. _isWindows = false;
  76. _windowsServicePack = 0;
  77. _isWindowsVista = false;
  78. _isWindowsNT = false;
  79. _isWindowsXP = false;
  80. _isWindows7 = false;
  81. _isWindows95 = false;
  82. _isWindows98 = false;
  83. _isWindowsMe = false;
  84. _isSolaris = false;
  85. _isLinux = false;
  86. _isOS2 = false;
  87. _isMacOSX = false;
  88. String os = System.getProperty("os.name").toLowerCase(Locale.US);
  89. String version = System.getProperty("os.version").toLowerCase(Locale.US);
  90. // set the operating system variables
  91. _isWindows = os.indexOf("windows") != -1;
  92. if (os.indexOf("windows nt") != -1) {
  93. _isWindowsNT = true;
  94. } else if (os.indexOf("windows xp") != -1) {
  95. _isWindowsXP = true;
  96. } else if (os.indexOf("windows 7") != -1) {
  97. _isWindows7 = true;
  98. } else if (os.indexOf("windows vista") != -1 && version.startsWith("6.1")) {
  99. // In jdk 1.6 before update 14 the os.name system property still
  100. // returns Windows Vista
  101. // The version number is set to 6.1 however, so we can check for
  102. // that and windows vista
  103. // together to determine if it is windows 7
  104. _isWindows7 = true;
  105. } else if (os.indexOf("windows vista") != -1) {
  106. _isWindowsVista = true;
  107. } else if (os.indexOf("windows 95") != -1) {
  108. _isWindows95 = true;
  109. } else if (os.indexOf("windows 98") != -1) {
  110. _isWindows98 = true;
  111. } else if (os.indexOf("windows me") != -1) {
  112. _isWindowsMe = true;
  113. } else if (os.indexOf("solaris") != -1) {
  114. _isSolaris = true;
  115. } else if (os.indexOf("linux") != -1) {
  116. _isLinux = true;
  117. } else if (os.indexOf("os/2") != -1) {
  118. _isOS2 = true;
  119. }
  120. if (_isWindows || _isLinux)
  121. _supportsTray = true;
  122. if (os.startsWith("mac os")) {
  123. if (os.endsWith("x")) {
  124. _isMacOSX = true;
  125. }
  126. }
  127. // If this is Windows XP or Vista, try to find out which service pack
  128. // is installed.
  129. if (_isWindowsXP || _isWindowsVista) {
  130. try {
  131. String ver = SystemUtils.registryReadText("HKEY_LOCAL_MACHINE",
  132. "Software\\Microsoft\\Windows NT\\CurrentVersion", "CSDVersion");
  133. ver = ver.replaceAll("[^0-9]", "");
  134. _windowsServicePack = Integer.parseInt(ver);
  135. if (LOG.isDebugEnabled())
  136. LOG.debug("Windows service pack " + _windowsServicePack);
  137. } catch (IOException e) {
  138. LOG.debug("Failed to determine Windows service pack", e);
  139. } catch (NumberFormatException e) {
  140. LOG.debug("Failed to determine Windows service pack", e);
  141. }
  142. }
  143. }
  144. /**
  145. * Returns the operating system.
  146. */
  147. public static String getOS() {
  148. return System.getProperty("os.name");
  149. }
  150. /**
  151. * Returns the operating system version.
  152. */
  153. public static String getOSVersion() {
  154. return System.getProperty("os.version");
  155. }
  156. /**
  157. * Returns the operating system architecture.
  158. */
  159. public static String getOSArch() {
  160. return System.getProperty("os.arch");
  161. }
  162. /**
  163. * Returns true if this is Windows NT or Windows 2000 and hence can support
  164. * a system tray feature.
  165. */
  166. public static boolean supportsTray() {
  167. return _supportsTray;
  168. }
  169. /**
  170. * Returns whether or not the OS is some version of Windows.
  171. *
  172. * @return <tt>true</tt> if the application is running on some Windows
  173. * version, <tt>false</tt> otherwise
  174. */
  175. public static boolean isWindows() {
  176. return _isWindows;
  177. }
  178. /**
  179. * Returns whether or not the OS is WinXP.
  180. *
  181. * @return <tt>true</tt> if the application is running on WinXP,
  182. * <tt>false</tt> otherwise
  183. */
  184. public static boolean isWindowsXP() {
  185. return _isWindowsXP;
  186. }
  187. /**
  188. * Returns whether or not the OS is Windows 7..
  189. *
  190. * @return <tt>true</tt> if the application is running on Windows 7,
  191. * <tt>false</tt> otherwise
  192. */
  193. public static boolean isWindows7() {
  194. return _isWindows7;
  195. }
  196. /**
  197. * @return true if the application is running on Windows NT
  198. */
  199. public static boolean isWindowsNT() {
  200. return _isWindowsNT;
  201. }
  202. /**
  203. * @return true if the application is running on Windows 95
  204. */
  205. public static boolean isWindows95() {
  206. return _isWindows95;
  207. }
  208. /**
  209. * @return true if the application is running on Windows 98
  210. */
  211. public static boolean isWindows98() {
  212. return _isWindows98;
  213. }
  214. /**
  215. * @return true if the application is running on Windows ME
  216. */
  217. public static boolean isWindowsMe() {
  218. return _isWindowsMe;
  219. }
  220. /**
  221. * @return true if the application is running on Windows Vista
  222. */
  223. public static boolean isWindowsVista() {
  224. return _isWindowsVista;
  225. }
  226. /**
  227. * @return true if the application is running on a windows with the 10
  228. * socket limit.
  229. */
  230. public static boolean isSocketChallengedWindows() {
  231. return _isWindowsXP || (_isWindowsVista && _windowsServicePack < 3);
  232. }
  233. /**
  234. * Returns whether or not the OS is OS/2.
  235. *
  236. * @return <tt>true</tt> if the application is running on OS/2,
  237. * <tt>false</tt> otherwise
  238. */
  239. public static boolean isOS2() {
  240. return _isOS2;
  241. }
  242. /**
  243. * Returns whether or not the OS is Mac OS X.
  244. *
  245. * @return <tt>true</tt> if the application is running on Mac OS X,
  246. * <tt>false</tt> otherwise
  247. */
  248. public static boolean isMacOSX() {
  249. return _isMacOSX;
  250. }
  251. /**
  252. * Returns whether or not the OS is Solaris.
  253. *
  254. * @return <tt>true</tt> if the application is running on Solaris,
  255. * <tt>false</tt> otherwise
  256. */
  257. public static boolean isSolaris() {
  258. return _isSolaris;
  259. }
  260. /**
  261. * Returns whether or not the OS is Linux.
  262. *
  263. * @return <tt>true</tt> if the application is running on Linux,
  264. * <tt>false</tt> otherwise
  265. */
  266. public static boolean isLinux() {
  267. return _isLinux;
  268. }
  269. /**
  270. * Returns whether or not the OS is some version of Unix, defined here as
  271. * only Solaris or Linux.
  272. */
  273. public static boolean isUnix() {
  274. return _isLinux || _isSolaris;
  275. }
  276. /**
  277. * Returns whether the OS is POSIX-like.
  278. */
  279. public static boolean isPOSIX() {
  280. return _isLinux || _isSolaris || _isMacOSX;
  281. }
  282. /**
  283. * Returns whether or not this operating system is considered capable of
  284. * meeting the requirements of a high load server.
  285. *
  286. * @return <tt>true</tt> if this OS meets high load server requirements,
  287. * <tt>false</tt> otherwise
  288. */
  289. public static boolean isHighLoadOS() {
  290. return !(_isWindows98 || _isWindows95 || _isWindowsMe || _isWindowsNT);
  291. }
  292. /**
  293. * @return true if this is a well-supported version of windows. (not 95, 98,
  294. * nt or me)
  295. */
  296. public static boolean isGoodWindows() {
  297. return isWindows() && isHighLoadOS();
  298. }
  299. /**
  300. * Return whether the current operating system supports moving files to the
  301. * trash.
  302. */
  303. public static boolean supportsTrash() {
  304. return isWindows() || isMacOSX();
  305. }
  306. /**
  307. * Returns the maximum path system of file system of the current OS or a
  308. * conservative approximation.
  309. */
  310. public static int getMaxPathLength() {
  311. if (isWindows()) {
  312. // From Windows NT onwards, Windows applications can create or
  313. // manipulate path lengths of 260 characters, where 259 are
  314. // actual characters + 1 NULL character. Where path is denoted
  315. // by the path, filename and extension plus 1 null character.
  316. //
  317. // NTFS can actually support 32767 characters in the paths
  318. // but one must prepend \\?\ to the file paths. This tells the
  319. // APIs to not to enforce 260 character limit.
  320. return 259;
  321. } else if (isLinux()) {
  322. return 4096 - 1;
  323. } else {
  324. return 1024 - 1;
  325. }
  326. }
  327. }