PageRenderTime 1265ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jponge/izpack-full-svn-history-copy
Java | 267 lines | 160 code | 34 blank | 73 comment | 30 complexity | fe364275567629bc8df331637f633cbf MD5 | raw file
  1. /*
  2. * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
  3. *
  4. * http://izpack.org/ http://developer.berlios.de/projects/izpack/
  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. *
  24. * Created at: Date: Nov 9, 2004 Time: 8:53:22 PM
  25. *
  26. * @author hani, Marc.Eppelmann@reddot.de
  27. */
  28. public final class OsVersion implements OsVersionConstants, StringConstants
  29. {
  30. //~ Static fields/initializers
  31. // *******************************************************************************************************************************
  32. /** OS_NAME = System.getProperty( "os.name" ) */
  33. public static final String OS_NAME = System.getProperty( OSNAME );
  34. /** True if this is FreeBSD. */
  35. public static final boolean IS_FREEBSD = StringTool.startsWithIgnoreCase(OS_NAME, FREEBSD );
  36. /** True if this is Linux. */
  37. public static final boolean IS_LINUX = StringTool.startsWithIgnoreCase(OS_NAME, LINUX );
  38. /** True if this is HP-UX. */
  39. public static final boolean IS_HPUX = StringTool.startsWithIgnoreCase(OS_NAME, HP_UX );
  40. /** True if this is AIX. */
  41. public static final boolean IS_AIX = StringTool.startsWithIgnoreCase(OS_NAME, AIX );
  42. /** True if this is SunOS. */
  43. public static final boolean IS_SUNOS = StringTool.startsWithIgnoreCase(OS_NAME, SUNOS );
  44. /** True if this is OS/2. */
  45. public static final boolean IS_OS2 = StringTool.startsWith(OS_NAME, OS_2 );
  46. /** True is this is Mac OS */
  47. public static final boolean IS_MAC = StringTool.startsWith(OS_NAME, MAC );
  48. /** True if this is the Mac OS X. */
  49. public static final boolean IS_OSX = StringTool.startsWithIgnoreCase(OS_NAME, MACOSX);
  50. /** True if this is Windows. */
  51. public static final boolean IS_WINDOWS = StringTool.startsWith(OS_NAME, WINDOWS );
  52. /** True if this is some variant of Unix (OSX, Linux, Solaris, FreeBSD, etc). */
  53. public static final boolean IS_UNIX = !IS_OS2 && !IS_WINDOWS;
  54. /** True if RedHat Linux was detected */
  55. public static final boolean IS_REDHAT_LINUX = IS_LINUX
  56. && ( ( FileUtil.fileContains(getReleaseFileName(), REDHAT ) || FileUtil.fileContains(getReleaseFileName() ,
  57. RED_HAT ) ) );
  58. /** True if Fedora Linux was detected */
  59. public static final boolean IS_FEDORA_LINUX = IS_LINUX
  60. && FileUtil.fileContains(getReleaseFileName(), FEDORA );
  61. /** True if Mandriva(Mandrake) Linux was detected */
  62. public static final boolean IS_MANDRAKE_LINUX = IS_LINUX
  63. && FileUtil.fileContains( getReleaseFileName(), MANDRAKE );
  64. /** True if Mandrake/Mandriva Linux was detected */
  65. public static final boolean IS_MANDRIVA_LINUX = ( IS_LINUX
  66. && FileUtil.fileContains( getReleaseFileName(), MANDRIVA ) ) || IS_MANDRAKE_LINUX;
  67. /** True if SuSE Linux was detected */
  68. public static final boolean IS_SUSE_LINUX = IS_LINUX
  69. && FileUtil.fileContains( getReleaseFileName(), SUSE, true ); /* caseInsensitive , since 'SUSE' 10 */
  70. /** True if Debian Linux or derived was detected */
  71. public static final boolean IS_DEBIAN_LINUX = (IS_LINUX
  72. && FileUtil.fileContains(PROC_VERSION, DEBIAN )) || ( IS_LINUX && new File( "/etc/debian_version" ).exists() );
  73. // TODO detect the newcomer (K)Ubuntu */
  74. //~ Methods
  75. // **************************************************************************************************************************************************
  76. /**
  77. * Gets the etc Release Filename
  78. *
  79. * @return name of the file the release info is stored in for Linux distributions
  80. */
  81. private static String getReleaseFileName()
  82. {
  83. String result = "";
  84. File[] etcList = new File("/etc").listFiles();
  85. if( etcList != null )
  86. for (int idx = 0; idx < etcList.length; idx++)
  87. {
  88. File etcEntry = etcList[idx];
  89. if (etcEntry.isFile())
  90. {
  91. if (etcEntry.getName().endsWith("-release"))
  92. {
  93. //match :-)
  94. return result = etcEntry.toString();
  95. }
  96. }
  97. }
  98. return result;
  99. }
  100. /**
  101. * Gets the Details of a Linux Distribution
  102. *
  103. * @return description string of the Linux distribution
  104. */
  105. private static String getLinuxDistribution()
  106. {
  107. String result = null;
  108. if (IS_SUSE_LINUX)
  109. {
  110. try
  111. {
  112. result = SUSE + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  113. }
  114. catch (IOException e)
  115. {
  116. // TODO ignore
  117. }
  118. }
  119. else if (IS_REDHAT_LINUX)
  120. {
  121. try
  122. {
  123. result = REDHAT + SP + LINUX + NL + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  124. }
  125. catch (IOException e)
  126. {
  127. // TODO ignore
  128. }
  129. }
  130. else if (IS_FEDORA_LINUX)
  131. {
  132. try
  133. {
  134. result = FEDORA + SP + LINUX + NL
  135. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  136. }
  137. catch (IOException e)
  138. {
  139. // TODO ignore
  140. }
  141. }
  142. else if (IS_MANDRAKE_LINUX)
  143. {
  144. try
  145. {
  146. result = MANDRAKE + SP + LINUX + NL
  147. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  148. }
  149. catch (IOException e)
  150. {
  151. // TODO ignore
  152. }
  153. }
  154. else if (IS_MANDRIVA_LINUX)
  155. {
  156. try
  157. {
  158. result = MANDRIVA + SP + LINUX + NL
  159. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  160. }
  161. catch (IOException e)
  162. {
  163. // TODO ignore
  164. }
  165. }
  166. else if (IS_DEBIAN_LINUX)
  167. {
  168. try
  169. {
  170. result = DEBIAN + SP + LINUX + NL
  171. + StringTool.stringArrayListToString(FileUtil.getFileContent("/etc/debian_version"));
  172. }
  173. catch (IOException e)
  174. {
  175. // TODO ignore
  176. }
  177. }
  178. else
  179. {
  180. try
  181. {
  182. result = "Unknown Linux Distribution\n"
  183. + StringTool.stringArrayListToString(FileUtil.getFileContent(getReleaseFileName()));
  184. }
  185. catch (IOException e)
  186. {
  187. // TODO ignore
  188. }
  189. }
  190. return result;
  191. }
  192. /**
  193. * returns a String which contains details of known OSs
  194. * @return the details
  195. */
  196. public static String getOsDetails()
  197. {
  198. StringBuffer result = new StringBuffer();
  199. result.append("OS_NAME=").append(OS_NAME).append(NL);
  200. if( IS_UNIX )
  201. {
  202. if( IS_LINUX )
  203. {
  204. result.append(getLinuxDistribution()).append(NL);
  205. }
  206. else
  207. {
  208. try
  209. {
  210. result.append(FileUtil.getFileContent(getReleaseFileName())).append(NL);
  211. }
  212. catch (IOException e)
  213. {
  214. Debug.log("Unable to get release file contents in 'getOsDetails'.");
  215. }
  216. }
  217. }
  218. if( IS_WINDOWS )
  219. {
  220. result.append(System.getProperty(OSNAME)).append(SP).append(System.getProperty("sun.os.patch.level", "")).append(NL);
  221. }
  222. return result.toString();
  223. }
  224. /**
  225. * Testmain
  226. *
  227. * @param args Commandline Args
  228. */
  229. public static void main(String[] args)
  230. {
  231. System.out.println( getOsDetails() );
  232. }
  233. }