]> source.dussan.org Git - poi.git/commitdiff
[github-197] Modify test class HeapDump to allow it work in different JDKs. Thanks...
authorPJ Fanning <fanningpj@apache.org>
Fri, 13 Nov 2020 18:00:21 +0000 (18:00 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 13 Nov 2020 18:00:21 +0000 (18:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1883393 13f79535-47bb-0310-9956-ffa450edef68

src/integrationtest/org/apache/poi/stress/HeapDump.java

index 47736b504f8368a624c7870b57ab6df42e6f2818..098230cad104d76f18f8655d2e539f16de1fc7bc 100644 (file)
@@ -18,8 +18,11 @@ package org.apache.poi.stress;
 
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 
 import com.sun.management.HotSpotDiagnosticMXBean;
+import org.apache.poi.ss.formula.eval.NotImplementedException;
 import org.apache.poi.util.SuppressForbidden;
 
 @SuppressForbidden("class only exists for manual tests in XSSFFileHandler")
@@ -40,9 +43,18 @@ public class HeapDump {
      *             only the live objects
      */
     public static void dumpHeap(String fileName, boolean live) throws IOException {
-        // initialize hotspot diagnostic MBean
-        initHotspotMBean();
-        hotspotMBean.dumpHeap(fileName, live);
+        try {
+            if (isIbmVm()) {
+                dumpHeapJ9(fileName);
+            } else {
+
+                // initialize hotspot diagnostic MBean
+                initHotspotMBean();
+                dumpHeapHotSpot(fileName, live);
+            }
+        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     // initialize the hotspot diagnostic MBean field
@@ -59,6 +71,28 @@ public class HeapDump {
     // get the hotspot diagnostic MBean from the platform MBean server
     private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException {
         return ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
-                        HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
+                HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
+    }
+
+    private static boolean isIbmVm() {
+        try {
+            Class.forName("com.ibm.jvm.Dump");
+            return true;
+        } catch (ClassNotFoundException e) {
+            return false;
+        }
+    }
+
+    private static void dumpHeapJ9(String fileName) throws ClassNotFoundException, NoSuchMethodException,
+            InvocationTargetException, IllegalAccessException {
+        Class<?> dump = Class.forName("com.ibm.jvm.Dump");
+        Method heapDumpToFile = dump.getMethod("heapDumpToFile", String.class);
+        heapDumpToFile.invoke(dump, fileName);
+    }
+
+    private static void dumpHeapHotSpot(String fileName, boolean live) throws NoSuchMethodException,
+            InvocationTargetException, IllegalAccessException {
+        Method dumpHeap = hotspotMBean.getClass().getMethod("dumpHeap", String.class, boolean.class);
+        dumpHeap.invoke(hotspotMBean, fileName, live);
     }
-}
+}
\ No newline at end of file