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 document post-1.8). Only try to add
the jar if both are available.
  • Loading branch information
apotterri committed Oct 10, 2023
1 parent 1c7abbf commit 16dc747
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions agent/src/main/java/com/appland/appmap/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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 @@ -46,30 +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 = 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);
}
}
addAgentJar(inst);
inst.addTransformer(new ClassFileTransformer());

try {
Expand Down Expand Up @@ -122,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 16dc747

Please sign in to comment.