diff options
author | Sven Gothel <[email protected]> | 2013-01-30 15:35:40 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-01-30 15:35:40 +0100 |
commit | 00c9fa8f2d69d15a2e4183e127b543a92fb5f4b8 (patch) | |
tree | 87645915a69c3c585cad008e0eee52ecf88b4524 /src/java | |
parent | be6c4158cf901af01be370b31e5bec368a51b2d5 (diff) |
GlueGen: NativeLibrary Fix, JNILibLoaderBase Enhancement
- NativeLibrary Fix
- enumerateLibraryPaths(..):
- Properly iterate through all prefix _and_ suffix.
- Make public for JNILibLoaderBase.loadLibraryInternal(..)
- isValidNativeLibraryName(..):
- Stop iterating through prefix, if previously found
but suffix doesn't match.
- JNILibLoaderBase.loadLibraryInternal(..) Enhancement
- Mark customLibLoader FIXME: remove (we will get rid of jnlp.launcher.class)
- If System.load(TempJarCache) and System.loadLibrary(plainLibName) fails,
use NativeLibrary.enumerateLibraryPaths() w/ System.load(..) as last resort.
Tested on Linux x86_64 Java6 and OSX Java7 manually, no regressions expected.
Diffstat (limited to 'src/java')
-rw-r--r-- | src/java/com/jogamp/common/jvm/JNILibLoaderBase.java | 104 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/os/NativeLibrary.java | 112 |
2 files changed, 135 insertions, 81 deletions
diff --git a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java index 94f451e..d0d8003 100644 --- a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java +++ b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java @@ -47,6 +47,8 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Arrays; import java.util.HashSet; +import java.util.Iterator; +import java.util.List; import com.jogamp.common.os.NativeLibrary; import com.jogamp.common.util.JarUtil; @@ -345,7 +347,7 @@ public class JNILibLoaderBase { } } - // private static final Class<?> customLauncherClass; + // private static final Class<?> customLauncherClass; // FIXME: remove private static final Method customLoadLibraryMethod; static { @@ -354,6 +356,7 @@ public class JNILibLoaderBase { final Method loadLibraryMethod = AccessController.doPrivileged(new PrivilegedAction<Method>() { public Method run() { + // FIXME: remove final boolean usingJNLPAppletLauncher = Debug.getBooleanProperty(sunAppletLauncherProperty, true); Class<?> launcherClass = null; @@ -380,8 +383,7 @@ public class JNILibLoaderBase { launcherClass = null; } } - } - + } if(null==launcherClass) { String launcherClassName = PropertyAccess.getProperty("jnlp.launcher.class", false, null); if(null!=launcherClassName) { @@ -406,41 +408,77 @@ public class JNILibLoaderBase { } private static void loadLibraryInternal(String libraryName, ClassLoader cl) { - // Note: special-casing JAWT which is built in to the JDK - if (null!=customLoadLibraryMethod && !libraryName.equals("jawt")) { - try { - customLoadLibraryMethod.invoke(null, new Object[] { libraryName }); - } catch (Exception e) { - Throwable t = e; - if (t instanceof InvocationTargetException) { - t = ((InvocationTargetException) t).getTargetException(); - } - if (t instanceof Error) { - throw (Error) t; - } - if (t instanceof RuntimeException) { - throw (RuntimeException) t; - } - // Throw UnsatisfiedLinkError for best compatibility with System.loadLibrary() - throw (UnsatisfiedLinkError) new UnsatisfiedLinkError("can not load library "+libraryName).initCause(e); - } - } else { - // System.err.println("sun.boot.library.path=" + Debug.getProperty("sun.boot.library.path", false)); - final String libraryPath = NativeLibrary.findLibrary(libraryName, cl); // implicit TempJarCache usage if used/initialized - if(DEBUG) { - System.err.print("JNILibLoaderBase: loadLibraryInternal("+libraryName+"): CL: "+libraryPath); - } - if(null != libraryPath) { + // Note: special-casing JAWT which is built in to the JDK + int mode = 0; // 1 - custom, 2 - System.load( TempJarCache ), 3 - System.loadLibrary( name ), 4 - System.load( enumLibNames ) + if (null!=customLoadLibraryMethod && !libraryName.equals("jawt")) { + // FIXME: remove if(DEBUG) { - System.err.println(" -> System.load("+libraryPath+")"); + System.err.println("JNILibLoaderBase: customLoad("+libraryName+") - mode 1"); + } + try { + customLoadLibraryMethod.invoke(null, new Object[] { libraryName }); + mode = 1; + } catch (Exception e) { + Throwable t = e; + if (t instanceof InvocationTargetException) { + t = ((InvocationTargetException) t).getTargetException(); + } + if (t instanceof Error) { + throw (Error) t; + } + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } + // Throw UnsatisfiedLinkError for best compatibility with System.loadLibrary() + throw (UnsatisfiedLinkError) new UnsatisfiedLinkError("can not load library "+libraryName).initCause(e); } - System.load(libraryPath); } else { + // System.err.println("sun.boot.library.path=" + Debug.getProperty("sun.boot.library.path", false)); + final String libraryPath = NativeLibrary.findLibrary(libraryName, cl); // implicit TempJarCache usage if used/initialized if(DEBUG) { - System.err.println(" -> System.loadLibrary("+libraryName+")"); + System.err.println("JNILibLoaderBase: loadLibraryInternal("+libraryName+"), TempJarCache: "+libraryPath); + } + if(null != libraryPath) { + if(DEBUG) { + System.err.println("JNILibLoaderBase: System.load("+libraryPath+") - mode 2"); + } + System.load(libraryPath); + mode = 2; + } else { + if(DEBUG) { + System.err.println("JNILibLoaderBase: System.loadLibrary("+libraryName+") - mode 3"); + } + try { + System.loadLibrary(libraryName); + mode = 3; + } catch (UnsatisfiedLinkError ex1) { + if(DEBUG) { + System.err.println("ERROR (retry w/ enumLibPath) - "+ex1.getMessage()); + } + List<String> possiblePaths = NativeLibrary.enumerateLibraryPaths(libraryName, libraryName, libraryName, true, cl); + // Iterate down these and see which one if any we can actually find. + for (Iterator<String> iter = possiblePaths.iterator(); 0 == mode && iter.hasNext(); ) { + String path = iter.next(); + if (DEBUG) { + System.err.println("JNILibLoaderBase: System.load("+path+") - mode 4"); + } + try { + System.load(path); + mode = 4; + } catch (UnsatisfiedLinkError ex2) { + if(DEBUG) { + System.err.println("n/a - "+ex2.getMessage()); + } + if(!iter.hasNext()) { + throw ex2; + } + } + } + } } - System.loadLibrary(libraryName); } - } + if(DEBUG) { + System.err.println("JNILibLoaderBase: loadLibraryInternal("+libraryName+"): OK - mode "+mode); + } } } diff --git a/src/java/com/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java index f5265af..49ec860 100755 --- a/src/java/com/jogamp/common/os/NativeLibrary.java +++ b/src/java/com/jogamp/common/os/NativeLibrary.java @@ -174,13 +174,13 @@ public class NativeLibrary implements DynamicLookupHelper { macOSXLibName, searchSystemPathFirst, loader); + Platform.initSingleton(); // loads native gluegen-rt library // Iterate down these and see which one if any we can actually find. for (Iterator<String> iter = possiblePaths.iterator(); iter.hasNext(); ) { String path = iter.next(); if (DEBUG) { System.err.println("NativeLibrary.open(): Trying to load " + path); } - Platform.initSingleton(); // loads native gluegen-rt library long res; if(global) { res = dynLink.openLibraryGlobal(path, DEBUG); @@ -254,18 +254,22 @@ public class NativeLibrary implements DynamicLookupHelper { * @return basename of libName w/o path, ie. /usr/lib/libDrinkBeer.so -> DrinkBeer on Unix systems, but null on Windows. */ public static String isValidNativeLibraryName(String libName, boolean isLowerCaseAlready) { - libName = IOUtil.getBasename(libName); - final String libNameLC = isLowerCaseAlready ? libName : libName.toLowerCase(); - for(int i=0; i<prefixes.length; i++) { - if (libNameLC.startsWith(prefixes[i])) { - for(int j=0; j<suffixes.length; j++) { - if (libNameLC.endsWith(suffixes[j])) { - final int s = prefixes[i].length(); - final int e = suffixes[j].length(); - return libName.substring(s, libName.length()-e); - } + final String libBaseName = IOUtil.getBasename(libName); + final String libBaseNameLC = isLowerCaseAlready ? libBaseName : libBaseName.toLowerCase(); + int prefixIdx = -1; + for(int i=0; i<prefixes.length && 0 > prefixIdx; i++) { + if (libBaseNameLC.startsWith(prefixes[i])) { + prefixIdx = i; + } + } + if( 0 <= prefixIdx ) { + for(int i=0; i<suffixes.length; i++) { + if (libBaseNameLC.endsWith(suffixes[i])) { + final int s = prefixes[prefixIdx].length(); + final int e = suffixes[i].length(); + return libBaseName.substring(s, libBaseName.length()-e); } - } + } } return null; } @@ -273,7 +277,7 @@ public class NativeLibrary implements DynamicLookupHelper { /** 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<String> enumerateLibraryPaths(String windowsLibName, + public static List<String> enumerateLibraryPaths(String windowsLibName, String unixLibName, String macOSXLibName, boolean searchSystemPathFirst, @@ -372,46 +376,58 @@ public class NativeLibrary implements DynamicLookupHelper { } 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 }; + // 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. + final String libBaseNameLC = IOUtil.getBasename(libName).toLowerCase(); + + int prefixIdx = -1; + for(int i=0; i<prefixes.length && 0 > prefixIdx; i++) { + if (libBaseNameLC.startsWith(prefixes[i])) { + prefixIdx = i; + } } - - 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( 0 <= prefixIdx ) { + for(int i=0; i<suffixes.length; i++) { + if (libBaseNameLC.endsWith(suffixes[i])) { + return new String[] { libName }; + } + } + int suffixIdx = -1; + for(int i=0; i<suffixes.length && 0 > suffixIdx; i++) { + suffixIdx = libBaseNameLC.indexOf(suffixes[i]); + } + boolean ok = true; + if (suffixIdx >= 0) { + // Check to see if everything after it is a Unix version number + for (int i = suffixIdx + 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 }; + } } - } - if (ok) { - return new String[] { libName }; - } } - } - String[] res = new String[prefixes.length * suffixes.length + - ( PlatformPropsImpl.OS_TYPE == Platform.OSType.MACOS ? 1 : 0 )]; - 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]; + String[] res = new String[prefixes.length * suffixes.length + + ( PlatformPropsImpl.OS_TYPE == Platform.OSType.MACOS ? 1 : 0 )]; + 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]; + } } - } - if (PlatformPropsImpl.OS_TYPE == Platform.OSType.MACOS) { - // Plain library-base-name in Framework folder - res[idx++] = libName; - } - return res; + if (PlatformPropsImpl.OS_TYPE == Platform.OSType.MACOS) { + // Plain library-base-name in Framework folder + res[idx++] = libName; + } + return res; } private static void addPaths(String path, String[] baseNames, List<String> paths) { |