PageRenderTime 1452ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

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