Log less confusing message when try to load native library

Motivation:

At the moment we log very confusing messages when trying to load a native library which kind of suggest that the whole loading process failed even if just one mechanism failed and the library could be loaded at the end.

Modifications:

Make the mesage less confusing and also log a successful load of the native library.

Result:

Less confusing logs.
This commit is contained in:
Norman Maurer 2016-09-09 11:16:25 +02:00
parent 4a18a143d2
commit eb7f751ba5

View File

@ -164,9 +164,10 @@ public final class NativeLibraryLoader {
for (String name : names) { for (String name : names) {
try { try {
load(name, loader); load(name, loader);
logger.debug("Successfully loaded the library: {}", name);
return; return;
} catch (Throwable t) { } catch (Throwable t) {
logger.debug("Unable to load the library: " + name + '.', t); logger.debug("Unable to load the library '{}', trying next name...", name, t);
} }
} }
throw new IllegalArgumentException("Failed to load any of the given libraries: " throw new IllegalArgumentException("Failed to load any of the given libraries: "
@ -245,14 +246,18 @@ public final class NativeLibraryLoader {
// Make sure the helper is belong to the target ClassLoader. // Make sure the helper is belong to the target ClassLoader.
final Class<?> newHelper = tryToLoadClass(loader, NativeLibraryUtil.class); final Class<?> newHelper = tryToLoadClass(loader, NativeLibraryUtil.class);
loadLibraryByHelper(newHelper, name, absolute); loadLibraryByHelper(newHelper, name, absolute);
} catch (Exception e) { // Should by pass the UnsatisfiedLinkError here! return;
logger.debug("Unable to load the library: " + name + '.', e); } catch (UnsatisfiedLinkError e) { // Should by pass the UnsatisfiedLinkError here!
NativeLibraryUtil.loadLibrary(name, absolute); // Fallback to local helper class. logger.debug("Unable to load the library '{}', trying other loading mechanism.", name, e);
} catch (Exception e) {
logger.debug("Unable to load the library '{}', trying other loading mechanism.", name, e);
} }
NativeLibraryUtil.loadLibrary(name, absolute); // Fallback to local helper class.
} }
private static void loadLibraryByHelper(final Class<?> helper, final String name, final boolean absolute) { private static void loadLibraryByHelper(final Class<?> helper, final String name, final boolean absolute)
AccessController.doPrivileged(new PrivilegedAction<Object>() { throws UnsatisfiedLinkError {
Object ret = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override @Override
public Object run() { public Object run() {
try { try {
@ -263,10 +268,22 @@ public final class NativeLibraryLoader {
method.setAccessible(true); method.setAccessible(true);
return method.invoke(null, name, absolute); return method.invoke(null, name, absolute);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException("Load library failed!", e); return e;
} }
} }
}); });
if (ret instanceof Throwable) {
Throwable error = (Throwable) ret;
Throwable cause = error.getCause();
if (cause != null) {
if (cause instanceof UnsatisfiedLinkError) {
throw (UnsatisfiedLinkError) cause;
} else {
throw new UnsatisfiedLinkError(cause.getMessage());
}
}
throw new UnsatisfiedLinkError(error.getMessage());
}
} }
/** /**