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
This commit is contained in:
nmittler 2016-04-12 07:16:57 -07:00
parent 718bf2fa45
commit de2515ddd8

View File

@ -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));
}
}