View Javadoc
1   /*
2    * Copyright 2022 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.socket.nio;
17  
18  import io.netty.channel.socket.SocketProtocolFamily;
19  import io.netty.util.internal.PlatformDependent;
20  import io.netty.util.internal.logging.InternalLogger;
21  import io.netty.util.internal.logging.InternalLoggerFactory;
22  
23  import java.io.IOException;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.net.StandardProtocolFamily;
27  import java.nio.channels.Channel;
28  import java.nio.channels.SocketChannel;
29  import java.nio.channels.spi.SelectorProvider;
30  
31  final class SelectorProviderUtil {
32      private static final InternalLogger logger = InternalLoggerFactory.getInstance(SelectorProviderUtil.class);
33  
34      static Method findOpenMethod(String methodName) {
35          if (PlatformDependent.javaVersion() >= 15) {
36              try {
37                  return SelectorProvider.class.getMethod(methodName, java.net.ProtocolFamily.class);
38              } catch (Throwable e) {
39                  logger.debug("SelectorProvider.{}(ProtocolFamily) not available, will use default", methodName, e);
40              }
41          }
42          return null;
43      }
44  
45      /**
46       * Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in
47       * {@link SelectorProvider#provider()} which is called by each SocketChannel.open() otherwise.
48       * <p>
49       * See <a href="https://github.com/netty/netty/issues/2308">#2308</a>.
50       */
51      private static <C extends Channel> C newChannel(Method method, SelectorProvider provider,
52                                                      Object family) throws IOException {
53          if (family != null && method != null) {
54              try {
55                  @SuppressWarnings("unchecked")
56                  C channel = (C) method.invoke(provider, family);
57                  return channel;
58              } catch (InvocationTargetException e) {
59                  throw new IOException(e);
60              } catch (IllegalAccessException e) {
61                  throw new IOException(e);
62              }
63          }
64          return null;
65      }
66  
67      static <C extends Channel> C newChannel(Method method, SelectorProvider provider,
68                                                      SocketProtocolFamily family) throws IOException {
69          if (family != null) {
70              return newChannel(method, provider, family.toJdkFamily());
71          }
72          return null;
73      }
74  
75      static <C extends Channel> C newDomainSocketChannel(Method method, SelectorProvider provider) throws IOException {
76          return newChannel(method, provider, StandardProtocolFamily.valueOf("UNIX"));
77      }
78  
79      private SelectorProviderUtil() { }
80  }