Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlyn Normington2012-11-14 07:12:28 -0500
committerGlyn Normington2012-11-14 08:52:49 -0500
commit8f5a85f64272ef0849d1b583a73744cfd9bb2e32 (patch)
tree104356ef8513b34b2b02861d26a100605b142615
parentf6d79fbafb890a702b14632b427f171381dc7648 (diff)
downloadorg.eclipse.virgo.medic-8f5a85f64272ef0849d1b583a73744cfd9bb2e32.tar.gz
org.eclipse.virgo.medic-8f5a85f64272ef0849d1b583a73744cfd9bb2e32.tar.xz
org.eclipse.virgo.medic-8f5a85f64272ef0849d1b583a73744cfd9bb2e32.zip
393756: support heap dumping in Java 7
-rw-r--r--org.eclipse.virgo.medic.core/src/main/java/org/eclipse/virgo/medic/dump/impl/heap/HeapDumpContributor.java40
-rw-r--r--org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/dump/impl/HeapDumpContributorTests.java29
2 files changed, 56 insertions, 13 deletions
diff --git a/org.eclipse.virgo.medic.core/src/main/java/org/eclipse/virgo/medic/dump/impl/heap/HeapDumpContributor.java b/org.eclipse.virgo.medic.core/src/main/java/org/eclipse/virgo/medic/dump/impl/heap/HeapDumpContributor.java
index 2ff1929..4083594 100644
--- a/org.eclipse.virgo.medic.core/src/main/java/org/eclipse/virgo/medic/dump/impl/heap/HeapDumpContributor.java
+++ b/org.eclipse.virgo.medic.core/src/main/java/org/eclipse/virgo/medic/dump/impl/heap/HeapDumpContributor.java
@@ -12,16 +12,22 @@
package org.eclipse.virgo.medic.dump.impl.heap;
import java.io.PrintWriter;
+import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import javax.management.MBeanServer;
+
import org.eclipse.virgo.medic.dump.Dump;
import org.eclipse.virgo.medic.dump.DumpContributionFailedException;
import org.eclipse.virgo.medic.dump.DumpContributor;
-
public final class HeapDumpContributor implements DumpContributor {
+ private static final MBeanServer MBEAN_SERVER = ManagementFactory.getPlatformMBeanServer();
+
+ private static final String HOTSPOT_DIAGNOSTIC_MBEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
+
private final Method heapDumpMethod;
private final Object diagnosticMBean;
@@ -31,14 +37,34 @@ public final class HeapDumpContributor implements DumpContributor {
Method heapDumpMethod = null;
Object diagnosticMBean = null;
+ // Java 7 strategy
try {
- Class<?> managementFactoryClass = Class.forName("sun.management.ManagementFactory", true, HeapDumpContributor.class.getClassLoader());
- Method method = managementFactoryClass.getMethod("getDiagnosticMXBean");
- diagnosticMBean = method.invoke(null);
- heapDumpMethod = diagnosticMBean.getClass().getMethod("dumpHeap", String.class, boolean.class);
+ Class<?> diagnosticMbeanClass = Class.forName("com.sun.management.HotSpotDiagnosticMXBean", true,
+ HeapDumpContributor.class.getClassLoader());
+ diagnosticMBean = ManagementFactory.newPlatformMXBeanProxy(MBEAN_SERVER, HOTSPOT_DIAGNOSTIC_MBEAN_NAME, diagnosticMbeanClass);
} catch (Exception e) {
- heapDumpMethod = null;
- diagnosticMBean = null;
+ // Ignore
+ }
+
+ if (diagnosticMBean == null) {
+ // Java 6 strategy
+ try {
+ Class<?> managementFactoryClass = Class.forName("sun.management.ManagementFactory", true, HeapDumpContributor.class.getClassLoader());
+ Method method = managementFactoryClass.getMethod("getDiagnosticMXBean");
+ diagnosticMBean = method.invoke(null);
+ } catch (Exception e) {
+ heapDumpMethod = null;
+ diagnosticMBean = null;
+ }
+ }
+
+ if (diagnosticMBean != null) {
+ try {
+ heapDumpMethod = diagnosticMBean.getClass().getMethod("dumpHeap", String.class, boolean.class);
+ } catch (Exception e) {
+ heapDumpMethod = null;
+ diagnosticMBean = null;
+ }
}
this.heapDumpMethod = heapDumpMethod;
diff --git a/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/dump/impl/HeapDumpContributorTests.java b/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/dump/impl/HeapDumpContributorTests.java
index 08042c8..fe5ce36 100644
--- a/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/dump/impl/HeapDumpContributorTests.java
+++ b/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/dump/impl/HeapDumpContributorTests.java
@@ -15,9 +15,12 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
+import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
+import javax.management.MBeanServer;
+
import org.eclipse.virgo.medic.dump.Dump;
import org.eclipse.virgo.medic.dump.DumpContributionFailedException;
import org.eclipse.virgo.medic.dump.DumpContributor;
@@ -26,9 +29,12 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-
public class HeapDumpContributorTests {
+ private static final MBeanServer MBEAN_SERVER = ManagementFactory.getPlatformMBeanServer();
+
+ private static final String HOTSPOT_DIAGNOSTIC_MBEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
+
private final File dumpDirectory = new File("target");
@Before
@@ -52,15 +58,26 @@ public class HeapDumpContributorTests {
contributor.contribute(dump);
- boolean managementFactoryAvailable;
+ boolean diagnostMbeanAvailable;
try {
- Class.forName("sun.management.ManagementFactory").getMethod("getDiagnosticMXBean");
- managementFactoryAvailable = true;
+ Class<?> diagnosticMbeanClass = Class.forName("com.sun.management.HotSpotDiagnosticMXBean", true,
+ HeapDumpContributor.class.getClassLoader());
+ ManagementFactory.newPlatformMXBeanProxy(MBEAN_SERVER, HOTSPOT_DIAGNOSTIC_MBEAN_NAME, diagnosticMbeanClass);
+ diagnostMbeanAvailable = true;
} catch (Exception e) {
- managementFactoryAvailable = false;
+ diagnostMbeanAvailable = false;
+ }
+
+ if (!diagnostMbeanAvailable) {
+ try {
+ Class.forName("sun.management.ManagementFactory").getMethod("getDiagnosticMXBean");
+ diagnostMbeanAvailable = true;
+ } catch (Exception e) {
+ diagnostMbeanAvailable = false;
+ }
}
- assertEquals(managementFactoryAvailable, new File(this.dumpDirectory, "heap.out").exists());
+ assertEquals(diagnostMbeanAvailable, new File(this.dumpDirectory, "heap.out").exists());
}
}

Back to the top