From 730bfd4fdb185ba6ef426d1e00beb70f0778a341 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 4 Aug 2014 15:03:25 -0700 Subject: [PATCH] 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. --- .../java/io/netty/channel/epoll/Epoll.java | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java index dd10b74b91..ccdfc58b64 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/Epoll.java @@ -20,19 +20,17 @@ package io.netty.channel.epoll; */ public final class Epoll { - private static final boolean IS_AVAILABLE; + private static final Throwable UNAVAILABILITY_CAUSE; static { - boolean available; + Throwable cause = null; int epollFd = -1; int eventFd = -1; try { epollFd = Native.epollCreate(); eventFd = Native.eventFd(); - available = true; - } catch (Throwable cause) { - // ignore - available = false; + } catch (Throwable t) { + cause = t; } finally { if (epollFd != -1) { 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 - * {@code netty-transport-native-epoll} can be used. + * {@code netty-transport-native-epoll} is available. */ public static boolean isAvailable() { - return IS_AVAILABLE; + return UNAVAILABILITY_CAUSE == null; + } + + /** + * Ensure that {@code netty-transport-native-epoll} 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 + * {@code netty-transport-native-epoll}. + * + * @return the cause if unavailable. {@code null} if available. + */ + public static Throwable unavailabilityCause() { + return UNAVAILABILITY_CAUSE; } private Epoll() { }