2222
2323package com .eatthepath .pushy .apns ;
2424
25- import io .netty .channel .EventLoopGroup ;
25+ import io .netty .channel .Channel ;
26+ import io .netty .channel .IoEventLoopGroup ;
27+ import io .netty .channel .IoHandler ;
2628import io .netty .channel .socket .DatagramChannel ;
2729import io .netty .channel .socket .SocketChannel ;
2830
@@ -36,64 +38,66 @@ class ClientChannelClassUtil {
3638 private static final Map <String , String > DATAGRAM_CHANNEL_CLASSES = new HashMap <>();
3739
3840 static {
39- SOCKET_CHANNEL_CLASSES .put ("io.netty.channel.nio.NioEventLoopGroup" , "io.netty.channel.socket.nio.NioSocketChannel" );
40- SOCKET_CHANNEL_CLASSES .put ("io.netty.channel.epoll.EpollEventLoopGroup" , "io.netty.channel.epoll.EpollSocketChannel" );
41- SOCKET_CHANNEL_CLASSES .put ("io.netty.channel.kqueue.KQueueEventLoopGroup" , "io.netty.channel.kqueue.KQueueSocketChannel" );
41+ SOCKET_CHANNEL_CLASSES .put ("io.netty.channel.nio.NioIoHandler" , "io.netty.channel.socket.nio.NioSocketChannel" );
42+ SOCKET_CHANNEL_CLASSES .put ("io.netty.channel.uring.IoUringIoHandler" , "io.netty.channel.uring.IoUringSocketChannel" );
43+ SOCKET_CHANNEL_CLASSES .put ("io.netty.channel.epoll.EpollIoHandler" , "io.netty.channel.epoll.EpollSocketChannel" );
44+ SOCKET_CHANNEL_CLASSES .put ("io.netty.channel.kqueue.KQueueIoHandler" , "io.netty.channel.kqueue.KQueueSocketChannel" );
4245
43- DATAGRAM_CHANNEL_CLASSES .put ("io.netty.channel.nio.NioEventLoopGroup" , "io.netty.channel.socket.nio.NioDatagramChannel" );
44- DATAGRAM_CHANNEL_CLASSES .put ("io.netty.channel.epoll.EpollEventLoopGroup" , "io.netty.channel.epoll.EpollDatagramChannel" );
45- DATAGRAM_CHANNEL_CLASSES .put ("io.netty.channel.kqueue.KQueueEventLoopGroup" , "io.netty.channel.kqueue.KQueueDatagramChannel" );
46+ DATAGRAM_CHANNEL_CLASSES .put ("io.netty.channel.nio.NioIoHandler" , "io.netty.channel.socket.nio.NioDatagramChannel" );
47+ DATAGRAM_CHANNEL_CLASSES .put ("io.netty.channel.uring.IoUringIoHandler" , "io.netty.channel.uring.IoUringDatagramChannel" );
48+ DATAGRAM_CHANNEL_CLASSES .put ("io.netty.channel.epoll.EpollIoHandler" , "io.netty.channel.epoll.EpollDatagramChannel" );
49+ DATAGRAM_CHANNEL_CLASSES .put ("io.netty.channel.kqueue.KQueueIoHandler" , "io.netty.channel.kqueue.KQueueDatagramChannel" );
4650 }
4751
4852 /**
4953 * Returns a socket channel class suitable for specified event loop group.
5054 *
51- * @param eventLoopGroup the event loop group for which to identify an appropriate socket channel class; must not
55+ * @param ioEventLoopGroup the event loop group for which to identify an appropriate socket channel class; must not
5256 * be {@code null}
5357 *
5458 * @return a socket channel class suitable for use with the given event loop group
5559 *
56- * @throws IllegalArgumentException in case of null or unrecognized event loop group
60+ * @throws IllegalArgumentException if no suitable socket channel class could be found for the given event loop
61+ * group
62+ * @throws NullPointerException if the given {@code ioEventLoopGroup} was {@code null}
5763 */
58- static Class <? extends SocketChannel > getSocketChannelClass (final EventLoopGroup eventLoopGroup ) {
59- Objects .requireNonNull (eventLoopGroup );
60-
61- final String socketChannelClassName = SOCKET_CHANNEL_CLASSES .get (eventLoopGroup .getClass ().getName ());
62-
63- if (socketChannelClassName == null ) {
64- throw new IllegalArgumentException ("No socket channel class found for event loop group type: " + eventLoopGroup .getClass ().getName ());
65- }
66-
67- try {
68- return Class .forName (socketChannelClassName ).asSubclass (SocketChannel .class );
69- } catch (final ClassNotFoundException e ) {
70- throw new IllegalArgumentException (e );
71- }
64+ static Class <? extends SocketChannel > getSocketChannelClass (final IoEventLoopGroup ioEventLoopGroup ) {
65+ return getChannelClass (Objects .requireNonNull (ioEventLoopGroup ), SOCKET_CHANNEL_CLASSES , SocketChannel .class );
7266 }
7367
7468 /**
7569 * Returns a datagram channel class suitable for specified event loop group.
7670 *
77- * @param eventLoopGroup the event loop group for which to identify an appropriate datagram channel class; must not
78- * be {@code null}
71+ * @param ioEventLoopGroup the event loop group for which to identify an appropriate datagram channel class; must
72+ * not be {@code null}
7973 *
8074 * @return a datagram channel class suitable for use with the given event loop group
8175 *
82- * @throws IllegalArgumentException in case of null or unrecognized event loop group
76+ * @throws IllegalArgumentException if no suitable datagram channel class could be found for the given event loop
77+ * group
78+ * @throws NullPointerException if the given {@code ioEventLoopGroup} was {@code null}
8379 */
84- static Class <? extends DatagramChannel > getDatagramChannelClass (final EventLoopGroup eventLoopGroup ) {
85- Objects .requireNonNull (eventLoopGroup );
80+ static Class <? extends DatagramChannel > getDatagramChannelClass (final IoEventLoopGroup ioEventLoopGroup ) {
81+ return getChannelClass (Objects .requireNonNull (ioEventLoopGroup ), DATAGRAM_CHANNEL_CLASSES , DatagramChannel .class );
82+ }
8683
87- final String datagramChannelClassName = DATAGRAM_CHANNEL_CLASSES .get (eventLoopGroup .getClass ().getName ());
84+ private static <C extends Channel > Class <? extends C > getChannelClass (final IoEventLoopGroup ioEventLoopGroup ,
85+ final Map <String , String > channelClassesByIoHandlerClass ,
86+ final Class <C > channelType ) {
8887
89- if (datagramChannelClassName == null ) {
90- throw new IllegalArgumentException ("No datagram channel class found for event loop group type: " + eventLoopGroup .getClass ().getName ());
91- }
88+ for (final Map .Entry <String , String > entry : channelClassesByIoHandlerClass .entrySet ()) {
89+ try {
90+ final Class <? extends IoHandler > ioHandlerClass =
91+ Class .forName (entry .getKey ()).asSubclass (IoHandler .class );
9292
93- try {
94- return Class .forName (datagramChannelClassName ).asSubclass (DatagramChannel .class );
95- } catch (final ClassNotFoundException e ) {
96- throw new IllegalArgumentException (e );
93+ if (ioEventLoopGroup .isIoType (ioHandlerClass )) {
94+ return Class .forName (entry .getValue ()).asSubclass (channelType );
95+ }
96+ } catch (final ClassNotFoundException e ) {
97+ continue ;
98+ }
9799 }
100+
101+ throw new IllegalArgumentException ("No suitable channel class found for event loop group" );
98102 }
99103}
0 commit comments