/
OSUtils.java
236 lines (210 loc) · 5.93 KB
/
OSUtils.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.laytonsmith.PureUtilities.Common;
import com.laytonsmith.PureUtilities.CommandExecutor;
import com.laytonsmith.PureUtilities.JavaVersion;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
/**
* This class contains utilities that help with OS specific tasks.
*
*/
public class OSUtils {
public static enum OS {
WINDOWS,
MAC,
LINUX,
SOLARIS,
UNKNOWN;
/**
* Returns true if this is {@link #WINDOWS}
* @return
*/
public boolean isWindows() {
return this == WINDOWS;
}
/**
* Returns true if this is {@link #MAC}
* @return
*/
public boolean isMac() {
return this == MAC;
}
/**
* Returns true if this is {@link #LINUX}
* @return
*/
public boolean isLinux() {
return this == LINUX;
}
/**
* Returns true if this is {@link #SOLARIS}
* @return
*/
public boolean isSolaris() {
return this == SOLARIS;
}
/**
* Returns true if this is {@link #UNKNOWN}
* @return
*/
public boolean isUnknown() {
return this == UNKNOWN;
}
/**
* Returns true if this is a strict UNIX implementation, that is, {@link #MAC} or {@link #SOLARIS}
*
* @return
*/
public boolean isUnix() {
return this == MAC || this == SOLARIS;
}
/**
* Returns true if this {@link #isUnix()} returns true, or if this is {@link #LINUX}. This is a less strict
* category than {@link #isUnix()}, because for most purposes, Linux is UNIX compatible, but this is not
* strictly the case. Depending on what you're doing, you may need to differentiate between strict UNIX OSes
* or not, so if this matters, you'll need to do a more granular check.
* @return
*/
public boolean isUnixLike() {
return isUnix() || this == LINUX;
}
}
/**
* Returns the OS that is currently running
*
* @return
*/
public static OS GetOS() {
String os = System.getProperty("os.name").toLowerCase();
if(os.contains("win")) {
return OS.WINDOWS;
} else if(os.contains("mac")) {
return OS.MAC;
} else if(os.contains("nix") || os.contains("nux")) {
return OS.LINUX;
} else if(os.contains("sunos")) {
return OS.SOLARIS;
} else {
return OS.UNKNOWN;
}
}
public static String GetLineEnding() {
return System.getProperty("line.separator");
}
/**
* Returns the process id of the currently running Java process. In Java versions < 9, this actually just a best
* effort, and it is possible in some implementations of Java, this will cause an Exception. However, if Java 9 or
* greater is running, this will be an authoritative response.
* @return
* @throws UnsupportedOperationException If the java implementation does not have correct support for this
* operation.
*/
@SuppressWarnings("checkstyle:localvariablename")
public static long GetMyPid() throws UnsupportedOperationException {
if(JavaVersion.getMajorVersion() >= 9) {
try {
// Since we compile against Java 8, we can't directly use the class, and have to use reflection.
// If Java 8 support is dropped, this can be uncommented and simplified.
// return ProcessHandle.current().pid();
Class ProcessHandle = Class.forName("java.lang.ProcessHandle");
Object current = ReflectionUtils.invokeMethod(ProcessHandle, null, "current");
Object pid = ReflectionUtils.invokeMethod(ProcessHandle, current, "pid");
return (Long) pid;
} catch (ClassNotFoundException ex) {
throw new UnsupportedOperationException(ex);
}
} else {
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
final int index = jvmName.indexOf('@');
try {
return Long.parseLong(jvmName.substring(0, index));
} catch (NumberFormatException e) {
//
}
throw new UnsupportedOperationException("Cannot find pid");
}
}
public static class ProcessInfo {
private final long pid;
private final String command;
ProcessInfo(long pid, String command) {
this.pid = pid;
this.command = command;
}
/**
* Returns the process id of the given process.
* @return
*/
public long getPid() {
return pid;
}
/**
* Returns the process name, i.e. "nginx" on linux or "cmd.exe" on windows.
* @return
*/
public String getCommand() {
return command;
}
@Override
public String toString() {
return pid + ": " + command;
}
}
/**
* Returns a list of processes running on this system.
* @return
*/
public static List<ProcessInfo> GetRunningProcesses() {
try {
if(GetOS().isWindows()) {
return GetRunningProcessesWindows();
} else if(GetOS().isUnixLike()) {
return GetRunningProcessesUnix();
} else {
throw new UnsupportedOperationException("Unsupported OS");
}
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(ex);
}
}
private static List<ProcessInfo> GetRunningProcessesWindows() throws InterruptedException, IOException {
List<ProcessInfo> list = new ArrayList<>();
String cmd = CommandExecutor.Execute("tasklist.exe /fo list");
String imageName = null;
String pid = null;
for(String line : cmd.split("\n|\r\n|\n\r")) {
line = line.trim();
if(line.isEmpty()) {
if(imageName != null) {
list.add(new ProcessInfo(Long.parseLong(pid), imageName));
}
continue;
}
if(line.startsWith("Image Name")) {
imageName = line.replaceAll("Image Name:\\s*(.*)", "$1");
continue;
}
if(line.startsWith("PID")) {
pid = line.replaceAll("PID:\\s*(.*)", "$1");
}
}
return list;
}
private static List<ProcessInfo> GetRunningProcessesUnix() throws InterruptedException, IOException {
List<ProcessInfo> list = new ArrayList<>();
String cmd = CommandExecutor.Execute("ps -ea");
boolean first = true;
for(String line : cmd.split("\n|\r\n|\n\r")) {
if(first) {
// First line is header
first = false;
continue;
}
line = line.trim();
String[] params = line.split("\\s+", 4);
list.add(new ProcessInfo(Long.parseLong(params[0]), params[3]));
}
return list;
}
}