Allow multiple agents to be attached

This commit is contained in:
Dave Syer 2014-05-01 09:41:18 +01:00
parent b884a73396
commit 438cff252d
1 changed files with 39 additions and 22 deletions

View File

@ -40,6 +40,8 @@ import org.springframework.boot.loader.tools.JavaExecutable;
import org.springframework.boot.loader.tools.MainClassFinder; import org.springframework.boot.loader.tools.MainClassFinder;
import org.springframework.boot.loader.tools.RunProcess; import org.springframework.boot.loader.tools.RunProcess;
import edu.emory.mathcs.backport.java.util.Arrays;
/** /**
* MOJO that can be used to run a executable archive application directly from Maven. * MOJO that can be used to run a executable archive application directly from Maven.
* *
@ -68,7 +70,7 @@ public class RunMojo extends AbstractMojo {
* Path to agent jar. * Path to agent jar.
*/ */
@Parameter(property = "run.agent") @Parameter(property = "run.agent")
private File agent; private File[] agent;
/** /**
* Flag to say that the agent requires -noverify. * Flag to say that the agent requires -noverify.
@ -111,7 +113,7 @@ public class RunMojo extends AbstractMojo {
private void findAgent() { private void findAgent() {
try { try {
if (this.agent == null) { if (this.agent == null || this.agent.length == 0) {
Class<?> loaded = Class.forName(SPRING_LOADED_AGENT_CLASSNAME); Class<?> loaded = Class.forName(SPRING_LOADED_AGENT_CLASSNAME);
if (loaded != null) { if (loaded != null) {
if (this.noverify == null) { if (this.noverify == null) {
@ -119,7 +121,7 @@ public class RunMojo extends AbstractMojo {
} }
CodeSource source = loaded.getProtectionDomain().getCodeSource(); CodeSource source = loaded.getProtectionDomain().getCodeSource();
if (source != null) { if (source != null) {
this.agent = new File(source.getLocation().getFile()); this.agent = new File[] { new File(source.getLocation().getFile()) };
} }
} }
} }
@ -133,36 +135,51 @@ public class RunMojo extends AbstractMojo {
} }
private void run(String startClassName) throws MojoExecutionException { private void run(String startClassName) throws MojoExecutionException {
findAgent(); List<String> args = new ArrayList<String>();
int extras = 0; addAgents(args);
if (this.agent != null) { addClasspath(args);
getLog().info("Attaching agent: " + this.agent); args.add(startClassName);
extras = 1; addArgs(args);
try {
new RunProcess(new JavaExecutable().toString()).run(args
.toArray(new String[args.size()]));
} }
if (this.noverify) { catch (Exception e) {
extras++; throw new MojoExecutionException("Could not exec java", e);
} }
String[] args = new String[this.arguments.length + extras + 3];
System.arraycopy(this.arguments, 0, args, extras + 3, this.arguments.length);
if (extras > 0) {
args[0] = "-javaagent:" + this.agent;
} }
if (this.noverify) {
args[1] = "-noverify"; private void addArgs(List<String> args) {
for (String arg : this.arguments) {
args.add(arg);
} }
args[extras + 2] = startClassName; }
private void addClasspath(List<String> args) throws MojoExecutionException {
try { try {
StringBuilder classpath = new StringBuilder(); StringBuilder classpath = new StringBuilder();
for (URL ele : getClassPathUrls()) { for (URL ele : getClassPathUrls()) {
classpath = classpath.append((classpath.length() > 0 ? File.pathSeparator classpath = classpath.append((classpath.length() > 0 ? File.pathSeparator
: "") + ele); : "") + ele);
} }
args[extras] = "-cp"; args.add("-cp");
args[extras + 1] = classpath.toString(); args.add(classpath.toString());
new RunProcess(new JavaExecutable().toString()).run(args);
} }
catch (Exception e) { catch (Exception e) {
throw new MojoExecutionException("Could not exec java", e); throw new MojoExecutionException("Could not build classpath", e);
}
}
private void addAgents(List<String> args) {
findAgent();
if (this.agent != null) {
getLog().info("Attaching agents: " + Arrays.asList(this.agent));
for (File agent : this.agent) {
args.add("-javaagent:" + agent);
}
}
if (this.noverify) {
args.add("-noverify");
} }
} }