Skip to content

Commit 4f03507

Browse files
authored
Add support of spring boot nested jar for SymDB (#7678)
Spring boot use a special jar organisation for nested ones. It uses a new jar protocol for URL: jar:nested:
1 parent 010e6f7 commit 4f03507

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/symbol/JarScanner.java

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class JarScanner {
1313
private static final Logger LOGGER = LoggerFactory.getLogger(JarScanner.class);
1414
private static final String JAR_FILE_PREFIX = "jar:file:";
15+
private static final String JAR_NESTED_PREFIX = "jar:nested:";
1516
private static final String FILE_PREFIX = "file:";
1617
// Spring prefixes:
1718
// https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html
@@ -41,6 +42,11 @@ public static Path extractJarPath(ProtectionDomain protectionDomain) throws URIS
4142
if (idx != -1) {
4243
return getPathFromPrefixedFileName(locationStr, JAR_FILE_PREFIX, idx);
4344
}
45+
} else if (locationStr.startsWith(JAR_NESTED_PREFIX)) {
46+
int idx = locationStr.indexOf("/!BOOT-INF/");
47+
if (idx != -1) {
48+
return getPathFromPrefixedFileName(locationStr, JAR_NESTED_PREFIX, idx);
49+
}
4450
} else if (locationStr.startsWith(FILE_PREFIX)) {
4551
return getPathFromPrefixedFileName(locationStr, FILE_PREFIX, locationStr.length());
4652
}

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/symbol/JarScannerTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.datadog.debugger.symbol;
22

33
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.when;
46

57
import java.net.MalformedURLException;
68
import java.net.URISyntaxException;
79
import java.net.URL;
810
import java.net.URLClassLoader;
11+
import java.security.CodeSource;
12+
import java.security.ProtectionDomain;
13+
import java.security.cert.Certificate;
914
import org.junit.jupiter.api.Test;
1015

1116
class JarScannerTest {
@@ -31,4 +36,15 @@ public void extractJarPathFromFile() throws ClassNotFoundException, URISyntaxExc
3136
Class<?> testClass = urlClassLoader.loadClass(CLASS_NAME);
3237
assertEquals(jarFileUrl.getFile(), JarScanner.extractJarPath(testClass).toString());
3338
}
39+
40+
@Test
41+
public void extractJarPathFromNestedJar() throws URISyntaxException {
42+
URL jarFileUrl = getClass().getResource("/debugger-symbol.jar");
43+
URL mockLocation = mock(URL.class);
44+
when(mockLocation.toString())
45+
.thenReturn("jar:nested:" + jarFileUrl.getFile() + "/!BOOT-INF/classes/!");
46+
CodeSource codeSource = new CodeSource(mockLocation, (Certificate[]) null);
47+
ProtectionDomain protectionDomain = new ProtectionDomain(codeSource, null);
48+
assertEquals(jarFileUrl.getFile(), JarScanner.extractJarPath(protectionDomain).toString());
49+
}
3450
}

0 commit comments

Comments
 (0)