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

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

https://github.com/Domokun/gluegen
Java | 253 lines | 141 code | 38 blank | 74 comment | 32 complexity | 01828df343380a558d9d5890e43b3e8d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, CPL-1.0
  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 java.nio.ByteBuffer;
  34. import java.nio.IntBuffer;
  35. import java.nio.ShortBuffer;
  36. import java.security.AccessController;
  37. import java.security.PrivilegedAction;
  38. /**
  39. * Utility class for querying platform specific properties.
  40. * @author Michael Bien
  41. * @author Sven Gothel
  42. */
  43. public class Platform {
  44. public static final boolean JAVA_SE;
  45. public static final boolean LITTLE_ENDIAN;
  46. public static final String OS;
  47. public static final String OS_VERSION;
  48. public static final String ARCH;
  49. public static final String JAVA_VENDOR;
  50. public static final String JAVA_VENDOR_URL;
  51. public static final String JAVA_VERSION;
  52. public static final String NEWLINE;
  53. private static final boolean is32Bit;
  54. private static final int pointerSizeInBits;
  55. static {
  56. // We don't seem to need an AccessController.doPrivileged() block
  57. // here as these system properties are visible even to unsigned
  58. // applets
  59. OS = System.getProperty("os.name");
  60. OS_VERSION = System.getProperty("os.version");
  61. ARCH = System.getProperty("os.arch");
  62. JAVA_VENDOR = System.getProperty("java.vendor");
  63. JAVA_VENDOR_URL = System.getProperty("java.vendor.url");
  64. JAVA_VERSION = System.getProperty("java.version");
  65. NEWLINE = System.getProperty("line.separator");
  66. JAVA_SE = initIsJavaSE();
  67. LITTLE_ENDIAN = initByteOrder();
  68. boolean libsLoaded = true;
  69. try{
  70. NativeLibrary.ensureNativeLibLoaded();
  71. }catch (UnsatisfiedLinkError err){
  72. libsLoaded = false;
  73. }
  74. if(libsLoaded) {
  75. pointerSizeInBits = getPointerSizeInBitsImpl();
  76. }else{
  77. pointerSizeInBits = -1;
  78. }
  79. is32Bit = initArch();
  80. }
  81. private Platform() {}
  82. private static boolean initArch() throws RuntimeException {
  83. if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) {
  84. return 32 == pointerSizeInBits;
  85. }else {
  86. String os_lc = OS.toLowerCase();
  87. String arch_lc = ARCH.toLowerCase();
  88. if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) ||
  89. (os_lc.startsWith("windows") && arch_lc.equals("arm")) ||
  90. (os_lc.startsWith("linux") && arch_lc.equals("i386")) ||
  91. (os_lc.startsWith("linux") && arch_lc.equals("x86")) ||
  92. (os_lc.startsWith("mac os") && arch_lc.equals("ppc")) ||
  93. (os_lc.startsWith("mac os") && arch_lc.equals("i386")) ||
  94. (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) ||
  95. (os_lc.startsWith("darwin") && arch_lc.equals("i386")) ||
  96. (os_lc.startsWith("sunos") && arch_lc.equals("sparc")) ||
  97. (os_lc.startsWith("sunos") && arch_lc.equals("x86")) ||
  98. (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) ||
  99. (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) {
  100. return true;
  101. } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) ||
  102. (os_lc.startsWith("linux") && arch_lc.equals("amd64")) ||
  103. (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) ||
  104. (os_lc.startsWith("linux") && arch_lc.equals("ia64")) ||
  105. (os_lc.startsWith("mac os") && arch_lc.equals("x86_64")) ||
  106. (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) ||
  107. (os_lc.startsWith("sunos") && arch_lc.equals("sparcv9")) ||
  108. (os_lc.startsWith("sunos") && arch_lc.equals("amd64"))) {
  109. return false;
  110. }else{
  111. throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")");
  112. }
  113. }
  114. }
  115. private static boolean initIsJavaSE() {
  116. // the fast path, check property Java SE instead of traversing through the ClassLoader
  117. String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() {
  118. public Object run() {
  119. return System.getProperty("java.runtime.name");
  120. }
  121. });
  122. if(java_runtime_name.indexOf("Java SE") != -1) {
  123. return true;
  124. }
  125. // probe for classes we need on a SE environment
  126. try {
  127. Class.forName("java.nio.LongBuffer");
  128. Class.forName("java.nio.DoubleBuffer");
  129. return true;
  130. } catch(ClassNotFoundException ex) {
  131. // continue with Java SE check
  132. }
  133. return false;
  134. }
  135. private static boolean initByteOrder() {
  136. ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order
  137. IntBuffer tst_i = tst_b.asIntBuffer();
  138. ShortBuffer tst_s = tst_b.asShortBuffer();
  139. tst_i.put(0, 0x0A0B0C0D);
  140. return 0x0C0D == tst_s.get(0);
  141. }
  142. private static native int getPointerSizeInBitsImpl();
  143. /**
  144. * Returns true only if this program is running on the Java Standard Edition.
  145. */
  146. public static boolean isJavaSE() {
  147. return JAVA_SE;
  148. }
  149. /**
  150. * Returns true only if this system uses little endian byte ordering.
  151. */
  152. public static boolean isLittleEndian() {
  153. return LITTLE_ENDIAN;
  154. }
  155. /**
  156. * Returns the OS name.
  157. */
  158. public static String getOS() {
  159. return OS;
  160. }
  161. /**
  162. * Returns the OS version.
  163. */
  164. public static String getOSVersion() {
  165. return OS_VERSION;
  166. }
  167. /**
  168. * Returns the CPU architecture String.
  169. */
  170. public static String getArch() {
  171. return ARCH;
  172. }
  173. /**
  174. * Returns the JAVA.
  175. */
  176. public static String getJavaVendor() {
  177. return JAVA_VENDOR;
  178. }
  179. /**
  180. * Returns the JAVA vendor url.
  181. */
  182. public static String getJavaVendorURL() {
  183. return JAVA_VENDOR_URL;
  184. }
  185. /**
  186. * Returns the JAVA vendor.
  187. */
  188. public static String getJavaVersion() {
  189. return JAVA_VERSION;
  190. }
  191. /**
  192. * Returns the JAVA vendor.
  193. */
  194. public static String getNewline() {
  195. return NEWLINE;
  196. }
  197. /**
  198. * Returns true if this JVM is a 32bit JVM.
  199. */
  200. public static boolean is32Bit() {
  201. return is32Bit;
  202. }
  203. /**
  204. * Returns true if this JVM is a 64bit JVM.
  205. */
  206. public static boolean is64Bit() {
  207. return !is32Bit;
  208. }
  209. public static int getPointerSizeInBits() {
  210. return pointerSizeInBits;
  211. }
  212. public static int getPointerSizeInBytes() {
  213. return pointerSizeInBits/8;
  214. }
  215. }