Skip to content

Commit

Permalink
Issue #73: Add support for IBM JDK local attach
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Sep 2, 2016
1 parent 717c04e commit 124ea8d
Showing 1 changed file with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
//import sun.tools.attach.HotSpotVirtualMachine;

/**
* Wrapper around tools.jar classes to be loaded using isolated classloader.
Expand All @@ -61,9 +60,9 @@
// This has to be called by reflection so it can as well be private to stress this is not an API
@SuppressWarnings("unused")
private static MBeanServerConnection getServerConnection(int pid) {
VirtualMachine vm = getVm(pid);

try {
JMXServiceURL serviceURL = new JMXServiceURL(connectorAddress(vm));
JMXServiceURL serviceURL = new JMXServiceURL(connectorAddress(pid));
return JMXConnectorFactory.connect(serviceURL).getMBeanServerConnection();
} catch (MalformedURLException ex) {
throw failed("JMX connection failed", ex);
Expand All @@ -83,7 +82,9 @@ private static VirtualMachine getVm(int pid) {
}
}

private static String connectorAddress(VirtualMachine vm) throws IOException {
private static String connectorAddress(int pid) throws IOException {
VirtualMachine vm = getVm(pid);

String address = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
if (address != null) return address;

Expand Down Expand Up @@ -150,6 +151,31 @@ private static String connectorAddress(VirtualMachine vm) throws IOException {
diag.add("management-agent.jar not found");
}

// IBM JDK uses specific class hierarchy
// Note this require both JVMs are IBM ones
try {
Class<?> ibmVmClass = Class.forName("com.ibm.tools.attach.VirtualMachine");
if (ibmVmClass.isInstance(vm)) {
Method attach = ibmVmClass.getMethod("attach", String.class);
Object ibmVm = attach.invoke(null, String.valueOf(pid));

Method method = ibmVm.getClass().getMethod("getTargetProperties", Boolean.class);
Properties props = (Properties) method.invoke(ibmVmClass, true);

address = props.getProperty(CONNECTOR_ADDRESS);
if (address != null) return address;

diag.add("IBM JDK attach successful - no address provided");
}
} catch (ClassNotFoundException e) {
diag.add("not an IBM JDK - unable to create local JMX connection; try HOSTNAME:PORT instead");
} catch (NoSuchMethodException e) {
diag.add("IBM JDK does not seem to support attach");
} catch (InvocationTargetException e) {
throw new AssertionError(e);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}

throw failedUnsupported("Unable to connect to JVM: " + diag.toString(), systemProperties);
}
Expand Down

0 comments on commit 124ea8d

Please sign in to comment.