PageRenderTime 1518ms CodeModel.GetById 48ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/com/izforge/izpack/util/OsVersion.java

https://github.com/Jahia/izpack
Java | 373 lines | 177 code | 46 blank | 150 comment | 42 complexity | 772702d785db29b67b941867f43f1c6f MD5 | raw file
  1. /*
  2. * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
  3. *
  4. * http://izpack.org/ http://izpack.codehaus.org/
  5. *
  6. * Copyright 2004 Hani Suleiman
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  9. * in compliance with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software distributed under the License
  14. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  15. * or implied. See the License for the specific language governing permissions and limitations under
  16. * the License.
  17. */
  18. package com.izforge.izpack.util;
  19. import java.io.File;
  20. import java.io.IOException;
  21. /**
  22. * This is a convienient class, which helps you to detect / identify the running OS/Distribution
  23. * <p/>
  24. * Created at: Date: Nov 9, 2004 Time: 8:53:22 PM
  25. *
  26. * @author hani, Marc.Eppelmann&#064;reddot.de
  27. */
  28. public final class OsVersion implements OsVersionConstants, StringConstants
  29. {
  30. //~ Static fields/initializers
  31. // *******************************************************************************************************************************
  32. /**
  33. * OS_NAME = System.getProperty( "os.name" )
  34. */
  35. public static final String OS_NAME = System.getProperty(OSNAME);
  36. /**
  37. * OS_ARCH = System.getProperty("os.arch")
  38. */
  39. public static final String OS_ARCH = System.getProperty(OSARCH);
  40. /**
  41. * OS_VERSION = System.getProperty("os.aversion")
  42. */
  43. public static final String OS_VERSION = System.getProperty(OSVERSION);
  44. /**
  45. * True if the processor is in the Intel x86 family.
  46. */
  47. public static final boolean IS_X86 = StringTool.startsWithIgnoreCase(OS_ARCH, X86) ||
  48. StringTool.startsWithIgnoreCase(OS_ARCH, I386);
  49. /**
  50. * True if the processor is in the PowerPC family.
  51. */
  52. public static final boolean IS_PPC = StringTool.startsWithIgnoreCase(OS_ARCH, PPC);
  53. /**
  54. * True if the processor is in the SPARC family.
  55. */
  56. public static final boolean IS_SPARC = StringTool.startsWithIgnoreCase(OS_ARCH, SPARC);
  57. /**
  58. * True if this is FreeBSD.
  59. */
  60. public static final boolean IS_FREEBSD = StringTool.startsWithIgnoreCase(OS_NAME, FREEBSD);
  61. /**
  62. * True if this is Linux.
  63. */
  64. public static final boolean IS_LINUX = StringTool.startsWithIgnoreCase(OS_NAME, LINUX);
  65. /**
  66. * True if this is HP-UX.
  67. */
  68. public static final boolean IS_HPUX = StringTool.startsWithIgnoreCase(OS_NAME, HP_UX);
  69. /**
  70. * True if this is AIX.
  71. */
  72. public static final boolean IS_AIX = StringTool.startsWithIgnoreCase(OS_NAME, AIX);
  73. /**
  74. * True if this is SunOS.
  75. */
  76. public static final boolean IS_SUNOS = StringTool.startsWithIgnoreCase(OS_NAME, SUNOS) ||
  77. StringTool.startsWithIgnoreCase(OS_NAME, SOLARIS);
  78. /**
  79. * True if this is SunOS / x86
  80. */
  81. public static final boolean IS_SUNOS_X86 = IS_SUNOS && IS_X86;
  82. /**
  83. * True if this is SunOS / sparc
  84. */
  85. public static final boolean IS_SUNOS_SPARC = IS_SUNOS && IS_SPARC;
  86. /**
  87. * True if this is OS/2.
  88. */
  89. public static final boolean IS_OS2 = StringTool.startsWith(OS_NAME, OS_2);
  90. /**
  91. * True is this is Mac OS
  92. */
  93. public static final boolean IS_MAC = StringTool.startsWith(OS_NAME, MAC);
  94. /**
  95. * True if this is the Mac OS X.
  96. */
  97. public static final boolean IS_OSX = StringTool.startsWithIgnoreCase(OS_NAME, MACOSX);
  98. /**
  99. * True if this is Windows.
  100. */
  101. public static final boolean IS_WINDOWS = StringTool.startsWith(OS_NAME, WINDOWS);
  102. /**
  103. * True if this is Windows XP
  104. */
  105. public static final boolean IS_WINDOWS_XP = IS_WINDOWS && OS_VERSION.equals(WINDOWS_XP_VERSION);
  106. /**
  107. * True if this is Windows 2003
  108. */
  109. public static final boolean IS_WINDOWS_2003 = IS_WINDOWS && OS_VERSION.equals(WINDOWS_2003_VERSION);
  110. /**
  111. * True if this is Windows VISTA
  112. */
  113. public static final boolean IS_WINDOWS_VISTA = IS_WINDOWS && OS_VERSION.equals(WINDOWS_VISTA_VERSION);
  114. /**
  115. * True if this is Windows 7
  116. */
  117. public static final boolean IS_WINDOWS_7 = IS_WINDOWS && OS_VERSION.equals(WINDOWS_7_VERSION);
  118. /**
  119. * True if this is Windows 8
  120. */
  121. public static final boolean IS_WINDOWS_8 = IS_WINDOWS && OS_VERSION.equals(WINDOWS_8_VERSION);
  122. /**
  123. * True if this is Windows 8.1
  124. */
  125. public static final boolean IS_WINDOWS_81 = IS_WINDOWS && OS_VERSION.equals(WINDOWS_81_VERSION);
  126. /**
  127. * True if this is Windows version that requires Administrator access
  128. */
  129. public static final boolean IS_WINDOWS_ADMIN_REQUIRED = IS_WINDOWS && (IS_WINDOWS_VISTA || IS_WINDOWS_7 || IS_WINDOWS_8 || IS_WINDOWS_81);
  130. /**
  131. * True if this is some variant of Unix (OSX, Linux, Solaris, FreeBSD, etc).
  132. */
  133. public static final boolean IS_UNIX = !IS_OS2 && !IS_WINDOWS;
  134. /**
  135. * True if RedHat Linux was detected
  136. */
  137. public static final boolean IS_REDHAT_LINUX = IS_LINUX
  138. && ((FileUtil.fileContains(getReleaseFileName(), REDHAT) || FileUtil.fileContains(getReleaseFileName(),
  139. RED_HAT)));
  140. /**
  141. * True if Fedora Linux was detected
  142. */
  143. public static final boolean IS_FEDORA_LINUX = IS_LINUX
  144. && FileUtil.fileContains(getReleaseFileName(), FEDORA);
  145. /**
  146. * True if Mandriva(Mandrake) Linux was detected
  147. */
  148. public static final boolean IS_MANDRAKE_LINUX = IS_LINUX
  149. && FileUtil.fileContains(getReleaseFileName(), MANDRAKE);
  150. /**
  151. * True if Mandrake/Mandriva Linux was detected
  152. */
  153. public static final boolean IS_MANDRIVA_LINUX = (IS_LINUX
  154. && FileUtil.fileContains(getReleaseFileName(), MANDRIVA)) || IS_MANDRAKE_LINUX;
  155. /**
  156. * True if SuSE Linux was detected
  157. */
  158. public static final boolean IS_SUSE_LINUX = IS_LINUX
  159. && FileUtil.fileContains(getReleaseFileName(), SUSE, true); /* caseInsensitive , since 'SUSE' 10 */
  160. /**
  161. * True if Debian Linux or derived was detected
  162. */
  163. public static final boolean IS_DEBIAN_LINUX = (IS_LINUX
  164. && FileUtil.fileContains(PROC_VERSION, DEBIAN)) || (IS_LINUX && new File("/etc/debian_version").exists());
  165. // TODO detect the newcomer (K)Ubuntu */
  166. //~ Methods
  167. // **************************************************************************************************************************************************
  168. /**
  169. * Gets the etc Release Filename
  170. *
  171. * @return name of the file the release info is stored in for Linux distributions
  172. */
  173. private static String getReleaseFileName()
  174. {
  175. String result = "";
  176. File[] etcList = new File("/etc").listFiles();
  177. if (etcList != null)
  178. {
  179. for (File etcEntry : etcList)
  180. {
  181. if (etcEntry.isFile())
  182. {
  183. if (etcEntry.getName().endsWith("-release"))
  184. {
  185. //match :-)
  186. return result = etcEntry.toString();
  187. }
  188. }
  189. }
  190. }
  191. return result;
  192. }
  193. /**
  194. * Gets the Details of a Linux Distribution
  195. *
  196. * @return description string of the Linux distribution
  197. */
  198. private static String getLinuxDistribution()
  199. {
  200. String result = null;
  201. if (IS_SUSE_LINUX)
  202. {
  203. try
  204. {
  205. result = SUSE + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  206. }
  207. catch (IOException e)
  208. {
  209. // TODO ignore
  210. }
  211. }
  212. else if (IS_REDHAT_LINUX)
  213. {
  214. try
  215. {
  216. result = REDHAT + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  217. }
  218. catch (IOException e)
  219. {
  220. // TODO ignore
  221. }
  222. }
  223. else if (IS_FEDORA_LINUX)
  224. {
  225. try
  226. {
  227. result = FEDORA + SP + LINUX + NL
  228. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  229. }
  230. catch (IOException e)
  231. {
  232. // TODO ignore
  233. }
  234. }
  235. else if (IS_MANDRAKE_LINUX)
  236. {
  237. try
  238. {
  239. result = MANDRAKE + SP + LINUX + NL
  240. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  241. }
  242. catch (IOException e)
  243. {
  244. // TODO ignore
  245. }
  246. }
  247. else if (IS_MANDRIVA_LINUX)
  248. {
  249. try
  250. {
  251. result = MANDRIVA + SP + LINUX + NL
  252. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  253. }
  254. catch (IOException e)
  255. {
  256. // TODO ignore
  257. }
  258. }
  259. else if (IS_DEBIAN_LINUX)
  260. {
  261. try
  262. {
  263. result = DEBIAN + SP + LINUX + NL
  264. + StringTool.stringArrayListToString(FileUtil.getFileContent("/etc/debian_version"));
  265. }
  266. catch (IOException e)
  267. {
  268. // TODO ignore
  269. }
  270. }
  271. else
  272. {
  273. try
  274. {
  275. result = "Unknown Linux Distribution\n"
  276. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  277. }
  278. catch (IOException e)
  279. {
  280. // TODO ignore
  281. }
  282. }
  283. return result;
  284. }
  285. /**
  286. * returns a String which contains details of known OSs
  287. *
  288. * @return the details
  289. */
  290. public static String getOsDetails()
  291. {
  292. StringBuffer result = new StringBuffer();
  293. result.append("OS_NAME=").append(OS_NAME).append(NL);
  294. if (IS_UNIX)
  295. {
  296. if (IS_LINUX)
  297. {
  298. result.append(getLinuxDistribution()).append(NL);
  299. }
  300. else
  301. {
  302. try
  303. {
  304. result.append(FileUtil.getFileContent(getReleaseFileName())).append(NL);
  305. }
  306. catch (IOException e)
  307. {
  308. Debug.log("Unable to get release file contents in 'getOsDetails'.");
  309. }
  310. }
  311. }
  312. if (IS_WINDOWS)
  313. {
  314. result.append(System.getProperty(OSNAME)).append(SP).append(System.getProperty("sun.os.patch.level", "")).append(NL);
  315. }
  316. return result.toString();
  317. }
  318. /**
  319. * Testmain
  320. *
  321. * @param args Commandline Args
  322. */
  323. public static void main(String[] args)
  324. {
  325. System.out.println(getOsDetails());
  326. }
  327. }