PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/com/jogamp/common/os/Platform.java

https://github.com/mbien/gluegen
Java | 254 lines | 142 code | 38 blank | 74 comment | 32 complexity | fef200afcad7827cb1a4cb45d2c3e3a9 MD5 | raw file
  1. /**
  2. * Copyright 2010 JogAmp Community. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without modification, are
  5. * permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this list of
  8. * conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  11. * of conditions and the following disclaimer in the documentation and/or other materials
  12. * provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * The views and conclusions contained in the software and documentation are those of the
  25. * authors and should not be interpreted as representing official policies, either expressed
  26. * or implied, of JogAmp Community.
  27. */
  28. /*
  29. * Created on Sunday, March 28 2010 14:43
  30. */
  31. package com.jogamp.common.os;
  32. import com.jogamp.common.nio.Buffers;
  33. import com.jogamp.gluegen.runtime.NativeLibLoader;
  34. import java.nio.ByteBuffer;
  35. import java.nio.IntBuffer;
  36. import java.nio.ShortBuffer;
  37. import java.security.AccessController;
  38. import java.security.PrivilegedAction;
  39. /**
  40. * Utility class for querying platform specific properties.
  41. * @author Michael Bien
  42. * @author Sven Gothel
  43. */
  44. public class Platform {
  45. public static final boolean JAVA_SE;
  46. public static final boolean LITTLE_ENDIAN;
  47. public static final String OS;
  48. public static final String OS_VERSION;
  49. public static final String ARCH;
  50. public static final String JAVA_VENDOR;
  51. public static final String JAVA_VENDOR_URL;
  52. public static final String JAVA_VERSION;
  53. public static final String NEWLINE;
  54. private static final boolean is32Bit;
  55. private static final int pointerSizeInBits;
  56. static {
  57. // We don't seem to need an AccessController.doPrivileged() block
  58. // here as these system properties are visible even to unsigned
  59. // applets
  60. OS = System.getProperty("os.name");
  61. OS_VERSION = System.getProperty("os.version");
  62. ARCH = System.getProperty("os.arch");
  63. JAVA_VENDOR = System.getProperty("java.vendor");
  64. JAVA_VENDOR_URL = System.getProperty("java.vendor.url");
  65. JAVA_VERSION = System.getProperty("java.version");
  66. NEWLINE = System.getProperty("line.separator");
  67. JAVA_SE = initIsJavaSE();
  68. LITTLE_ENDIAN = initByteOrder();
  69. boolean libsLoaded = true;
  70. try{
  71. NativeLibLoader.loadGlueGenRT();
  72. }catch (UnsatisfiedLinkError err){
  73. libsLoaded = false;
  74. }
  75. if(libsLoaded) {
  76. pointerSizeInBits = getPointerSizeInBitsImpl();
  77. }else{
  78. pointerSizeInBits = -1;
  79. }
  80. is32Bit = initArch();
  81. }
  82. private Platform() {}
  83. private static boolean initArch() throws RuntimeException {
  84. if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) {
  85. return 32 == pointerSizeInBits;
  86. }else {
  87. String os_lc = OS.toLowerCase();
  88. String arch_lc = ARCH.toLowerCase();
  89. if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) ||
  90. (os_lc.startsWith("windows") && arch_lc.equals("arm")) ||
  91. (os_lc.startsWith("linux") && arch_lc.equals("i386")) ||
  92. (os_lc.startsWith("linux") && arch_lc.equals("x86")) ||
  93. (os_lc.startsWith("mac os") && arch_lc.equals("ppc")) ||
  94. (os_lc.startsWith("mac os") && arch_lc.equals("i386")) ||
  95. (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) ||
  96. (os_lc.startsWith("darwin") && arch_lc.equals("i386")) ||
  97. (os_lc.startsWith("sunos") && arch_lc.equals("sparc")) ||
  98. (os_lc.startsWith("sunos") && arch_lc.equals("x86")) ||
  99. (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) ||
  100. (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) {
  101. return true;
  102. } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) ||
  103. (os_lc.startsWith("linux") && arch_lc.equals("amd64")) ||
  104. (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) ||
  105. (os_lc.startsWith("linux") && arch_lc.equals("ia64")) ||
  106. (os_lc.startsWith("mac os") && arch_lc.equals("x86_64")) ||
  107. (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) ||
  108. (os_lc.startsWith("sunos") && arch_lc.equals("sparcv9")) ||
  109. (os_lc.startsWith("sunos") && arch_lc.equals("amd64"))) {
  110. return false;
  111. }else{
  112. throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")");
  113. }
  114. }
  115. }
  116. private static boolean initIsJavaSE() {
  117. // the fast path, check property Java SE instead of traversing through the ClassLoader
  118. String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() {
  119. public Object run() {
  120. return System.getProperty("java.runtime.name");
  121. }
  122. });
  123. if(java_runtime_name.indexOf("Java SE") != -1) {
  124. return true;
  125. }
  126. // probe for classes we need on a SE environment
  127. try {
  128. Class.forName("java.nio.LongBuffer");
  129. Class.forName("java.nio.DoubleBuffer");
  130. return true;
  131. } catch(ClassNotFoundException ex) {
  132. // continue with Java SE check
  133. }
  134. return false;
  135. }
  136. private static boolean initByteOrder() {
  137. ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order
  138. IntBuffer tst_i = tst_b.asIntBuffer();
  139. ShortBuffer tst_s = tst_b.asShortBuffer();
  140. tst_i.put(0, 0x0A0B0C0D);
  141. return 0x0C0D == tst_s.get(0);
  142. }
  143. private static native int getPointerSizeInBitsImpl();
  144. /**
  145. * Returns true only if this program is running on the Java Standard Edition.
  146. */
  147. public static boolean isJavaSE() {
  148. return JAVA_SE;
  149. }
  150. /**
  151. * Returns true only if this system uses little endian byte ordering.
  152. */
  153. public static boolean isLittleEndian() {
  154. return LITTLE_ENDIAN;
  155. }
  156. /**
  157. * Returns the OS name.
  158. */
  159. public static String getOS() {
  160. return OS;
  161. }
  162. /**
  163. * Returns the OS version.
  164. */
  165. public static String getOSVersion() {
  166. return OS_VERSION;
  167. }
  168. /**
  169. * Returns the CPU architecture String.
  170. */
  171. public static String getArch() {
  172. return ARCH;
  173. }
  174. /**
  175. * Returns the JAVA.
  176. */
  177. public static String getJavaVendor() {
  178. return JAVA_VENDOR;
  179. }
  180. /**
  181. * Returns the JAVA vendor url.
  182. */
  183. public static String getJavaVendorURL() {
  184. return JAVA_VENDOR_URL;
  185. }
  186. /**
  187. * Returns the JAVA vendor.
  188. */
  189. public static String getJavaVersion() {
  190. return JAVA_VERSION;
  191. }
  192. /**
  193. * Returns the JAVA vendor.
  194. */
  195. public static String getNewline() {
  196. return NEWLINE;
  197. }
  198. /**
  199. * Returns true if this JVM is a 32bit JVM.
  200. */
  201. public static boolean is32Bit() {
  202. return is32Bit;
  203. }
  204. /**
  205. * Returns true if this JVM is a 64bit JVM.
  206. */
  207. public static boolean is64Bit() {
  208. return !is32Bit;
  209. }
  210. public static int getPointerSizeInBits() {
  211. return pointerSizeInBits;
  212. }
  213. public static int getPointerSizeInBytes() {
  214. return pointerSizeInBits/8;
  215. }
  216. }