From eb7f751ba519cbcab47d640cd18757f09d077b55 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 9 Sep 2016 11:16:25 +0200 Subject: [PATCH] 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. --- .../util/internal/NativeLibraryLoader.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java index b8757b5b29..60859bab80 100644 --- a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java +++ b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java @@ -164,9 +164,10 @@ public final class NativeLibraryLoader { for (String name : names) { try { load(name, loader); + logger.debug("Successfully loaded the library: {}", name); return; } 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: " @@ -245,14 +246,18 @@ public final class NativeLibraryLoader { // Make sure the helper is belong to the target ClassLoader. final Class newHelper = tryToLoadClass(loader, NativeLibraryUtil.class); loadLibraryByHelper(newHelper, name, absolute); - } catch (Exception e) { // Should by pass the UnsatisfiedLinkError here! - logger.debug("Unable to load the library: " + name + '.', e); - NativeLibraryUtil.loadLibrary(name, absolute); // Fallback to local helper class. + return; + } catch (UnsatisfiedLinkError e) { // Should by pass the UnsatisfiedLinkError here! + 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) { - AccessController.doPrivileged(new PrivilegedAction() { + private static void loadLibraryByHelper(final Class helper, final String name, final boolean absolute) + throws UnsatisfiedLinkError { + Object ret = AccessController.doPrivileged(new PrivilegedAction() { @Override public Object run() { try { @@ -263,10 +268,22 @@ public final class NativeLibraryLoader { method.setAccessible(true); return method.invoke(null, name, absolute); } 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()); + } } /**