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    *   https://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.unix;
17  
18  import io.netty.util.internal.ClassInitializerUtil;
19  import io.netty.util.internal.UnstableApi;
20  
21  import java.io.IOException;
22  import java.net.InetSocketAddress;
23  import java.net.PortUnreachableException;
24  import java.nio.channels.ClosedChannelException;
25  import java.util.concurrent.atomic.AtomicBoolean;
26  
27  /**
28   * Tells if <a href="https://netty.io/wiki/native-transports.html">{@code netty-transport-native-unix}</a> is
29   * supported.
30   */
31  public final class Unix {
32      private static final AtomicBoolean registered = new AtomicBoolean();
33  
34      static {
35          // Preload all classes that will be used in the OnLoad(...) function of JNI to eliminate the possiblity of a
36          // class-loader deadlock. This is a workaround for https://github.com/netty/netty/issues/11209.
37  
38          // This needs to match all the classes that are loaded via NETTY_JNI_UTIL_LOAD_CLASS or looked up via
39          // NETTY_JNI_UTIL_FIND_CLASS.
40          ClassInitializerUtil.tryLoadClasses(Unix.class,
41                  // netty_unix_errors
42                  OutOfMemoryError.class, RuntimeException.class, ClosedChannelException.class,
43                  IOException.class, PortUnreachableException.class,
44  
45                  // netty_unix_socket
46                  DatagramSocketAddress.class, DomainDatagramSocketAddress.class, InetSocketAddress.class
47          );
48      }
49  
50      /**
51       * Internal method... Should never be called from the user.
52       *
53       * @param registerTask
54       */
55      @UnstableApi
56      public static synchronized void registerInternal(Runnable registerTask) {
57          registerTask.run();
58          Socket.initialize();
59      }
60  
61      /**
62       * Returns {@code true} if and only if the <a href="https://netty.io/wiki/native-transports.html">{@code
63       * netty_transport_native_unix}</a> is available.
64       */
65      @Deprecated
66      public static boolean isAvailable() {
67          return false;
68      }
69  
70      /**
71       * Ensure that <a href="https://netty.io/wiki/native-transports.html">{@code netty_transport_native_unix}</a> is
72       * available.
73       *
74       * @throws UnsatisfiedLinkError if unavailable
75       */
76      @Deprecated
77      public static void ensureAvailability() {
78         throw new UnsupportedOperationException();
79      }
80  
81      /**
82       * Returns the cause of unavailability of <a href="https://netty.io/wiki/native-transports.html">
83       * {@code netty_transport_native_unix}</a>.
84       *
85       * @return the cause if unavailable. {@code null} if available.
86       */
87      @Deprecated
88      public static Throwable unavailabilityCause() {
89          return new UnsupportedOperationException();
90      }
91  
92      private Unix() {
93      }
94  }