}
}
@Nonnull
protected MBeanServerConnection connectToMbeanServer(@Nonnull String pid) throws IOException {
VirtualMachine vm;
try {
Integer.parseInt(pid);
} catch (Exception e) {
logger.warn("Exception parsing PID '{}'", pid, e);
}
try {
vm = VirtualMachine.attach(pid);
} catch (Exception e) {
throw new IllegalStateException("Exception attaching VM with PID '" + pid + "'", e);
}
logger.trace("VM Agent Properties");
for (String key : new TreeSet<String>(vm.getAgentProperties().stringPropertyNames())) {
logger.trace("\t {}: {}", key, vm.getAgentProperties().get(key));
}
logger.trace("VM System Properties");
for (String key : new TreeSet<String>(vm.getSystemProperties().stringPropertyNames())) {
logger.trace("\t {}: {}", key, vm.getSystemProperties().get(key));
}
String connectorAddress =
vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (connectorAddress == null) {
String agent = vm.getSystemProperties().getProperty(
"java.home") + File.separator + "lib" + File.separator +
"management-agent.jar";
try {
vm.loadAgent(agent);
} catch (Exception e) {
throw new IllegalStateException("Exception loading agent " + agent);
}
connectorAddress = vm.getAgentProperties().getProperty(
"com.sun.management.jmxremote.localConnectorAddress");
}
if (connectorAddress == null) {
throw new IllegalStateException("Could not attach to pid: " + pid + ". No connector available");