From d9b4ec050caa6178e38e6c54eed37cb5ef5083d9 Mon Sep 17 00:00:00 2001 From: nmittler Date: Tue, 12 Apr 2016 07:16:57 -0700 Subject: [PATCH] Support preloading of native epoll lib Motivation: Some applications may use alternative methods of loading the epoll JNI symbols. We should support this use case. Modifications: Attempt to use a side effect free JNI method. If that fails, load the library. Result: Fixes #5122 --- .../java/io/netty/channel/epoll/Native.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java index e4ba4ce2a7..d9244314fb 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Native.java @@ -53,14 +53,16 @@ import static io.netty.channel.unix.Errors.newIOException; */ public final class Native { static { - String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim(); - if (!name.startsWith("linux")) { - throw new IllegalStateException("Only supported on Linux"); + try { + // First, try calling a side-effect free JNI method to see if the library was already + // loaded by the application. + offsetofEpollData(); + } catch (UnsatisfiedLinkError ignore) { + // The library was not previously loaded, load it now. + loadNativeLibrary(); } - NativeLibraryLoader.load(SystemPropertyUtil.get("io.netty.packagePrefix", "").replace('.', '-') + - "netty-transport-native-epoll", - PlatformDependent.getClassLoader(Native.class)); } + // EventLoop operations and constants public static final int EPOLLIN = epollin(); public static final int EPOLLOUT = epollout(); @@ -250,4 +252,13 @@ public final class Native { private Native() { // utility } + + private static void loadNativeLibrary() { + String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim(); + if (!name.startsWith("linux")) { + throw new IllegalStateException("Only supported on Linux"); + } + NativeLibraryLoader.load(SystemPropertyUtil.get("io.netty.packagePrefix", "").replace('.', '-') + + "netty-transport-native-epoll", PlatformDependent.getClassLoader(Native.class)); + } }