PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/source/org/limewire/util/OSUtils.java

https://github.com/zootella/Lost-in-the-Space
Java | 415 lines | 203 code | 52 blank | 160 comment | 60 complexity | acc46093598a316aa34897d0e13a19a6 MD5 | raw file
  1. package org.limewire.util;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.Locale;
  7. import java.util.Map;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.limewire.inspection.Inspectable;
  11. import org.limewire.inspection.InspectionPoint;
  12. /**
  13. * Provides methods to get operating system properties, resources and versions,
  14. * and determine operating system criteria.
  15. */
  16. public class OSUtils {
  17. private static final Log LOG = LogFactory.getLog(OSUtils.class);
  18. static {
  19. setOperatingSystems();
  20. }
  21. /**
  22. * Variable for whether or not we're on Windows.
  23. */
  24. private static boolean _isWindows;
  25. /**
  26. * Variable for whether or not we're on Windows NT.
  27. */
  28. private static boolean _isWindowsNT;
  29. /**
  30. * Variable for whether or not we're on Windows XP.
  31. */
  32. private static boolean _isWindowsXP;
  33. /**
  34. * Variable for whether or not we're on Windows 7.
  35. */
  36. private static boolean _isWindows7;
  37. /**
  38. * Variable for whether or not we're on Windows 95.
  39. */
  40. private static boolean _isWindows95;
  41. /**
  42. * Variable for whether or not we're on Windows 98.
  43. */
  44. private static boolean _isWindows98;
  45. /**
  46. * Variable for whether or not we're on Windows Me.
  47. */
  48. private static boolean _isWindowsMe;
  49. /**
  50. * Variable for whether or not we're on Windows Vista.
  51. */
  52. private static boolean _isWindowsVista;
  53. /**
  54. * Variable for whether or not we're on Windows Vista SP2 or higher.
  55. */
  56. private static boolean _isSlightlyLessBrokenVersionOfWindowsVista;
  57. /**
  58. * Variable for whether or not the operating system allows the
  59. * application to be reduced to the system tray.
  60. */
  61. private static boolean _supportsTray;
  62. /**
  63. * Variable for whether or not we're on Mac OS X.
  64. */
  65. private static boolean _isMacOSX;
  66. /**
  67. * Variable for whether or not we're on Linux.
  68. */
  69. private static boolean _isLinux;
  70. /**
  71. * Variable for whether or not we're on Solaris.
  72. */
  73. private static boolean _isSolaris;
  74. /**
  75. * Variable for whether or not we're on OS/2.
  76. */
  77. private static boolean _isOS2;
  78. /** Operating System information */
  79. @SuppressWarnings("unused") @InspectionPoint("os_info")
  80. private static final Inspectable osInspect = new OSInspecter();
  81. /**
  82. * Sets the operating system variables.
  83. */
  84. public static void setOperatingSystems() {
  85. _isWindows = false;
  86. _isWindowsVista = false;
  87. _isSlightlyLessBrokenVersionOfWindowsVista = false;
  88. _isWindowsNT = false;
  89. _isWindowsXP = false;
  90. _isWindows7 = false;
  91. _isWindows95 = false;
  92. _isWindows98 = false;
  93. _isWindowsMe = false;
  94. _isSolaris = false;
  95. _isLinux = false;
  96. _isOS2 = false;
  97. _isMacOSX = false;
  98. String os = System.getProperty("os.name").toLowerCase(Locale.US);
  99. String version = System.getProperty("os.version").toLowerCase(Locale.US);
  100. // set the operating system variables
  101. _isWindows = os.indexOf("windows") != -1;
  102. if (os.indexOf("windows nt") != -1) {
  103. _isWindowsNT = true;
  104. } else if (os.indexOf("windows xp") != -1) {
  105. _isWindowsXP = true;
  106. } else if(os.indexOf("windows 7") != -1) {
  107. _isWindows7 = true;
  108. } else if (os.indexOf("windows vista") != -1 && version.startsWith("6.1")) {
  109. //In jdk 1.6 before update 14 the os.name system property still returns Windows Vista
  110. //The version number is set to 6.1 however, so we can check for that and windows vista
  111. //together to determine if it is windows 7
  112. _isWindows7 = true;
  113. } else if (os.indexOf("windows vista") != -1) {
  114. _isWindowsVista = true;
  115. } else if(os.indexOf("windows 95") != -1) {
  116. _isWindows95 = true;
  117. } else if(os.indexOf("windows 98") != -1) {
  118. _isWindows98 = true;
  119. } else if(os.indexOf("windows me") != -1) {
  120. _isWindowsMe = true;
  121. } else if(os.indexOf("solaris") != -1) {
  122. _isSolaris = true;
  123. } else if(os.indexOf("linux") != -1) {
  124. _isLinux = true;
  125. } else if(os.indexOf("os/2") != -1) {
  126. _isOS2 = true;
  127. }
  128. if(_isWindows || _isLinux)
  129. _supportsTray = true;
  130. if(os.startsWith("mac os")) {
  131. if(os.endsWith("x")) {
  132. _isMacOSX = true;
  133. }
  134. }
  135. // If this is Windows Vista, try to find out whether SP2 (or higher)
  136. // is installed, which removes the half-open TCP connection limit.
  137. if(_isWindowsVista) {
  138. BufferedReader br = null;
  139. try {
  140. // Execute reg.exe to query a registry key
  141. Process p = Runtime.getRuntime().exec(new String[] {
  142. "reg",
  143. "query",
  144. "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
  145. "/v",
  146. "CSDVersion"
  147. });
  148. // Parse the output
  149. br = new BufferedReader(
  150. new InputStreamReader(p.getInputStream()));
  151. String line = null;
  152. while((line = br.readLine()) != null) {
  153. if(line.matches(".*CSDVersion.*"))
  154. break;
  155. }
  156. // Assume there won't be more than 9 service packs for Vista
  157. if(line != null && line.matches(".*Service Pack [2-9]")) {
  158. LOG.debug("Slightly less broken version of Windows Vista");
  159. _isSlightlyLessBrokenVersionOfWindowsVista = true;
  160. }
  161. } catch(Throwable t) {
  162. LOG.debug("Failed to determine Windows version", t);
  163. } finally {
  164. if(br != null) {
  165. try {
  166. br.close();
  167. } catch(IOException ignored) {}
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * Returns the operating system.
  174. */
  175. public static String getOS() {
  176. return System.getProperty("os.name");
  177. }
  178. /**
  179. * Returns the operating system version.
  180. */
  181. public static String getOSVersion() {
  182. return System.getProperty("os.version");
  183. }
  184. /**
  185. * Returns the operating system architecture.
  186. */
  187. public static String getOSArch() {
  188. return System.getProperty("os.arch");
  189. }
  190. /**
  191. * Returns true if this is Windows NT or Windows 2000 and
  192. * hence can support a system tray feature.
  193. */
  194. public static boolean supportsTray() {
  195. return _supportsTray;
  196. }
  197. /**
  198. * Returns whether or not the OS is some version of Windows.
  199. *
  200. * @return <tt>true</tt> if the application is running on some Windows
  201. * version, <tt>false</tt> otherwise
  202. */
  203. public static boolean isWindows() {
  204. return _isWindows;
  205. }
  206. /**
  207. * Returns whether or not the OS is WinXP.
  208. *
  209. * @return <tt>true</tt> if the application is running on WinXP,
  210. * <tt>false</tt> otherwise
  211. */
  212. public static boolean isWindowsXP() {
  213. return _isWindowsXP;
  214. }
  215. /**
  216. * Returns whether or not the OS is Windows 7..
  217. *
  218. * @return <tt>true</tt> if the application is running on Windows 7,
  219. * <tt>false</tt> otherwise
  220. */
  221. public static boolean isWindows7() {
  222. return _isWindows7;
  223. }
  224. /**
  225. * @return true if the application is running on Windows NT
  226. */
  227. public static boolean isWindowsNT() {
  228. return _isWindowsNT;
  229. }
  230. /**
  231. * @return true if the application is running on Windows 95
  232. */
  233. public static boolean isWindows95() {
  234. return _isWindows95;
  235. }
  236. /**
  237. * @return true if the application is running on Windows 98
  238. */
  239. public static boolean isWindows98() {
  240. return _isWindows98;
  241. }
  242. /**
  243. * @return true if the application is running on Windows ME
  244. */
  245. public static boolean isWindowsMe() {
  246. return _isWindowsMe;
  247. }
  248. /**
  249. * @return true if the application is running on Windows Vista
  250. */
  251. public static boolean isWindowsVista() {
  252. return _isWindowsVista;
  253. }
  254. /**
  255. * @return true if the application is running on a windows with
  256. * the 10 socket limit.
  257. */
  258. public static boolean isSocketChallengedWindows() {
  259. return _isWindowsXP || (_isWindowsVista && !_isSlightlyLessBrokenVersionOfWindowsVista);
  260. }
  261. /**
  262. * Returns whether or not the OS is OS/2.
  263. *
  264. * @return <tt>true</tt> if the application is running on OS/2,
  265. * <tt>false</tt> otherwise
  266. */
  267. public static boolean isOS2() {
  268. return _isOS2;
  269. }
  270. /**
  271. * Returns whether or not the OS is Mac OS X.
  272. *
  273. * @return <tt>true</tt> if the application is running on Mac OS X,
  274. * <tt>false</tt> otherwise
  275. */
  276. public static boolean isMacOSX() {
  277. return _isMacOSX;
  278. }
  279. /**
  280. * Returns whether or not the OS is Solaris.
  281. *
  282. * @return <tt>true</tt> if the application is running on Solaris,
  283. * <tt>false</tt> otherwise
  284. */
  285. public static boolean isSolaris() {
  286. return _isSolaris;
  287. }
  288. /**
  289. * Returns whether or not the OS is Linux.
  290. *
  291. * @return <tt>true</tt> if the application is running on Linux,
  292. * <tt>false</tt> otherwise
  293. */
  294. public static boolean isLinux() {
  295. return _isLinux;
  296. }
  297. /**
  298. * Returns whether or not the OS is some version of
  299. * Unix, defined here as only Solaris or Linux.
  300. */
  301. public static boolean isUnix() {
  302. return _isLinux || _isSolaris;
  303. }
  304. /**
  305. * Returns whether the OS is POSIX-like.
  306. */
  307. public static boolean isPOSIX() {
  308. return _isLinux || _isSolaris || _isMacOSX;
  309. }
  310. /**
  311. * Returns whether or not this operating system is considered
  312. * capable of meeting the requirements of a high load server.
  313. *
  314. * @return <tt>true</tt> if this OS meets high load server requirements,
  315. * <tt>false</tt> otherwise
  316. */
  317. public static boolean isHighLoadOS() {
  318. return !(_isWindows98 || _isWindows95 || _isWindowsMe || _isWindowsNT);
  319. }
  320. /**
  321. * @return true if this is a well-supported version of windows.
  322. * (not 95, 98, nt or me)
  323. */
  324. public static boolean isGoodWindows() {
  325. return isWindows() && isHighLoadOS();
  326. }
  327. /**
  328. * Return whether the current operating system supports moving files
  329. * to the trash.
  330. */
  331. public static boolean supportsTrash() {
  332. return isWindows() || isMacOSX();
  333. }
  334. /**
  335. * Returns the maximum path system of file system of the current OS
  336. * or a conservative approximation.
  337. */
  338. public static int getMaxPathLength() {
  339. if (isWindows()) {
  340. return Short.MAX_VALUE;
  341. }
  342. else if (isLinux()) {
  343. return 4096 - 1;
  344. }
  345. else {
  346. return 1024 - 1;
  347. }
  348. }
  349. public static boolean supportsTLS() {
  350. return true;
  351. }
  352. private static class OSInspecter implements Inspectable {
  353. @Override
  354. public Object inspect() {
  355. Map<String, Object> data = new HashMap<String, Object>();
  356. data.put("os_name", getOS());
  357. data.put("os_ver", getOSVersion());
  358. data.put("os_arch", getOSArch());
  359. data.put("num_cpus", Runtime.getRuntime().availableProcessors());
  360. return data;
  361. }
  362. }
  363. }