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 java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.net.SocketAddress;
21 import java.nio.file.Path;
22
23 final class NioDomainSocketUtil {
24
25 private static final Method OF_METHOD;
26 private static final Method GET_PATH_METHOD;
27
28 static {
29 Method ofMethod;
30 Method getPathMethod;
31 try {
32 Class<?> clazz = Class.forName("java.net.UnixDomainSocketAddress");
33 ofMethod = clazz.getMethod("of", String.class);
34 getPathMethod = clazz.getMethod("getPath");
35
36 } catch (Throwable error) {
37 ofMethod = null;
38 getPathMethod = null;
39 }
40 OF_METHOD = ofMethod;
41 GET_PATH_METHOD = getPathMethod;
42 }
43
44 static SocketAddress newUnixDomainSocketAddress(String path) {
45 if (OF_METHOD == null) {
46 throw new IllegalStateException();
47 }
48 try {
49 return (SocketAddress) OF_METHOD.invoke(null, path);
50 } catch (IllegalAccessException | InvocationTargetException e) {
51 throw new IllegalStateException(e);
52 }
53 }
54
55 static void deleteSocketFile(SocketAddress address) {
56 if (GET_PATH_METHOD == null) {
57 throw new IllegalStateException();
58 }
59 try {
60 Path path = (Path) GET_PATH_METHOD.invoke(address);
61 if (path != null) {
62 path.toFile().delete();
63 }
64 } catch (IllegalAccessException | InvocationTargetException e) {
65 throw new IllegalStateException(e);
66 }
67 }
68
69 private NioDomainSocketUtil() { }
70 }