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.netty5.testsuite.transport.socket;
17  
18  import io.netty5.channel.ChannelFactory;
19  import io.netty5.channel.EventLoop;
20  import io.netty5.channel.EventLoopGroup;
21  import io.netty5.channel.ServerChannel;
22  import io.netty5.channel.ServerChannelFactory;
23  import io.netty5.channel.socket.DatagramChannel;
24  import io.netty5.channel.socket.SocketChannel;
25  import io.netty5.channel.socket.nio.NioDatagramChannel;
26  import io.netty5.channel.socket.nio.NioServerSocketChannel;
27  import io.netty5.channel.socket.nio.NioSocketChannel;
28  import io.netty5.util.internal.logging.InternalLogger;
29  import io.netty5.util.internal.logging.InternalLoggerFactory;
30  
31  import java.io.IOException;
32  import java.lang.reflect.InvocationTargetException;
33  import java.lang.reflect.Method;
34  import java.net.ProtocolFamily;
35  import java.net.StandardProtocolFamily;
36  import java.nio.channels.spi.SelectorProvider;
37  
38  final class NioDomainSocketTestUtil {
39      private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioDomainSocketTestUtil.class);
40  
41      private static final ProtocolFamily FAMILY;
42      private static final boolean DATAGRAM_SUPPORTED;
43      private static final boolean SOCKET_SUPPORTED;
44  
45      static {
46          boolean datagramSupported = false;
47          boolean socketSupported = false;
48          ProtocolFamily family = null;
49          try {
50              family = StandardProtocolFamily.valueOf("UNIX");
51              try {
52                  java.nio.channels.DatagramChannel channel = SelectorProvider.provider().openDatagramChannel(family);
53                  channel.close();
54                  datagramSupported = true;
55              } catch (UnsupportedOperationException | IOException e) {
56                  logger.debug("SelectorProvider can't be used to support Datagram Unix Domain Sockets", e);
57              }
58  
59              try {
60                  Method openServerSocketChannelMethod = SelectorProvider.class
61                          .getMethod("openServerSocketChannel", ProtocolFamily.class);
62  
63                  java.nio.channels.Channel channel = (java.nio.channels.Channel) openServerSocketChannelMethod
64                          .invoke(SelectorProvider.provider(), family);
65                  channel.close();
66  
67                  Method openSocketChannelMethod = SelectorProvider.class
68                          .getMethod("openSocketChannel", ProtocolFamily.class);
69  
70                  channel = (java.nio.channels.Channel) openSocketChannelMethod
71                          .invoke(SelectorProvider.provider(), family);
72                  channel.close();
73  
74                  socketSupported = true;
75              } catch (NoSuchMethodException | IllegalAccessException |
76                       InvocationTargetException | UnsupportedOperationException | IOException e) {
77                  logger.debug("SelectorProvider can't be used to support Socket Unix Domain Sockets", e);
78              }
79          } catch (IllegalArgumentException e) {
80              logger.debug("StandardProtocolFamily doesn't support Unix Domain Sockets", e);
81          }
82          DATAGRAM_SUPPORTED = datagramSupported;
83          SOCKET_SUPPORTED = socketSupported;
84          FAMILY = family;
85      }
86  
87      static boolean isDatagramSupported() {
88          // As of this today this is not supported by the JDK but at some point it might, so let's prepare for it.
89          return DATAGRAM_SUPPORTED;
90      }
91  
92      static boolean isSocketSupported() {
93          return SOCKET_SUPPORTED;
94      }
95  
96      static ProtocolFamily domainSocketFamily() {
97          return FAMILY;
98      }
99  
100     static boolean isDomainSocketFamily(ProtocolFamily family) {
101         return family != null && family == FAMILY;
102     }
103 
104     static ChannelFactory<DatagramChannel> newDomainSocketDatagramChannelFactory() {
105         if (DATAGRAM_SUPPORTED) {
106             return new ChannelFactory<>() {
107                 @Override
108                 public DatagramChannel newChannel(EventLoop eventLoop)  {
109                     return new NioDatagramChannel(eventLoop, NioDomainSocketTestUtil.domainSocketFamily());
110                 }
111 
112                 @Override
113                 public String toString() {
114                     return NioDatagramChannel.class.getSimpleName() + ".class";
115                 }
116             };
117         }
118         throw new UnsupportedOperationException();
119     }
120 
121     static ServerChannelFactory<ServerChannel> newDomainSocketServerChannelFactory() {
122         if (SOCKET_SUPPORTED) {
123             return new ServerChannelFactory<>() {
124                 @Override
125                 public ServerChannel newChannel(EventLoop eventLoop, EventLoopGroup childEventLoopGroup)  {
126                     return new NioServerSocketChannel(eventLoop, eventLoop, SelectorProvider.provider(), FAMILY);
127                 }
128 
129                 @Override
130                 public String toString() {
131                     return NioServerSocketChannel.class.getSimpleName() + ".class";
132                 }
133             };
134         }
135         throw new UnsupportedOperationException();
136     }
137 
138     static ChannelFactory<SocketChannel> newDomainSocketChannelFactory() {
139         if (SOCKET_SUPPORTED) {
140             return new ChannelFactory<>() {
141                 @Override
142                 public SocketChannel newChannel(EventLoop eventLoop)  {
143                     return new NioSocketChannel(eventLoop, SelectorProvider.provider(), FAMILY);
144                 }
145 
146                 @Override
147                 public String toString() {
148                     return NioSocketChannel.class.getSimpleName() + ".class";
149                 }
150             };
151         }
152         throw new UnsupportedOperationException();
153     }
154 
155     private NioDomainSocketTestUtil() { }
156 }