-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOsHelper.java
More file actions
108 lines (91 loc) · 3.69 KB
/
OsHelper.java
File metadata and controls
108 lines (91 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.arloor.forwardproxy.util;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.kqueue.KQueueEventLoopGroup;
import io.netty.channel.kqueue.KQueueServerSocketChannel;
import io.netty.channel.kqueue.KQueueSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.management.ManagementFactory;
import java.util.Optional;
public class OsHelper {
private static final Logger logger = LoggerFactory.getLogger(OsHelper.class);
private static final OS os = parseOS();
public static Class<? extends ServerSocketChannel> serverSocketChannelClazz() {
return os.serverSocketChannelClazz;
}
public static Class<? extends SocketChannel> socketChannelClazz() {
return os.socketChannelClazz;
}
public static EventLoopGroup buildEventLoopGroup(int num) {
return os.buildEventLoopGroup(num);
}
public static boolean isUnix() {
return os.equals(OS.Unix);
}
public static boolean isWindows() {
return os.equals(OS.Windows);
}
public static boolean isMac() {
return os.equals(OS.MacOS);
}
private enum OS {
MacOS("mac", KQueueServerSocketChannel.class, KQueueSocketChannel.class) {
@Override
EventLoopGroup buildEventLoopGroup(int num) {
return new KQueueEventLoopGroup(num);
}
},
Unix("unix", EpollServerSocketChannel.class, EpollSocketChannel.class) {
@Override
EventLoopGroup buildEventLoopGroup(int num) {
return new EpollEventLoopGroup(num);
}
},
Windows("windows", NioServerSocketChannel.class, NioSocketChannel.class) {
@Override
EventLoopGroup buildEventLoopGroup(int num) {
return new NioEventLoopGroup(num);
}
},
Other("other", NioServerSocketChannel.class, NioSocketChannel.class) {
@Override
EventLoopGroup buildEventLoopGroup(int num) {
return new NioEventLoopGroup(num);
}
};
String name;
Class<? extends ServerSocketChannel> serverSocketChannelClazz;
Class<? extends SocketChannel> socketChannelClazz;
abstract EventLoopGroup buildEventLoopGroup(int num);
OS(String name, Class<? extends ServerSocketChannel> serverSocketChannelClass, Class<? extends SocketChannel> socketChannelClass) {
this.name = name;
this.serverSocketChannelClazz = serverSocketChannelClass;
this.socketChannelClazz = socketChannelClass;
}
}
private static OsHelper.OS parseOS() {
String osName = System.getProperty("os.name");
logger.info("当前系统为: " + osName);
String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.split("@")[0];
logger.info("该进程pid= " + pid);
osName = Optional.ofNullable(osName).orElse("").toLowerCase();
if ((osName.contains("win"))) {
return OS.Windows;
} else if (osName.contains("mac")) {
return OS.MacOS;
} else if (osName.contains("nix") || osName.contains("nux") || osName.indexOf("aix") > 0) {
return OS.Unix;
} else {
return OS.Other;
}
}
}