-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathJarScanner.java
More file actions
78 lines (72 loc) · 2.92 KB
/
JarScanner.java
File metadata and controls
78 lines (72 loc) · 2.92 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
package com.datadog.debugger.symbol;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JarScanner {
private static final Logger LOGGER = LoggerFactory.getLogger(JarScanner.class);
private static final String JAR_FILE_PREFIX = "jar:file:";
private static final String JAR_NESTED_PREFIX = "jar:nested:";
private static final String FILE_PREFIX = "file:";
// Spring prefixes:
// https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html
private static final String SPRING_CLASSES_PREFIX = "BOOT-INF/classes/";
private static final String SPRING_DEPS_PREFIX = "BOOT-INF/lib/";
public static Path extractJarPath(Class<?> clazz, SymDBReport symDBReport)
throws URISyntaxException {
return extractJarPath(clazz.getProtectionDomain(), symDBReport);
}
public static Path extractJarPath(ProtectionDomain protectionDomain, SymDBReport symDBReport) {
if (protectionDomain == null) {
return null;
}
CodeSource codeSource = protectionDomain.getCodeSource();
if (codeSource == null) {
return null;
}
URL location = codeSource.getLocation();
if (location == null) {
return null;
}
String locationStr = location.toString();
LOGGER.debug("CodeSource Location={}", locationStr);
if (locationStr.startsWith(JAR_FILE_PREFIX)) {
int idx = locationStr.indexOf("!/");
if (idx != -1) {
return getPathFromPrefixedFileName(locationStr, JAR_FILE_PREFIX, idx);
}
} else if (locationStr.startsWith(JAR_NESTED_PREFIX)) {
int idx = locationStr.indexOf("/!BOOT-INF/");
if (idx != -1) {
return getPathFromPrefixedFileName(locationStr, JAR_NESTED_PREFIX, idx);
}
} else if (locationStr.startsWith(FILE_PREFIX)) {
return getPathFromPrefixedFileName(locationStr, FILE_PREFIX, locationStr.length());
}
symDBReport.addLocationError(locationStr);
return null;
}
public static String trimPrefixes(String classFilePath) {
if (classFilePath.startsWith(SPRING_CLASSES_PREFIX)) {
return classFilePath.substring(SPRING_CLASSES_PREFIX.length());
}
return classFilePath;
}
private static Path getPathFromPrefixedFileName(String locationStr, String prefix, int endIdx) {
String fileName = locationStr.substring(prefix.length(), endIdx);
LOGGER.debug("jar filename={}", fileName);
try {
// Reconstruct a file URI and use Paths.get(URI) to correctly handle
// platform-specific paths, including Windows drive letters (e.g. /C:/...)
return Paths.get(new URI("file:" + fileName));
} catch (URISyntaxException e) {
LOGGER.debug("Failed to parse as URI: {}, falling back to direct path", fileName, e);
return Paths.get(fileName);
}
}
}