1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.workbench.internal.contributions;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.lang.management.ManagementFactory;
\r
17 import java.lang.reflect.InvocationTargetException;
\r
18 import java.lang.reflect.Method;
\r
20 import org.eclipse.core.runtime.IPath;
\r
21 import org.eclipse.core.runtime.IProgressMonitor;
\r
22 import org.eclipse.core.runtime.Path;
\r
23 import org.eclipse.core.runtime.preferences.InstanceScope;
\r
24 import org.eclipse.jface.dialogs.MessageDialog;
\r
25 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
\r
26 import org.eclipse.jface.operation.IRunnableWithProgress;
\r
27 import org.eclipse.jface.resource.JFaceResources;
\r
28 import org.eclipse.jface.resource.LocalResourceManager;
\r
29 import org.eclipse.swt.SWT;
\r
30 import org.eclipse.swt.events.SelectionAdapter;
\r
31 import org.eclipse.swt.events.SelectionEvent;
\r
32 import org.eclipse.swt.layout.FillLayout;
\r
33 import org.eclipse.swt.widgets.Button;
\r
34 import org.eclipse.swt.widgets.Composite;
\r
35 import org.eclipse.swt.widgets.FileDialog;
\r
36 import org.osgi.service.prefs.BackingStoreException;
\r
37 import org.osgi.service.prefs.Preferences;
\r
38 import org.simantics.utils.ui.ErrorLogger;
\r
39 import org.simantics.utils.ui.ExceptionUtils;
\r
40 import org.simantics.utils.ui.gfx.HSVAdjustmentImageDescriptor;
\r
41 import org.simantics.workbench.internal.Activator;
\r
44 * @author Tuukka Lehtonen
\r
46 public class DumpHeapButtonTrim extends Composite {
\r
48 private static final String PREF_HEAP_DUMP_PATH = "heap.dump.path";
\r
50 LocalResourceManager resourceManager;
\r
51 boolean disabled = false;
\r
54 * Creates a new heap status control with the given parent, and using
\r
55 * the given preference store to obtain settings such as the refresh
\r
58 * @param parent the parent composite
\r
59 * @param prefStore the preference store
\r
61 public DumpHeapButtonTrim(Composite parent) {
\r
62 super(parent, SWT.NONE);
\r
65 setLayout(new FillLayout());
\r
67 final Button b = new Button(this, SWT.PUSH);
\r
68 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);
\r
70 b.setToolTipText("Dump Java heap for debugging");
\r
71 b.setImage(resourceManager.createImage(Activator.getImageDescriptor("img/lorry.png")));
\r
72 b.addSelectionListener(new SelectionAdapter() {
\r
74 public void widgetSelected(SelectionEvent e) {
\r
80 if (getBean() == null) {
\r
82 b.setImage(resourceManager.createImage(HSVAdjustmentImageDescriptor.adjustSaturation(
\r
83 Activator.getImageDescriptor("img/lorry.png"), 0)));
\r
84 b.setToolTipText("Sorry, Java heap dumping not available, JVM does not support HotSpotDiagnosticMXBean.");
\r
88 private void restorePrefs() {
\r
89 Preferences prefs = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
\r
90 String p = prefs.get(PREF_HEAP_DUMP_PATH, null);
\r
91 path = p == null ? null : new Path(p);
\r
94 private void savePrefs() {
\r
95 Preferences prefs = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
\r
96 prefs.put(PREF_HEAP_DUMP_PATH, path.toPortableString());
\r
100 } catch (BackingStoreException e) {
\r
101 ErrorLogger.defaultLogError(e);
\r
105 private void dumpHeap() {
\r
106 FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
\r
107 fd.setFilterExtensions(new String[] { "*.hprof" });
\r
108 if (path != null) {
\r
109 fd.setFileName(path.lastSegment());
\r
110 fd.setFilterPath(path.removeLastSegments(1).toOSString());
\r
112 String result = fd.open();
\r
113 if (result == null)
\r
116 path = new Path(result);
\r
120 final File dumpFile = path.toFile();
\r
121 if (dumpFile.exists() && !dumpFile.delete()) {
\r
122 MessageDialog.openError(getShell(), "Delete Failed", "Could not delete old heap dump file '" + dumpFile + "'.\n\nIs the file still in use?");
\r
125 new ProgressMonitorDialog(getShell()).run(true, false, new IRunnableWithProgress() {
\r
127 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
128 monitor.beginTask("Creating heap dump '" + dumpFile + "'", IProgressMonitor.UNKNOWN);
\r
130 Object bean = getBean();
\r
134 Method m = bean.getClass().getMethod("dumpHeap", String.class, boolean.class);
\r
135 m.invoke(bean, path.toOSString(), true);
\r
136 } catch (IllegalArgumentException e) {
\r
137 throw new InvocationTargetException(e);
\r
138 } catch (IllegalAccessException e) {
\r
139 throw new InvocationTargetException(e);
\r
140 } catch (SecurityException e) {
\r
141 throw new InvocationTargetException(e);
\r
142 } catch (NoSuchMethodException e) {
\r
143 throw new InvocationTargetException(e);
\r
149 } catch (InvocationTargetException e) {
\r
150 Throwable t = e.getTargetException();
\r
151 ExceptionUtils.logAndShowError(t);
\r
152 } catch (InterruptedException e) {
\r
153 ExceptionUtils.logAndShowError(e);
\r
157 private static Object getBean() {
\r
158 Class<?> beanClass = getBeanClass();
\r
159 if (beanClass == null)
\r
162 Object bean = ManagementFactory.newPlatformMXBeanProxy(
\r
163 ManagementFactory.getPlatformMBeanServer(),
\r
164 "com.sun.management:type=HotSpotDiagnostic",
\r
167 } catch (IOException e) {
\r
172 private static Class<?> getBeanClass() {
\r
174 Class<?> clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
\r
176 } catch (ClassNotFoundException e) {
\r