forked from stackimpact/stackimpact-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentUtils.java
More file actions
124 lines (92 loc) · 3.01 KB
/
AgentUtils.java
File metadata and controls
124 lines (92 loc) · 3.01 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.stackimpact.agent;
import java.util.Random;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.security.MessageDigest;
import java.util.Formatter;
import java.util.UUID;
import java.lang.management.ManagementFactory;
public class AgentUtils {
public final static String AGENT_FRAME_PREFIX = "com.stackimpact.agent";
public static long timestamp() {
return System.currentTimeMillis() / 1000L;
}
public static long millis() {
return System.currentTimeMillis();
}
public static long nanos() {
return System.nanoTime();
}
public static String generateUUID() throws Exception {
UUID uuid = UUID.randomUUID();
return generateSHA1(uuid.toString());
}
public static String getSystemTempDir() {
if(System.getProperty("java.io.tmpdir") != null) {
return System.getProperty("java.io.tmpdir");
}
else {
return "/tmp"; // Unix only
}
}
public static String getPID() {
String pid = ManagementFactory.getRuntimeMXBean().getName();
int indexOf = pid.indexOf('@');
if (indexOf > 0) {
pid = pid.substring(0, indexOf);
}
return pid;
}
public static String getOSTag() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.indexOf("linux") >= 0) {
return "linux";
}
else if (osName.indexOf("mac") >= 0) {
return "macos";
}
else if (osName.indexOf("windows") >= 0) {
return "win";
}
return null;
}
public static int[] getJavaVersion() throws Exception {
String version = System.getProperty("java.runtime.version");
if (version == null) {
throw new Exception("java.runtime.version property is not available");
}
int major = 0;
Pattern p = Pattern.compile("^(?:1\\.)?(\\d+)");
Matcher m = p.matcher(version);
if (m.lookingAt()) {
major = Integer.parseInt(m.group(1));
}
int update = 0;
p = Pattern.compile(".*_(\\d+)");
m = p.matcher(version);
if (m.lookingAt()) {
update = Integer.parseInt(m.group(1));
}
if (major > 0) {
return new int[] {major, update};
}
else {
throw new Exception("Cannot parse java.runtime.verison property: " + version);
}
}
public static String generateSHA1(String str) throws Exception {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(str.getBytes("UTF-8"));
Formatter formatter = new Formatter();
for (byte b : crypt.digest()) {
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
public static boolean isAgentFrame(String name) {
return name.startsWith(AGENT_FRAME_PREFIX);
}
}