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.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
52
53
54
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 }