View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.epoll;
17  
18  import io.netty.channel.unix.FileDescriptor;
19  import io.netty.util.internal.PlatformDependent;
20  import io.netty.util.internal.SystemPropertyUtil;
21  
22  /**
23   * Tells if <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is supported.
24   */
25  public final class Epoll {
26  
27      private static final Throwable UNAVAILABILITY_CAUSE;
28  
29      static  {
30          Throwable cause = null;
31  
32          if (SystemPropertyUtil.getBoolean("io.netty.transport.noNative", false)) {
33              cause = new UnsupportedOperationException(
34                      "Native transport was explicit disabled with -Dio.netty.transport.noNative=true");
35          } else {
36              FileDescriptor epollFd = null;
37              FileDescriptor eventFd = null;
38              try {
39                  epollFd = Native.newEpollCreate();
40                  eventFd = Native.newEventFd();
41              } catch (Throwable t) {
42                  cause = t;
43              } finally {
44                  if (epollFd != null) {
45                      try {
46                          epollFd.close();
47                      } catch (Exception ignore) {
48                          // ignore
49                      }
50                  }
51                  if (eventFd != null) {
52                      try {
53                          eventFd.close();
54                      } catch (Exception ignore) {
55                          // ignore
56                      }
57                  }
58              }
59          }
60  
61          if (cause != null) {
62              UNAVAILABILITY_CAUSE = cause;
63          } else {
64              UNAVAILABILITY_CAUSE = PlatformDependent.hasUnsafe()
65                      ? null
66                      : new IllegalStateException(
67                      "sun.misc.Unsafe not available",
68                      PlatformDependent.getUnsafeUnavailabilityCause());
69          }
70      }
71  
72      /**
73       * Returns {@code true} if and only if the
74       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is available.
75       */
76      public static boolean isAvailable() {
77          return UNAVAILABILITY_CAUSE == null;
78      }
79  
80      /**
81       * Ensure that <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is
82       * available.
83       *
84       * @throws UnsatisfiedLinkError if unavailable
85       */
86      public static void ensureAvailability() {
87          if (UNAVAILABILITY_CAUSE != null) {
88              throw (Error) new UnsatisfiedLinkError(
89                      "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
90          }
91      }
92  
93      /**
94       * Returns the cause of unavailability of
95       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a>.
96       *
97       * @return the cause if unavailable. {@code null} if available.
98       */
99      public static Throwable unavailabilityCause() {
100         return UNAVAILABILITY_CAUSE;
101     }
102 
103     private Epoll() { }
104 }