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