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.SystemPropertyUtil;
20  
21  /**
22   * Tells if <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is supported.
23   */
24  public final class Epoll {
25  
26      private static final Throwable UNAVAILABILITY_CAUSE;
27  
28      static  {
29          Throwable cause = null;
30  
31          if (SystemPropertyUtil.getBoolean("io.netty.transport.noNative", false)) {
32              cause = new UnsupportedOperationException(
33                      "Native transport was explicit disabled with -Dio.netty.transport.noNative=true");
34          } else {
35              FileDescriptor epollFd = null;
36              FileDescriptor eventFd = null;
37              try {
38                  epollFd = Native.newEpollCreate();
39                  eventFd = Native.newEventFd();
40              } catch (Throwable t) {
41                  cause = t;
42              } finally {
43                  if (epollFd != null) {
44                      try {
45                          epollFd.close();
46                      } catch (Exception ignore) {
47                          // ignore
48                      }
49                  }
50                  if (eventFd != null) {
51                      try {
52                          eventFd.close();
53                      } catch (Exception ignore) {
54                          // ignore
55                      }
56                  }
57              }
58          }
59  
60          UNAVAILABILITY_CAUSE = cause;
61      }
62  
63      /**
64       * Returns {@code true} if and only if the
65       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is available.
66       */
67      public static boolean isAvailable() {
68          return UNAVAILABILITY_CAUSE == null;
69      }
70  
71      /**
72       * Ensure that <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is
73       * available.
74       *
75       * @throws UnsatisfiedLinkError if unavailable
76       */
77      public static void ensureAvailability() {
78          if (UNAVAILABILITY_CAUSE != null) {
79              throw (Error) new UnsatisfiedLinkError(
80                      "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
81          }
82      }
83  
84      /**
85       * Returns the cause of unavailability of
86       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a>.
87       *
88       * @return the cause if unavailable. {@code null} if available.
89       */
90      public static Throwable unavailabilityCause() {
91          return UNAVAILABILITY_CAUSE;
92      }
93  
94      private Epoll() { }
95  }