--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.workbench.internal.contributions;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.lang.management.ManagementFactory;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.lang.reflect.Method;\r
+\r
+import org.eclipse.core.runtime.IPath;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.osgi.service.prefs.BackingStoreException;\r
+import org.osgi.service.prefs.Preferences;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+import org.simantics.utils.ui.gfx.HSVAdjustmentImageDescriptor;\r
+import org.simantics.workbench.internal.Activator;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DumpHeapButtonTrim extends Composite {\r
+\r
+ private static final String PREF_HEAP_DUMP_PATH = "heap.dump.path";\r
+ IPath path;\r
+ LocalResourceManager resourceManager;\r
+ boolean disabled = false;\r
+\r
+ /**\r
+ * Creates a new heap status control with the given parent, and using\r
+ * the given preference store to obtain settings such as the refresh\r
+ * interval.\r
+ * \r
+ * @param parent the parent composite\r
+ * @param prefStore the preference store\r
+ */\r
+ public DumpHeapButtonTrim(Composite parent) {\r
+ super(parent, SWT.NONE);\r
+\r
+ restorePrefs();\r
+ setLayout(new FillLayout());\r
+\r
+ final Button b = new Button(this, SWT.PUSH);\r
+ this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);\r
+\r
+ b.setToolTipText("Dump Java heap for debugging");\r
+ b.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/lorry.png")));\r
+ b.addSelectionListener(new SelectionAdapter() {\r
+ @Override\r
+ public void widgetSelected(SelectionEvent e) {\r
+ if (disabled)\r
+ return;\r
+ dumpHeap();\r
+ }\r
+ });\r
+ if (getBean() == null) {\r
+ disabled = true;\r
+ b.setImage(resourceManager.createImage(HSVAdjustmentImageDescriptor.adjustSaturation(\r
+ Activator.getImageDescriptor("img/lorry.png"), 0)));\r
+ b.setToolTipText("Sorry, Java heap dumping not available, JVM does not support HotSpotDiagnosticMXBean.");\r
+ }\r
+ }\r
+\r
+ private void restorePrefs() {\r
+ Preferences prefs = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);\r
+ String p = prefs.get(PREF_HEAP_DUMP_PATH, null);\r
+ path = p == null ? null : new Path(p);\r
+ }\r
+\r
+ private void savePrefs() {\r
+ Preferences prefs = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);\r
+ prefs.put(PREF_HEAP_DUMP_PATH, path.toPortableString());\r
+\r
+ try {\r
+ prefs.flush();\r
+ } catch (BackingStoreException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ }\r
+ }\r
+\r
+ private void dumpHeap() {\r
+ FileDialog fd = new FileDialog(getShell(), SWT.SAVE);\r
+ fd.setFilterExtensions(new String[] { "*.hprof" });\r
+ if (path != null) {\r
+ fd.setFileName(path.lastSegment());\r
+ fd.setFilterPath(path.removeLastSegments(1).toOSString());\r
+ }\r
+ String result = fd.open();\r
+ if (result == null)\r
+ return;\r
+\r
+ path = new Path(result);\r
+ savePrefs();\r
+\r
+ try {\r
+ final File dumpFile = path.toFile();\r
+ if (dumpFile.exists() && !dumpFile.delete()) {\r
+ MessageDialog.openError(getShell(), "Delete Failed", "Could not delete old heap dump file '" + dumpFile + "'.\n\nIs the file still in use?");\r
+ return;\r
+ }\r
+ new ProgressMonitorDialog(getShell()).run(true, false, new IRunnableWithProgress() {\r
+ @Override\r
+ public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+ monitor.beginTask("Creating heap dump '" + dumpFile + "'", IProgressMonitor.UNKNOWN);\r
+ try {\r
+ Object bean = getBean();\r
+ if (bean == null)\r
+ return;\r
+\r
+ Method m = bean.getClass().getMethod("dumpHeap", String.class, boolean.class);\r
+ m.invoke(bean, path.toOSString(), true);\r
+ } catch (IllegalArgumentException e) {\r
+ throw new InvocationTargetException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new InvocationTargetException(e);\r
+ } catch (SecurityException e) {\r
+ throw new InvocationTargetException(e);\r
+ } catch (NoSuchMethodException e) {\r
+ throw new InvocationTargetException(e);\r
+ } finally {\r
+ monitor.done();\r
+ }\r
+ }\r
+ });\r
+ } catch (InvocationTargetException e) {\r
+ Throwable t = e.getTargetException();\r
+ ExceptionUtils.logAndShowError(t);\r
+ } catch (InterruptedException e) {\r
+ ExceptionUtils.logAndShowError(e);\r
+ }\r
+ }\r
+\r
+ private static Object getBean() {\r
+ Class<?> beanClass = getBeanClass();\r
+ if (beanClass == null)\r
+ return null;\r
+ try {\r
+ Object bean = ManagementFactory.newPlatformMXBeanProxy(\r
+ ManagementFactory.getPlatformMBeanServer(),\r
+ "com.sun.management:type=HotSpotDiagnostic",\r
+ beanClass);\r
+ return bean;\r
+ } catch (IOException e) {\r
+ return null;\r
+ }\r
+ }\r
+\r
+ private static Class<?> getBeanClass() {\r
+ try {\r
+ Class<?> clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");\r
+ return clazz;\r
+ } catch (ClassNotFoundException e) {\r
+ return null;\r
+ }\r
+ }\r
+\r
+}
\ No newline at end of file