From 3f1e796db12fe3956a6a82bca49dfd9b22d8027a Mon Sep 17 00:00:00 2001 From: Alan Potter Date: Tue, 10 Oct 2023 06:15:43 -0400 Subject: [PATCH] fix: only add the agent jar if it can be located 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. --- .../main/java/com/appland/appmap/Agent.java | 61 +++++++++++-------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/agent/src/main/java/com/appland/appmap/Agent.java b/agent/src/main/java/com/appland/appmap/Agent.java index b1f93245..98d70041 100644 --- a/agent/src/main/java/com/appland/appmap/Agent.java +++ b/agent/src/main/java/com/appland/appmap/Agent.java @@ -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; @@ -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 { @@ -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); + } + } + } }