PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/xuggle/ferry/JNIEnv.java

https://github.com/eix128/xuggle-xuggler
Java | 225 lines | 118 code | 13 blank | 94 comment | 31 complexity | cc0cdba1cc82f3138a81cf8691fa755d MD5 | raw file
Possible License(s): GPL-2.0, 0BSD, GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. /*******************************************************************************
  2. * Copyright (c) 2012, Art Clarke. All rights reserved.
  3. *
  4. * This file is part of Xuggle-Xuggler-Main.
  5. *
  6. * Xuggle-Xuggler-Main is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Xuggle-Xuggler-Main is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Xuggle-Xuggler-Main. If not, see <http://www.gnu.org/licenses/>.
  18. *******************************************************************************/
  19. package com.xuggle.ferry;
  20. /**
  21. * A set of utilities that can be used to find out
  22. * information about the Native environment that
  23. * we are running within.
  24. * @author aclarke
  25. *
  26. */
  27. public class JNIEnv
  28. {
  29. /**
  30. * The CPU architecture (i.e. chip architecture).
  31. * @author aclarke
  32. *
  33. */
  34. enum CPUArch{
  35. /** 32-bit systems such as the i386, i486, i586 or i686 */
  36. X86,
  37. /** 64-bit systems based on the x86 family */
  38. X86_64,
  39. /** 32-bit PowerPC based systems */
  40. PPC,
  41. /** 64-bit PowerPC based systems */
  42. PPC64,
  43. /** A chip architecture unknown to {@link Ferry}. */
  44. UNKNOWN,
  45. }
  46. /** The Operating system family */
  47. enum OSFamily {
  48. /** Linux family of operating systems */
  49. LINUX,
  50. /** Macintosh (darwin) family of operating systems */
  51. MAC,
  52. /** Microsoft Windows family of operating systems */
  53. WINDOWS,
  54. /** An unknown OS that {@link Ferry} knows nothing about */
  55. UNKNOWN,
  56. }
  57. /**
  58. * Our singleton.
  59. */
  60. private static final JNIEnv mEnv = new JNIEnv();
  61. private final CPUArch mCPUArch;
  62. private final OSFamily mOSFamily;
  63. /** Private. Only one {@link JNIEnv} is allowed per Java Virtual machine. */
  64. private JNIEnv() {
  65. mCPUArch = getCPUArch(System.getProperty("os.arch"));
  66. mOSFamily = getOSFamily(System.getProperty("os.name"));
  67. }
  68. /**
  69. * Get the (static) {@link JNIEnv} for this JVM.
  70. * @return the environment
  71. */
  72. public static JNIEnv getEnv() { return mEnv; }
  73. /**
  74. * Get the CPU architecture based on the passed in javaCPUArch specifier.
  75. *
  76. * The string should be of a format returned from {@link System#getProperty(String)}
  77. * for the property "os.arch".
  78. *
  79. * @param javaCPU the string to parse
  80. * @return the CPU architecture
  81. * @see System#getProperty(String)
  82. */
  83. public static CPUArch getCPUArch(String javaCPU)
  84. {
  85. final CPUArch javaArch;
  86. final String javaCPUArch = javaCPU != null ? javaCPU.toLowerCase() : "";
  87. // first parse the java arch
  88. if (javaCPUArch.startsWith("x86_64") ||
  89. javaCPUArch.startsWith("amd64") ||
  90. javaCPUArch.startsWith("ia64")) {
  91. javaArch = CPUArch.X86_64;
  92. } else if (
  93. javaCPUArch.startsWith("ppc64") ||
  94. javaCPUArch.startsWith("powerpc64")
  95. ) {
  96. javaArch = CPUArch.PPC64;
  97. } else if (
  98. javaCPUArch.startsWith("ppc") ||
  99. javaCPUArch.startsWith("powerpc")
  100. ) {
  101. javaArch = CPUArch.PPC;
  102. } else if (
  103. javaCPUArch.contains("86")
  104. )
  105. {
  106. javaArch = CPUArch.X86;
  107. } else {
  108. javaArch = CPUArch.UNKNOWN;
  109. }
  110. return javaArch;
  111. }
  112. /**
  113. * Return a CPUArch from parsing a GNU autoconf triple.
  114. *
  115. * For example "x86_64-w64-mingw32" will return {@link JNIEnv.CPUArch#X86_64}
  116. * and "ppc-apple-darwin" will return {@link JNIEnv.CPUArch#PPC}.
  117. *
  118. * @param gnuString the GNU string
  119. * @return the architecture
  120. */
  121. public static CPUArch getCPUArchFromGNUString(String gnuString)
  122. {
  123. final String nativeCpu = gnuString.toLowerCase();
  124. final CPUArch nativeArch;
  125. // then the native arch
  126. if (nativeCpu.startsWith("x86_64") ||
  127. nativeCpu.startsWith("amd64") ||
  128. nativeCpu.startsWith("ia64"))
  129. nativeArch = CPUArch.X86_64;
  130. else if (
  131. nativeCpu.startsWith("ppc64") ||
  132. nativeCpu.startsWith("powerpc64")
  133. )
  134. nativeArch = CPUArch.PPC64;
  135. else if (
  136. nativeCpu.startsWith("ppc") ||
  137. nativeCpu.startsWith("powerpc")
  138. )
  139. nativeArch = CPUArch.PPC;
  140. else if (
  141. nativeCpu.contains("86")
  142. )
  143. nativeArch = CPUArch.X86;
  144. else
  145. nativeArch = CPUArch.UNKNOWN;
  146. return nativeArch;
  147. }
  148. /**
  149. * Get the OSFamily based on the passed in osName specifier.
  150. *
  151. * The string should be of a format returned from {@link System#getProperty(String)}
  152. * for the property "os.name".
  153. *
  154. * @param osName the string to parse
  155. * @return the OSFamily
  156. * @see System#getProperty(String)
  157. */
  158. public static OSFamily getOSFamily(String osName)
  159. {
  160. final OSFamily retval;
  161. if (osName != null && osName.length() > 0)
  162. {
  163. if (osName.startsWith("Windows"))
  164. retval = OSFamily.WINDOWS;
  165. else if (osName.startsWith("Mac"))
  166. retval = OSFamily.MAC;
  167. else if (osName.startsWith("Linux"))
  168. retval = OSFamily.LINUX;
  169. else
  170. retval = OSFamily.UNKNOWN;
  171. } else
  172. retval = OSFamily.UNKNOWN;
  173. return retval;
  174. }
  175. /**
  176. * Return an OS Family from parsing a GNU autoconf triple.
  177. *
  178. * For example "x86_64-w64-mingw32" will return {@link JNIEnv.OSFamily#WINDOWS}
  179. * and "ppc-apple-darwin" will return {@link JNIEnv.OSFamily#MAC}.
  180. *
  181. * @param gnuString the GNU string
  182. * @return the OSFamily
  183. */
  184. public static OSFamily getOSFamilyFromGNUString(String gnuString)
  185. {
  186. final String nativeOs = (gnuString != null ? gnuString.toLowerCase() : "");
  187. final OSFamily retval;
  188. if (nativeOs.startsWith("mingw") || nativeOs.startsWith("cygwin"))
  189. retval = OSFamily.WINDOWS;
  190. else if (nativeOs.startsWith("darwin"))
  191. retval = OSFamily.MAC;
  192. else if (nativeOs.startsWith("linux"))
  193. retval = OSFamily.LINUX;
  194. else
  195. retval = OSFamily.UNKNOWN;
  196. return retval;
  197. }
  198. /**
  199. * @return the {@link JNIEnv.CPUArch} of this Java Virtual Machine.
  200. */
  201. public CPUArch getCPUArch()
  202. {
  203. return mCPUArch;
  204. }
  205. /**
  206. * @return the {@link JNIEnv.OSFamily} of this Java Virtual Machine.
  207. */
  208. public OSFamily getOSFamily()
  209. {
  210. return mOSFamily;
  211. }
  212. }