log.log(Level.INFO, "conf mounts: " + confBuilder.toString());
log.log(Level.INFO, "cmd mounts: " + cmdBuilder.toString());
//
CloseableList closeable = new CloseableList();
Shell shell;
if (pids != null && pids.size() > 0) {
//
if (interactive && pids.size() > 1) {
throw new Exception("Cannot attach to more than one JVM in interactive mode");
}
// Compute classpath
String classpath = System.getProperty("java.class.path");
String sep = System.getProperty("path.separator");
StringBuilder buffer = new StringBuilder();
for (String path : classpath.split(Pattern.quote(sep))) {
File file = new File(path);
if (file.exists()) {
if (buffer.length() > 0) {
buffer.append(' ');
}
buffer.append(file.getCanonicalPath());
}
}
// Create manifest
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.putValue("Agent-Class", Agent.class.getName());
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attributes.put(Attributes.Name.CLASS_PATH, buffer.toString());
// Create jar file
File agentFile = File.createTempFile("agent", ".jar");
agentFile.deleteOnExit();
JarOutputStream out = new JarOutputStream(new FileOutputStream(agentFile), manifest);
out.close();
log.log(Level.INFO, "Created agent jar " + agentFile.getCanonicalPath());
// Build the options
StringBuilder sb = new StringBuilder();
// Path configuration
sb.append("--cmd ");
Delimiter.EMPTY.escape(toString(cmdBuilder), sb);
sb.append(' ');
sb.append("--conf ");
Delimiter.EMPTY.escape(toString(confBuilder), sb);
sb.append(' ');
// Propagate canonical config
if (properties != null) {
for (String property : properties) {
sb.append("--property ");
Delimiter.EMPTY.escape(property, sb);
sb.append(' ');
}
}
//
if (interactive) {
RemoteServer server = new RemoteServer(0);
int port = server.bind();
log.log(Level.INFO, "Callback server set on port " + port);
sb.append(port);
String options = sb.toString();
Integer pid = pids.get(0);
final VirtualMachine vm = VirtualMachine.attach("" + pid);
log.log(Level.INFO, "Loading agent with command " + options + " as agent " + agentFile.getCanonicalPath());
vm.loadAgent(agentFile.getCanonicalPath(), options);
server.accept();
shell = server.getShell();
closeable.add(new Closeable() {
public void close() throws IOException {
vm.detach();
}
});
} else {