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 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 }