Add more utility methods to check the availability of the epoll transport

Related issue: #2733

Motivation:

Unlike OpenSsl, Epoll lacks a couple useful availability checker
methods:

- ensureAvailability()
- unavailabilityCause()

Modifications:

Add missing methods

Result:

More ways to check the availability and to get the cause of
unavailability programatically.
This commit is contained in:
Trustin Lee 2014-08-04 15:03:25 -07:00
parent 2506dc778c
commit 730bfd4fdb

View File

@ -20,19 +20,17 @@ package io.netty.channel.epoll;
*/ */
public final class Epoll { public final class Epoll {
private static final boolean IS_AVAILABLE; private static final Throwable UNAVAILABILITY_CAUSE;
static { static {
boolean available; Throwable cause = null;
int epollFd = -1; int epollFd = -1;
int eventFd = -1; int eventFd = -1;
try { try {
epollFd = Native.epollCreate(); epollFd = Native.epollCreate();
eventFd = Native.eventFd(); eventFd = Native.eventFd();
available = true; } catch (Throwable t) {
} catch (Throwable cause) { cause = t;
// ignore
available = false;
} finally { } finally {
if (epollFd != -1) { if (epollFd != -1) {
try { try {
@ -49,15 +47,43 @@ public final class Epoll {
} }
} }
} }
IS_AVAILABLE = available;
if (cause != null) {
UNAVAILABILITY_CAUSE = cause;
} else {
UNAVAILABILITY_CAUSE = null;
}
} }
/** /**
* Returns {@code true} if and only if the * Returns {@code true} if and only if the
* <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> can be used. * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is available.
*/ */
public static boolean isAvailable() { public static boolean isAvailable() {
return IS_AVAILABLE; return UNAVAILABILITY_CAUSE == null;
}
/**
* Ensure that <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is
* available.
*
* @throws UnsatisfiedLinkError if unavailable
*/
public static void ensureAvailability() {
if (UNAVAILABILITY_CAUSE != null) {
throw (Error) new UnsatisfiedLinkError(
"failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
}
}
/**
* Returns the cause of unavailability of
* <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a>.
*
* @return the cause if unavailable. {@code null} if available.
*/
public static Throwable unavailabilityCause() {
return UNAVAILABILITY_CAUSE;
} }
private Epoll() { } private Epoll() { }