PageRenderTime 1590ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/izpack-src/tags/4.2.1/src/lib/com/izforge/izpack/util/OsVersion.java

https://github.com/jponge/izpack-full-svn-history-copy
Java | 358 lines | 174 code | 43 blank | 141 comment | 36 complexity | ce5bf9ccd47bad99545a28686258204e 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 some variant of Unix (OSX, Linux, Solaris, FreeBSD, etc).
  120. */
  121. public static final boolean IS_UNIX = !IS_OS2 && !IS_WINDOWS;
  122. /**
  123. * True if RedHat Linux was detected
  124. */
  125. public static final boolean IS_REDHAT_LINUX = IS_LINUX
  126. && ((FileUtil.fileContains(getReleaseFileName(), REDHAT) || FileUtil.fileContains(getReleaseFileName(),
  127. RED_HAT)));
  128. /**
  129. * True if Fedora Linux was detected
  130. */
  131. public static final boolean IS_FEDORA_LINUX = IS_LINUX
  132. && FileUtil.fileContains(getReleaseFileName(), FEDORA);
  133. /**
  134. * True if Mandriva(Mandrake) Linux was detected
  135. */
  136. public static final boolean IS_MANDRAKE_LINUX = IS_LINUX
  137. && FileUtil.fileContains(getReleaseFileName(), MANDRAKE);
  138. /**
  139. * True if Mandrake/Mandriva Linux was detected
  140. */
  141. public static final boolean IS_MANDRIVA_LINUX = (IS_LINUX
  142. && FileUtil.fileContains(getReleaseFileName(), MANDRIVA)) || IS_MANDRAKE_LINUX;
  143. /**
  144. * True if SuSE Linux was detected
  145. */
  146. public static final boolean IS_SUSE_LINUX = IS_LINUX
  147. && FileUtil.fileContains(getReleaseFileName(), SUSE, true); /* caseInsensitive , since 'SUSE' 10 */
  148. /**
  149. * True if Debian Linux or derived was detected
  150. */
  151. public static final boolean IS_DEBIAN_LINUX = (IS_LINUX
  152. && FileUtil.fileContains(PROC_VERSION, DEBIAN)) || (IS_LINUX && new File("/etc/debian_version").exists());
  153. // TODO detect the newcomer (K)Ubuntu */
  154. //~ Methods
  155. // **************************************************************************************************************************************************
  156. /**
  157. * Gets the etc Release Filename
  158. *
  159. * @return name of the file the release info is stored in for Linux distributions
  160. */
  161. private static String getReleaseFileName()
  162. {
  163. String result = "";
  164. File[] etcList = new File("/etc").listFiles();
  165. if (etcList != null)
  166. {
  167. for (File etcEntry : etcList)
  168. {
  169. if (etcEntry.isFile())
  170. {
  171. if (etcEntry.getName().endsWith("-release"))
  172. {
  173. //match :-)
  174. return result = etcEntry.toString();
  175. }
  176. }
  177. }
  178. }
  179. return result;
  180. }
  181. /**
  182. * Gets the Details of a Linux Distribution
  183. *
  184. * @return description string of the Linux distribution
  185. */
  186. private static String getLinuxDistribution()
  187. {
  188. String result = null;
  189. if (IS_SUSE_LINUX)
  190. {
  191. try
  192. {
  193. result = SUSE + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  194. }
  195. catch (IOException e)
  196. {
  197. // TODO ignore
  198. }
  199. }
  200. else if (IS_REDHAT_LINUX)
  201. {
  202. try
  203. {
  204. result = REDHAT + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  205. }
  206. catch (IOException e)
  207. {
  208. // TODO ignore
  209. }
  210. }
  211. else if (IS_FEDORA_LINUX)
  212. {
  213. try
  214. {
  215. result = FEDORA + SP + LINUX + NL
  216. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  217. }
  218. catch (IOException e)
  219. {
  220. // TODO ignore
  221. }
  222. }
  223. else if (IS_MANDRAKE_LINUX)
  224. {
  225. try
  226. {
  227. result = MANDRAKE + SP + LINUX + NL
  228. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  229. }
  230. catch (IOException e)
  231. {
  232. // TODO ignore
  233. }
  234. }
  235. else if (IS_MANDRIVA_LINUX)
  236. {
  237. try
  238. {
  239. result = MANDRIVA + SP + LINUX + NL
  240. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  241. }
  242. catch (IOException e)
  243. {
  244. // TODO ignore
  245. }
  246. }
  247. else if (IS_DEBIAN_LINUX)
  248. {
  249. try
  250. {
  251. result = DEBIAN + SP + LINUX + NL
  252. + StringTool.stringArrayListToString(FileUtil.getFileContent("/etc/debian_version"));
  253. }
  254. catch (IOException e)
  255. {
  256. // TODO ignore
  257. }
  258. }
  259. else
  260. {
  261. try
  262. {
  263. result = "Unknown Linux Distribution\n"
  264. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  265. }
  266. catch (IOException e)
  267. {
  268. // TODO ignore
  269. }
  270. }
  271. return result;
  272. }
  273. /**
  274. * returns a String which contains details of known OSs
  275. *
  276. * @return the details
  277. */
  278. public static String getOsDetails()
  279. {
  280. StringBuffer result = new StringBuffer();
  281. result.append("OS_NAME=").append(OS_NAME).append(NL);
  282. if (IS_UNIX)
  283. {
  284. if (IS_LINUX)
  285. {
  286. result.append(getLinuxDistribution()).append(NL);
  287. }
  288. else
  289. {
  290. try
  291. {
  292. result.append(FileUtil.getFileContent(getReleaseFileName())).append(NL);
  293. }
  294. catch (IOException e)
  295. {
  296. Debug.log("Unable to get release file contents in 'getOsDetails'.");
  297. }
  298. }
  299. }
  300. if (IS_WINDOWS)
  301. {
  302. result.append(System.getProperty(OSNAME)).append(SP).append(System.getProperty("sun.os.patch.level", "")).append(NL);
  303. }
  304. return result.toString();
  305. }
  306. /**
  307. * Testmain
  308. *
  309. * @param args Commandline Args
  310. */
  311. public static void main(String[] args)
  312. {
  313. System.out.println(getOsDetails());
  314. }
  315. }