View Javadoc
1   /*
2    * Copyright 2016 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.kqueue;
17  
18  import io.netty.channel.unix.FileDescriptor;
19  import io.netty.util.internal.PlatformDependent;
20  import io.netty.util.internal.UnstableApi;
21  
22  /**
23   * If KQueue is available the JNI resources will be loaded when this class loads.
24   */
25  @UnstableApi
26  public final class KQueue {
27      private static final Throwable UNAVAILABILITY_CAUSE;
28  
29      static  {
30          Throwable cause = null;
31          FileDescriptor kqueueFd = null;
32          try {
33              kqueueFd = Native.newKQueue();
34          } catch (Throwable t) {
35              cause = t;
36          } finally {
37              if (kqueueFd != null) {
38                  try {
39                      kqueueFd.close();
40                  } catch (Exception ignore) {
41                      // ignore
42                  }
43              }
44          }
45  
46          if (cause != null) {
47              UNAVAILABILITY_CAUSE = cause;
48          } else {
49              UNAVAILABILITY_CAUSE = PlatformDependent.hasUnsafe()
50                      ? null
51                      : new IllegalStateException(
52                              "sun.misc.Unsafe not available",
53                              PlatformDependent.getUnsafeUnavailabilityCause());
54          }
55      }
56  
57      /**
58       * Returns {@code true} if and only if the
59       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-kqueue}</a> is available.
60       */
61      public static boolean isAvailable() {
62          return UNAVAILABILITY_CAUSE == null;
63      }
64  
65      /**
66       * Ensure that <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-kqueue}</a> is
67       * available.
68       *
69       * @throws UnsatisfiedLinkError if unavailable
70       */
71      public static void ensureAvailability() {
72          if (UNAVAILABILITY_CAUSE != null) {
73              throw (Error) new UnsatisfiedLinkError(
74                      "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
75          }
76      }
77  
78      /**
79       * Returns the cause of unavailability of
80       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-kqueue}</a>.
81       *
82       * @return the cause if unavailable. {@code null} if available.
83       */
84      public static Throwable unavailabilityCause() {
85          return UNAVAILABILITY_CAUSE;
86      }
87  
88      private KQueue() { }
89  }