1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
47
48
49
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 | IllegalAccessException e) {
59 throw new IOException(e);
60 }
61 }
62 return null;
63 }
64
65 static <C extends Channel> C newChannel(Method method, SelectorProvider provider,
66 SocketProtocolFamily family) throws IOException {
67 if (family != null) {
68 return newChannel(method, provider, family.toJdkFamily());
69 }
70 return null;
71 }
72
73 static <C extends Channel> C newDomainSocketChannel(Method method, SelectorProvider provider) throws IOException {
74 return newChannel(method, provider, StandardProtocolFamily.valueOf("UNIX"));
75 }
76
77 private SelectorProviderUtil() { }
78 }