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