|
| 1 | +package org.scalasbt.ipcsocket; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.net.ProtocolFamily; |
| 6 | +import java.net.ServerSocket; |
| 7 | +import java.net.SocketAddress; |
| 8 | +import java.net.SocketException; |
| 9 | +import java.net.StandardProtocolFamily; |
| 10 | +import java.nio.channels.ServerSocketChannel; |
| 11 | +import java.util.Arrays; |
| 12 | + |
| 13 | +public abstract class ServerSocketWrapper { |
| 14 | + private ServerSocketWrapper() {} |
| 15 | + |
| 16 | + private static SocketAddress unixDomainSocketAddress(final String pathName) |
| 17 | + throws ReflectiveOperationException { |
| 18 | + final Class<?> clazz = Class.forName("java.net.UnixDomainSocketAddress"); |
| 19 | + final Method method = clazz.getMethod("of", String.class); |
| 20 | + return (SocketAddress) method.invoke(null, pathName); |
| 21 | + } |
| 22 | + |
| 23 | + private static ProtocolFamily unixProtocolFamily() { |
| 24 | + return Arrays.stream(StandardProtocolFamily.class.getEnumConstants()) |
| 25 | + .filter(a -> "UNIX".equals(a.name())) |
| 26 | + .findFirst() |
| 27 | + .orElseThrow(() -> new RuntimeException("not found UNIX value in StandardProtocolFamily")); |
| 28 | + } |
| 29 | + |
| 30 | + static ServerSocketWrapper newJdkUnixDomainSocket(final String pathName) |
| 31 | + throws ReflectiveOperationException, IOException { |
| 32 | + final SocketAddress address = unixDomainSocketAddress(pathName); |
| 33 | + final ProtocolFamily protocolFamily = unixProtocolFamily(); |
| 34 | + final Method openMethod = |
| 35 | + ServerSocketChannel.class.getMethod("open", java.net.ProtocolFamily.class); |
| 36 | + final ServerSocketChannel serverSocketChannel = |
| 37 | + (ServerSocketChannel) openMethod.invoke(null, protocolFamily); |
| 38 | + serverSocketChannel.bind(address); |
| 39 | + return ServerSocketWrapper.fromServerSocketChannel(serverSocketChannel); |
| 40 | + } |
| 41 | + |
| 42 | + public static ServerSocketWrapper newUnixDomainSocket( |
| 43 | + final String pathName, final boolean jni, final boolean jdk) throws IOException { |
| 44 | + ServerSocketWrapper result; |
| 45 | + if (jdk) { |
| 46 | + try { |
| 47 | + result = newJdkUnixDomainSocket(pathName); |
| 48 | + } catch (ReflectiveOperationException e) { |
| 49 | + result = ServerSocketWrapper.fromServerSocket(new UnixDomainServerSocket(pathName, jni)); |
| 50 | + } |
| 51 | + } else { |
| 52 | + result = ServerSocketWrapper.fromServerSocket(new UnixDomainServerSocket(pathName, jni)); |
| 53 | + } |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + public abstract void setSoTimeout(int timeout) throws SocketException; |
| 58 | + |
| 59 | + public abstract SocketWrapper accept() throws IOException; |
| 60 | + |
| 61 | + public abstract void close() throws IOException; |
| 62 | + |
| 63 | + public static ServerSocketWrapper fromServerSocket(final ServerSocket socket) { |
| 64 | + return new ServerSocketImpl(socket); |
| 65 | + } |
| 66 | + |
| 67 | + public static ServerSocketWrapper fromServerSocketChannel(final ServerSocketChannel channel) { |
| 68 | + return new ServerSocketChannelImpl(channel); |
| 69 | + } |
| 70 | + |
| 71 | + private static final class ServerSocketImpl extends ServerSocketWrapper { |
| 72 | + private final ServerSocket socket; |
| 73 | + |
| 74 | + ServerSocketImpl(ServerSocket socket) { |
| 75 | + this.socket = socket; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void setSoTimeout(int timeout) throws SocketException { |
| 80 | + socket.setSoTimeout(timeout); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public SocketWrapper accept() throws IOException { |
| 85 | + return SocketWrapper.fromSocket(socket.accept()); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void close() throws IOException { |
| 90 | + socket.close(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static final class ServerSocketChannelImpl extends ServerSocketWrapper { |
| 95 | + private final ServerSocketChannel channel; |
| 96 | + |
| 97 | + ServerSocketChannelImpl(ServerSocketChannel channel) { |
| 98 | + this.channel = channel; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void setSoTimeout(int timeout) throws SocketException {} |
| 103 | + |
| 104 | + @Override |
| 105 | + public SocketWrapper accept() throws IOException { |
| 106 | + return SocketWrapper.fromByteChannel(channel.accept()); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void close() throws IOException { |
| 111 | + channel.close(); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments