Skip to content

Commit

Permalink
fix: only add the agent jar if it can be located
Browse files Browse the repository at this point in the history
The CodeSource and location of the agent jar's ProtectionDomain can both
be null (though the latter was only documented post-1.8). Only try to
add the jar if both are available.

Also, when the location URL is not null, convert it to a URI instead of
a Path. This works on all platforms.
  • Loading branch information
apotterri authored and dustinbyrne committed Oct 12, 2023
1 parent 5eb40da commit ca4bc1b
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions agent/src/main/java/com/appland/appmap/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -45,22 +48,7 @@ public static void premain(String agentArgs, Instrumentation inst) {
logger.debug("System properties: {}", System.getProperties());
logger.debug(new Exception(), "whereAmI");

URL jarURL = Agent.class.getProtectionDomain().getCodeSource().getLocation();
Path agentJar = Paths.get(jarURL.getPath());
// During testing of the agent itself, classes get loaded from a directory.
// The rest of the time (i.e. when it's actually deployed), they'll always
// come from a jar file.
JarFile jarFile = null;
if (!Files.isDirectory(agentJar)) {
try {
jarFile = new JarFile(Paths.get(jarURL.getPath()).toFile());
inst.appendToSystemClassLoaderSearch(jarFile);
} catch (IOException e) {
System.err.println("Failed to load the agent jar");
e.printStackTrace();
System.exit(1);
}
}
addAgentJar(inst);
inst.addTransformer(new ClassFileTransformer());

try {
Expand Down Expand Up @@ -113,6 +101,40 @@ public static void premain(String agentArgs, Instrumentation inst) {
}
}

private static void addAgentJar(Instrumentation inst) {
ProtectionDomain protectionDomain = Agent.class.getProtectionDomain();
CodeSource codeSource;
URL jarURL;
if (((codeSource = protectionDomain.getCodeSource()) == null)
|| ((jarURL = codeSource.getLocation()) == null)) {
// Nothing we can do if we can't locate the agent jar
return;
}

Path agentJar = null;
try {
agentJar = Paths.get(jarURL.toURI());
} catch (URISyntaxException e) {
// Doesn't seem like this should ever happen....
System.err.println("Failed getting path to agent jar");
e.printStackTrace();
System.exit(1);
}
// During testing of the agent itself, classes get loaded from a directory.
// The rest of the time (i.e. when it's actually deployed), they'll always
// come from a jar file.
JarFile jarFile = null;
if (!Files.isDirectory(agentJar)) {
try {
jarFile = new JarFile(agentJar.toFile());
inst.appendToSystemClassLoaderSearch(jarFile);
} catch (IOException e) {
System.err.println("Failed to load the agent jar");
e.printStackTrace();
System.exit(1);
}
}
}


}

0 comments on commit ca4bc1b

Please sign in to comment.