diff options
Diffstat (limited to 'src/java/com/jogamp/common/os')
-rwxr-xr-x | src/java/com/jogamp/common/os/DynamicLinker.java | 50 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/os/DynamicLookupHelper.java | 53 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java | 58 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/os/NativeLibrary.java | 426 | ||||
-rw-r--r-- | src/java/com/jogamp/common/os/Platform.java | 163 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java | 64 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java | 47 |
7 files changed, 861 insertions, 0 deletions
diff --git a/src/java/com/jogamp/common/os/DynamicLinker.java b/src/java/com/jogamp/common/os/DynamicLinker.java new file mode 100755 index 0000000..ef5d5dc --- /dev/null +++ b/src/java/com/jogamp/common/os/DynamicLinker.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.jogamp.common.os; + +/** Provides an abstract interface to the OS's low-level dynamic + linking functionality. */ + +interface DynamicLinker { + public long openLibraryGlobal(String pathname, boolean debug); + public long openLibraryLocal(String pathname, boolean debug); + public long lookupSymbol(long libraryHandle, String symbolName); + public void closeLibrary(long libraryHandle); +} diff --git a/src/java/com/jogamp/common/os/DynamicLookupHelper.java b/src/java/com/jogamp/common/os/DynamicLookupHelper.java new file mode 100755 index 0000000..a4f74a4 --- /dev/null +++ b/src/java/com/jogamp/common/os/DynamicLookupHelper.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2003-2005 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.jogamp.common.os; + +/** Interface callers may use to use the ProcAddressHelper's {@link + ProcAddressHelper#resetProcAddressTable resetProcAddressTable} + helper method to install function pointers into a + ProcAddressTable. This must typically be written with native + code. */ + +public interface DynamicLookupHelper { + /** + * Try to fetch the function pointer for function 'funcName'. + */ + public long dynamicLookupFunction(String funcName); +} diff --git a/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java new file mode 100755 index 0000000..531bc5c --- /dev/null +++ b/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java @@ -0,0 +1,58 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:27:00 PDT 2006 ----! */ + +package com.jogamp.common.os; + + +public class MacOSXDynamicLinkerImpl implements DynamicLinker { + + public static final int RTLD_LAZY = 0x1; + public static final int RTLD_NOW = 0x2; + public static final int RTLD_LOCAL = 0x4; + public static final int RTLD_GLOBAL = 0x8; + + /** Interface to C language function: <br> <code> int dlclose(void * __handle); </code> */ + private static native int dlclose(long __handle); + + /** Interface to C language function: <br> <code> char * dlerror(void); </code> */ + private static native java.lang.String dlerror(); + + /** Interface to C language function: <br> <code> void * dlopen(const char * __path, int __mode); </code> */ + private static native long dlopen(java.lang.String __path, int __mode); + + /** Interface to C language function: <br> <code> void * dlsym(void * __handle, const char * __symbol); </code> */ + private static native long dlsym(long __handle, java.lang.String __symbol); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibraryLocal(String pathname, boolean debug) { + // Note we use RTLD_LOCAL visibility to _NOT_ allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_LOCAL); + } + + public long openLibraryGlobal(String pathname, boolean debug) { + // Note we use RTLD_GLOBAL visibility to allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_GLOBAL); + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return dlsym(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + dlclose(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class MacOSXDynamicLinkerImpl diff --git a/src/java/com/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java new file mode 100755 index 0000000..2de8bc9 --- /dev/null +++ b/src/java/com/jogamp/common/os/NativeLibrary.java @@ -0,0 +1,426 @@ +/* + * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.jogamp.common.os; + +import com.jogamp.gluegen.runtime.NativeLibLoader; +import java.io.*; +import java.lang.reflect.*; +import java.security.*; +import java.util.*; + +/** Provides low-level, relatively platform-independent access to + shared ("native") libraries. The core library routines + <code>System.load()</code> and <code>System.loadLibrary()</code> + in general provide suitable functionality for applications using + native code, but are not flexible enough to support certain kinds + of glue code generation and deployment strategies. This class + supports direct linking of native libraries to other shared + objects not necessarily installed on the system (in particular, + via the use of dlopen(RTLD_GLOBAL) on Unix platforms) as well as + manual lookup of function names to support e.g. GlueGen's + ProcAddressTable glue code generation style without additional + supporting code needed in the generated library. */ + +public class NativeLibrary implements DynamicLookupHelper { + private static final int WINDOWS = 1; + private static final int UNIX = 2; + private static final int MACOSX = 3; + private static boolean DEBUG; + private static int platform; + private static DynamicLinker dynLink; + private static String[] prefixes; + private static String[] suffixes; + + static { + // Determine platform we're running on + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.startsWith("wind")) { + platform = WINDOWS; + } else if (osName.startsWith("mac os x")) { + platform = MACOSX; + } else { + platform = UNIX; + } + + DEBUG = (System.getProperty("gluegen.debug.NativeLibrary") != null); + + return null; + } + }); + // Instantiate dynamic linker implementation + switch (platform) { + case WINDOWS: + dynLink = new WindowsDynamicLinkerImpl(); + prefixes = new String[] { "" }; + suffixes = new String[] { ".dll" }; + break; + case UNIX: + dynLink = new UnixDynamicLinkerImpl(); + prefixes = new String[] { "lib" }; + suffixes = new String[] { ".so" }; + break; + case MACOSX: + dynLink = new MacOSXDynamicLinkerImpl(); + prefixes = new String[] { "lib", "" }; + suffixes = new String[] { ".dylib", ".jnilib", "" }; + break; + default: + throw new InternalError("Platform not initialized properly"); + } + } + + // Platform-specific representation for the handle to the open + // library. This is an HMODULE on Windows and a void* (the result of + // a dlopen() call) on Unix and Mac OS X platforms. + private long libraryHandle; + + // May as well keep around the path to the library we opened + private String libraryPath; + + // Private constructor to prevent arbitrary instances from floating around + private NativeLibrary(long libraryHandle, String libraryPath) { + this.libraryHandle = libraryHandle; + this.libraryPath = libraryPath; + } + + /** Opens the given native library, assuming it has the same base + name on all platforms, looking first in the system's search + path, and in the context of the specified ClassLoader, which is + used to help find the library in the case of e.g. Java Web Start. */ + public static NativeLibrary open(String libName, ClassLoader loader) { + return open(libName, libName, libName, true, loader, true); + } + + /** Opens the given native library, assuming it has the same base + name on all platforms, looking first in the system's search + path, and in the context of the specified ClassLoader, which is + used to help find the library in the case of e.g. Java Web Start. */ + public static NativeLibrary open(String libName, ClassLoader loader, boolean global) { + return open(libName, libName, libName, true, loader, global); + } + + /** Opens the given native library, assuming it has the given base + names (no "lib" prefix or ".dll/.so/.dylib" suffix) on the + Windows, Unix and Mac OS X platforms, respectively, and in the + context of the specified ClassLoader, which is used to help find + the library in the case of e.g. Java Web Start. The + searchSystemPathFirst argument changes the behavior to first + search the default system path rather than searching it last. + Note that we do not currently handle DSO versioning on Unix. + Experience with JOAL and OpenAL has shown that it is extremely + problematic to rely on a specific .so version (for one thing, + ClassLoader.findLibrary on Unix doesn't work with files not + ending in .so, for example .so.0), and in general if this + dynamic loading facility is used correctly the version number + will be irrelevant. + */ + public static NativeLibrary open(String windowsLibName, + String unixLibName, + String macOSXLibName, + boolean searchSystemPathFirst, + ClassLoader loader) { + return open(windowsLibName, unixLibName, macOSXLibName, searchSystemPathFirst, loader, true); + } + + public static NativeLibrary open(String windowsLibName, + String unixLibName, + String macOSXLibName, + boolean searchSystemPathFirst, + ClassLoader loader, boolean global) { + List possiblePaths = enumerateLibraryPaths(windowsLibName, + unixLibName, + macOSXLibName, + searchSystemPathFirst, + loader); + // Iterate down these and see which one if any we can actually find. + for (Iterator iter = possiblePaths.iterator(); iter.hasNext(); ) { + String path = (String) iter.next(); + if (DEBUG) { + System.out.println("Trying to load " + path); + } + ensureNativeLibLoaded(); + long res; + if(global) { + res = dynLink.openLibraryGlobal(path, DEBUG); + } else { + res = dynLink.openLibraryLocal(path, DEBUG); + } + if (res != 0) { + if (DEBUG) { + System.out.println("Successfully loaded " + path + ": res = 0x" + Long.toHexString(res)); + } + return new NativeLibrary(res, path); + } + } + + if (DEBUG) { + System.out.println("Did not succeed in loading (" + windowsLibName + ", " + unixLibName + ", " + macOSXLibName + ")"); + } + + // For now, just return null to indicate the open operation didn't + // succeed (could also throw an exception if we could tell which + // of the openLibrary operations actually failed) + return null; + } + + /** Looks up the given function name in this native library. */ + public long dynamicLookupFunction(String funcName) { + if (libraryHandle == 0) + throw new RuntimeException("Library is not open"); + return dynLink.lookupSymbol(libraryHandle, funcName); + } + + /** Retrieves the low-level library handle from this NativeLibrary + object. On the Windows platform this is an HMODULE, and on Unix + and Mac OS X platforms the void* result of calling dlopen(). */ + public long getLibraryHandle() { + return libraryHandle; + } + + /** Retrieves the path under which this library was opened. */ + public String getLibraryPath() { + return libraryPath; + } + + /** Closes this native library. Further lookup operations are not + allowed after calling this method. */ + public void close() { + if (libraryHandle == 0) + throw new RuntimeException("Library already closed"); + long handle = libraryHandle; + libraryHandle = 0; + dynLink.closeLibrary(handle); + } + + /** Given the base library names (no prefixes/suffixes) for the + various platforms, enumerate the possible locations and names of + the indicated native library on the system. */ + private static List enumerateLibraryPaths(String windowsLibName, + String unixLibName, + String macOSXLibName, + boolean searchSystemPathFirst, + ClassLoader loader) { + List paths = new ArrayList(); + String libName = selectName(windowsLibName, unixLibName, macOSXLibName); + if (libName == null) + return paths; + + // Allow user's full path specification to override our building of paths + File file = new File(libName); + if (file.isAbsolute()) { + paths.add(libName); + return paths; + } + + String[] baseNames = buildNames(libName); + + if (searchSystemPathFirst) { + // Add just the library names to use the OS's search algorithm + for (int i = 0; i < baseNames.length; i++) { + paths.add(baseNames[i]); + } + } + + // The idea to ask the ClassLoader to find the library is borrowed + // from the LWJGL library + String clPath = getPathFromClassLoader(libName, loader); + if (DEBUG) { + System.out.println("Class loader path to " + libName + ": " + clPath); + } + if (clPath != null) { + paths.add(clPath); + } + + // Add entries from java.library.path + String javaLibraryPath = + (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return System.getProperty("java.library.path"); + } + }); + if (javaLibraryPath != null) { + StringTokenizer tokenizer = new StringTokenizer(javaLibraryPath, File.pathSeparator); + while (tokenizer.hasMoreTokens()) { + addPaths(tokenizer.nextToken(), baseNames, paths); + } + } + + // Add current working directory + String userDir = + (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return System.getProperty("user.dir"); + } + }); + addPaths(userDir, baseNames, paths); + + // Add probable Mac OS X-specific paths + if (platform == MACOSX) { + // Add historical location + addPaths("/Library/Frameworks/" + libName + ".Framework", baseNames, paths); + // Add current location + addPaths("/System/Library/Frameworks/" + libName + ".Framework", baseNames, paths); + } + + if (!searchSystemPathFirst) { + // Add just the library names to use the OS's search algorithm + for (int i = 0; i < baseNames.length; i++) { + paths.add(baseNames[i]); + } + } + + return paths; + } + + + private static String selectName(String windowsLibName, + String unixLibName, + String macOSXLibName) { + switch (platform) { + case WINDOWS: + return windowsLibName; + case UNIX: + return unixLibName; + case MACOSX: + return macOSXLibName; + default: + throw new InternalError(); + } + } + + private static String[] buildNames(String libName) { + // If the library name already has the prefix / suffix added + // (principally because we want to force a version number on Unix + // operating systems) then just return the library name. + if (libName.startsWith(prefixes[0])) { + if (libName.endsWith(suffixes[0])) { + return new String[] { libName }; + } + + int idx = libName.indexOf(suffixes[0]); + boolean ok = true; + if (idx >= 0) { + // Check to see if everything after it is a Unix version number + for (int i = idx + suffixes[0].length(); + i < libName.length(); + i++) { + char c = libName.charAt(i); + if (!(c == '.' || (c >= '0' && c <= '9'))) { + ok = false; + break; + } + } + if (ok) { + return new String[] { libName }; + } + } + } + + String[] res = new String[prefixes.length * suffixes.length]; + int idx = 0; + for (int i = 0; i < prefixes.length; i++) { + for (int j = 0; j < suffixes.length; j++) { + res[idx++] = prefixes[i] + libName + suffixes[j]; + } + } + return res; + } + + private static void addPaths(String path, String[] baseNames, List paths) { + for (int j = 0; j < baseNames.length; j++) { + paths.add(path + File.separator + baseNames[j]); + } + } + + private static boolean initializedFindLibraryMethod = false; + private static Method findLibraryMethod = null; + private static String getPathFromClassLoader(final String libName, final ClassLoader loader) { + if (loader == null) + return null; + if (!initializedFindLibraryMethod) { + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + try { + findLibraryMethod = ClassLoader.class.getDeclaredMethod("findLibrary", + new Class[] { String.class }); + findLibraryMethod.setAccessible(true); + } catch (Exception e) { + // Fail silently disabling this functionality + } + initializedFindLibraryMethod = true; + return null; + } + }); + } + if (findLibraryMethod != null) { + try { + return (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + try { + return findLibraryMethod.invoke(loader, new Object[] { libName }); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }); + } catch (Exception e) { + if (DEBUG) { + e.printStackTrace(); + } + // Fail silently and continue with other search algorithms + } + } + return null; + } + + private static volatile boolean loadedDynLinkNativeLib; + public static void ensureNativeLibLoaded() { + if (!loadedDynLinkNativeLib) { + synchronized (NativeLibrary.class) { + if (!loadedDynLinkNativeLib) { + loadedDynLinkNativeLib = true; + NativeLibLoader.loadGlueGenRT(); + } + } + } + } +} diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java new file mode 100644 index 0000000..169af13 --- /dev/null +++ b/src/java/com/jogamp/common/os/Platform.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2010, Michael Bien, Sven Gothel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Michael Bien BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Created on Sunday, March 28 2010 14:43 + */ +package com.jogamp.common.os; + +import com.jogamp.common.nio.Buffers; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.nio.ShortBuffer; + +/** + * Utility class for querying platform specific properties. + * @author Michael Bien, Sven Gothel + */ +public class Platform { + + public static final boolean JAVA_SE; + public static final boolean LITTLE_ENDIAN; + + private final static boolean is32Bit; + private final static int pointerSizeInBits; + private final static String os, arch; + + static { + NativeLibrary.ensureNativeLibLoaded(); + + // We don't seem to need an AccessController.doPrivileged() block + // here as these system properties are visible even to unsigned + // applets + os = System.getProperty("os.name"); + arch = System.getProperty("os.arch"); + + pointerSizeInBits = getPointerSizeInBitsImpl(); + + // Try to use Sun's sun.arch.data.model first .. + if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) { + is32Bit = ( 32 == pointerSizeInBits ); + }else { + String os_lc = os.toLowerCase(); + String arch_lc = arch.toLowerCase(); + + if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) || + (os_lc.startsWith("windows") && arch_lc.equals("arm")) || + (os_lc.startsWith("linux") && arch_lc.equals("i386")) || + (os_lc.startsWith("linux") && arch_lc.equals("x86")) || + (os_lc.startsWith("mac os_lc") && arch_lc.equals("ppc")) || + (os_lc.startsWith("mac os_lc") && arch_lc.equals("i386")) || + (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) || + (os_lc.startsWith("darwin") && arch_lc.equals("i386")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparc")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("x86")) || + (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) || + (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) { + is32Bit = true; + } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) || + (os_lc.startsWith("linux") && arch_lc.equals("amd64")) || + (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) || + (os_lc.startsWith("linux") && arch_lc.equals("ia64")) || + (os_lc.startsWith("mac os_lc") && arch_lc.equals("x86_64")) || + (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparcv9")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("amd64"))) { + is32Bit = false; + }else{ + throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")"); + } + } + + // fast path + boolean se = System.getProperty("java.runtime.name").indexOf("Java SE") != -1; + + if(!se) { + try{ + Class.forName("java.nio.LongBuffer"); + Class.forName("java.nio.DoubleBuffer"); + se = true; + }catch(ClassNotFoundException ex) { + se = false; + } + } + JAVA_SE = se; + + // byte order + ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order + IntBuffer tst_i = tst_b.asIntBuffer(); + ShortBuffer tst_s = tst_b.asShortBuffer(); + tst_i.put(0, 0x0A0B0C0D); + LITTLE_ENDIAN = 0x0C0D == tst_s.get(0); + } + + private Platform() {} + + private static native int getPointerSizeInBitsImpl(); + + /** + * Returns true only if this program is running on the Java Standard Edition. + */ + public static boolean isJavaSE() { + return JAVA_SE; + } + + /** + * Returns true only if this system uses little endian byte ordering. + */ + public static boolean isLittleEndian() { + return LITTLE_ENDIAN; + } + + /** + * Returns the OS name. + */ + public static String getOS() { + return os; + } + + /** + * Returns the CPU architecture String. + */ + public static String getArch() { + return arch; + } + + /** + * Returns true if this JVM is a 32bit JVM. + */ + public static boolean is32Bit() { + return is32Bit; + } + + public static int getPointerSizeInBits() { + return pointerSizeInBits; + } + +} + + diff --git a/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java new file mode 100755 index 0000000..02bc828 --- /dev/null +++ b/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java @@ -0,0 +1,64 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:26:59 PDT 2006 ----! */ + +package com.jogamp.common.os; + + +public class UnixDynamicLinkerImpl implements DynamicLinker { + + public static final int RTLD_LAZY = 0x00001; + public static final int RTLD_NOW = 0x00002; + public static final int RTLD_NOLOAD = 0x00004; + public static final int RTLD_GLOBAL = 0x00100; + public static final int RTLD_LOCAL = 0x00000; + public static final int RTLD_PARENT = 0x00200; + public static final int RTLD_GROUP = 0x00400; + public static final int RTLD_WORLD = 0x00800; + public static final int RTLD_NODELETE = 0x01000; + public static final int RTLD_FIRST = 0x02000; + + /** Interface to C language function: <br> <code> int dlclose(void * ); </code> */ + private static native int dlclose(long arg0); + + /** Interface to C language function: <br> <code> char * dlerror(void); </code> */ + private static native java.lang.String dlerror(); + + /** Interface to C language function: <br> <code> void * dlopen(const char * , int); </code> */ + private static native long dlopen(java.lang.String arg0, int arg1); + + /** Interface to C language function: <br> <code> void * dlsym(void * , const char * ); </code> */ + private static native long dlsym(long arg0, java.lang.String arg1); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibraryLocal(String pathname, boolean debug) { + // Note we use RTLD_GLOBAL visibility to _NOT_ allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_LOCAL); + } + + public long openLibraryGlobal(String pathname, boolean debug) { + // Note we use RTLD_GLOBAL visibility to allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_GLOBAL); + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return dlsym(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + dlclose(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class UnixDynamicLinkerImpl diff --git a/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java new file mode 100755 index 0000000..f5a3312 --- /dev/null +++ b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java @@ -0,0 +1,47 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Tue May 27 02:37:55 PDT 2008 ----! */ + +package com.jogamp.common.os; + + +public class WindowsDynamicLinkerImpl implements DynamicLinker { + + + /** Interface to C language function: <br> <code> BOOL FreeLibrary(HANDLE hLibModule); </code> */ + private static native int FreeLibrary(long hLibModule); + + /** Interface to C language function: <br> <code> DWORD GetLastError(void); </code> */ + private static native int GetLastError(); + + /** Interface to C language function: <br> <code> PROC GetProcAddressA(HANDLE hModule, LPCSTR lpProcName); </code> */ + private static native long GetProcAddressA(long hModule, java.lang.String lpProcName); + + /** Interface to C language function: <br> <code> HANDLE LoadLibraryW(LPCWSTR lpLibFileName); </code> */ + private static native long LoadLibraryW(java.lang.String lpLibFileName); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibraryLocal(String libraryName, boolean debug) { + // How does that work under Windows ? + // Don't know .. so it's an alias for the time being + return openLibraryGlobal(libraryName, debug); + } + + public long openLibraryGlobal(String libraryName, boolean debug) { + long handle = LoadLibraryW(libraryName); + if(0==handle && debug) { + int err = GetLastError(); + System.err.println("LoadLibraryW \""+libraryName+"\" failed, error code: 0x"+Integer.toHexString(err)+", "+err); + } + return handle; + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return GetProcAddressA(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + FreeLibrary(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class WindowsDynamicLinkerImpl |