PageRenderTime 77ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/izpack-src/branches/4.0/src/lib/com/izforge/izpack/util/OsVersion.java

https://github.com/jponge/izpack-full-svn-history-copy
Java | 333 lines | 169 code | 38 blank | 126 comment | 32 complexity | b5d456c5eb74f2f2b166f7b0a05f8e6a 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. * True if the processor is in the Intel x86 family.
  42. */
  43. public static final boolean IS_X86 = StringTool.startsWithIgnoreCase(OS_ARCH, X86) ||
  44. StringTool.startsWithIgnoreCase(OS_ARCH, I386);
  45. /**
  46. * True if the processor is in the PowerPC family.
  47. */
  48. public static final boolean IS_PPC = StringTool.startsWithIgnoreCase(OS_ARCH, PPC);
  49. /**
  50. * True if the processor is in the SPARC family.
  51. */
  52. public static final boolean IS_SPARC = StringTool.startsWithIgnoreCase(OS_ARCH, SPARC);
  53. /**
  54. * True if this is FreeBSD.
  55. */
  56. public static final boolean IS_FREEBSD = StringTool.startsWithIgnoreCase(OS_NAME, FREEBSD);
  57. /**
  58. * True if this is Linux.
  59. */
  60. public static final boolean IS_LINUX = StringTool.startsWithIgnoreCase(OS_NAME, LINUX);
  61. /**
  62. * True if this is HP-UX.
  63. */
  64. public static final boolean IS_HPUX = StringTool.startsWithIgnoreCase(OS_NAME, HP_UX);
  65. /**
  66. * True if this is AIX.
  67. */
  68. public static final boolean IS_AIX = StringTool.startsWithIgnoreCase(OS_NAME, AIX);
  69. /**
  70. * True if this is SunOS.
  71. */
  72. public static final boolean IS_SUNOS = StringTool.startsWithIgnoreCase(OS_NAME, SUNOS) ||
  73. StringTool.startsWithIgnoreCase(OS_NAME, SOLARIS);
  74. /**
  75. * True if this is SunOS / x86
  76. */
  77. public static final boolean IS_SUNOS_X86 = IS_SUNOS && IS_X86;
  78. /**
  79. * True if this is SunOS / sparc
  80. */
  81. public static final boolean IS_SUNOS_SPARC = IS_SUNOS && IS_SPARC;
  82. /**
  83. * True if this is OS/2.
  84. */
  85. public static final boolean IS_OS2 = StringTool.startsWith(OS_NAME, OS_2);
  86. /**
  87. * True is this is Mac OS
  88. */
  89. public static final boolean IS_MAC = StringTool.startsWith(OS_NAME, MAC);
  90. /**
  91. * True if this is the Mac OS X.
  92. */
  93. public static final boolean IS_OSX = StringTool.startsWithIgnoreCase(OS_NAME, MACOSX);
  94. /**
  95. * True if this is Windows.
  96. */
  97. public static final boolean IS_WINDOWS = StringTool.startsWith(OS_NAME, WINDOWS);
  98. /**
  99. * True if this is some variant of Unix (OSX, Linux, Solaris, FreeBSD, etc).
  100. */
  101. public static final boolean IS_UNIX = !IS_OS2 && !IS_WINDOWS;
  102. /**
  103. * True if RedHat Linux was detected
  104. */
  105. public static final boolean IS_REDHAT_LINUX = IS_LINUX
  106. && ((FileUtil.fileContains(getReleaseFileName(), REDHAT) || FileUtil.fileContains(getReleaseFileName(),
  107. RED_HAT)));
  108. /**
  109. * True if Fedora Linux was detected
  110. */
  111. public static final boolean IS_FEDORA_LINUX = IS_LINUX
  112. && FileUtil.fileContains(getReleaseFileName(), FEDORA);
  113. /**
  114. * True if Mandriva(Mandrake) Linux was detected
  115. */
  116. public static final boolean IS_MANDRAKE_LINUX = IS_LINUX
  117. && FileUtil.fileContains(getReleaseFileName(), MANDRAKE);
  118. /**
  119. * True if Mandrake/Mandriva Linux was detected
  120. */
  121. public static final boolean IS_MANDRIVA_LINUX = (IS_LINUX
  122. && FileUtil.fileContains(getReleaseFileName(), MANDRIVA)) || IS_MANDRAKE_LINUX;
  123. /**
  124. * True if SuSE Linux was detected
  125. */
  126. public static final boolean IS_SUSE_LINUX = IS_LINUX
  127. && FileUtil.fileContains(getReleaseFileName(), SUSE, true); /* caseInsensitive , since 'SUSE' 10 */
  128. /**
  129. * True if Debian Linux or derived was detected
  130. */
  131. public static final boolean IS_DEBIAN_LINUX = (IS_LINUX
  132. && FileUtil.fileContains(PROC_VERSION, DEBIAN)) || (IS_LINUX && new File("/etc/debian_version").exists());
  133. // TODO detect the newcomer (K)Ubuntu */
  134. //~ Methods
  135. // **************************************************************************************************************************************************
  136. /**
  137. * Gets the etc Release Filename
  138. *
  139. * @return name of the file the release info is stored in for Linux distributions
  140. */
  141. private static String getReleaseFileName()
  142. {
  143. String result = "";
  144. File[] etcList = new File("/etc").listFiles();
  145. if (etcList != null)
  146. {
  147. for (File etcEntry : etcList)
  148. {
  149. if (etcEntry.isFile())
  150. {
  151. if (etcEntry.getName().endsWith("-release"))
  152. {
  153. //match :-)
  154. return result = etcEntry.toString();
  155. }
  156. }
  157. }
  158. }
  159. return result;
  160. }
  161. /**
  162. * Gets the Details of a Linux Distribution
  163. *
  164. * @return description string of the Linux distribution
  165. */
  166. private static String getLinuxDistribution()
  167. {
  168. String result = null;
  169. if (IS_SUSE_LINUX)
  170. {
  171. try
  172. {
  173. result = SUSE + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  174. }
  175. catch (IOException e)
  176. {
  177. // TODO ignore
  178. }
  179. }
  180. else if (IS_REDHAT_LINUX)
  181. {
  182. try
  183. {
  184. result = REDHAT + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  185. }
  186. catch (IOException e)
  187. {
  188. // TODO ignore
  189. }
  190. }
  191. else if (IS_FEDORA_LINUX)
  192. {
  193. try
  194. {
  195. result = FEDORA + SP + LINUX + NL
  196. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  197. }
  198. catch (IOException e)
  199. {
  200. // TODO ignore
  201. }
  202. }
  203. else if (IS_MANDRAKE_LINUX)
  204. {
  205. try
  206. {
  207. result = MANDRAKE + SP + LINUX + NL
  208. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  209. }
  210. catch (IOException e)
  211. {
  212. // TODO ignore
  213. }
  214. }
  215. else if (IS_MANDRIVA_LINUX)
  216. {
  217. try
  218. {
  219. result = MANDRIVA + SP + LINUX + NL
  220. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  221. }
  222. catch (IOException e)
  223. {
  224. // TODO ignore
  225. }
  226. }
  227. else if (IS_DEBIAN_LINUX)
  228. {
  229. try
  230. {
  231. result = DEBIAN + SP + LINUX + NL
  232. + StringTool.stringArrayListToString(FileUtil.getFileContent("/etc/debian_version"));
  233. }
  234. catch (IOException e)
  235. {
  236. // TODO ignore
  237. }
  238. }
  239. else
  240. {
  241. try
  242. {
  243. result = "Unknown Linux Distribution\n"
  244. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  245. }
  246. catch (IOException e)
  247. {
  248. // TODO ignore
  249. }
  250. }
  251. return result;
  252. }
  253. /**
  254. * returns a String which contains details of known OSs
  255. *
  256. * @return the details
  257. */
  258. public static String getOsDetails()
  259. {
  260. StringBuffer result = new StringBuffer();
  261. result.append("OS_NAME=").append(OS_NAME).append(NL);
  262. if (IS_UNIX)
  263. {
  264. if (IS_LINUX)
  265. {
  266. result.append(getLinuxDistribution()).append(NL);
  267. }
  268. else
  269. {
  270. try
  271. {
  272. result.append(FileUtil.getFileContent(getReleaseFileName())).append(NL);
  273. }
  274. catch (IOException e)
  275. {
  276. Debug.log("Unable to get release file contents in 'getOsDetails'.");
  277. }
  278. }
  279. }
  280. if (IS_WINDOWS)
  281. {
  282. result.append(System.getProperty(OSNAME)).append(SP).append(System.getProperty("sun.os.patch.level", "")).append(NL);
  283. }
  284. return result.toString();
  285. }
  286. /**
  287. * Testmain
  288. *
  289. * @param args Commandline Args
  290. */
  291. public static void main(String[] args)
  292. {
  293. System.out.println(getOsDetails());
  294. }
  295. }