]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/contributions/DumpHeapButtonTrim.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / contributions / DumpHeapButtonTrim.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.workbench.internal.contributions;\r
13 \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
19 \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
42 \r
43 /**\r
44  * @author Tuukka Lehtonen\r
45  */\r
46 public class DumpHeapButtonTrim extends Composite {\r
47 \r
48     private static final String PREF_HEAP_DUMP_PATH = "heap.dump.path";\r
49     IPath path;\r
50     LocalResourceManager resourceManager;\r
51     boolean disabled = false;\r
52 \r
53     /**\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
56      * interval.\r
57      * \r
58      * @param parent the parent composite\r
59      * @param prefStore the preference store\r
60      */\r
61     public DumpHeapButtonTrim(Composite parent) {\r
62         super(parent, SWT.NONE);\r
63 \r
64         restorePrefs();\r
65         setLayout(new FillLayout());\r
66 \r
67         final Button b = new Button(this, SWT.PUSH);\r
68         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);\r
69 \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
73             @Override\r
74             public void widgetSelected(SelectionEvent e) {\r
75                 if (disabled)\r
76                     return;\r
77                 dumpHeap();\r
78             }\r
79         });\r
80         if (getBean() == null) {\r
81             disabled = true;\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
85         }\r
86     }\r
87 \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
92     }\r
93 \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
97 \r
98         try {\r
99             prefs.flush();\r
100         } catch (BackingStoreException e) {\r
101             ErrorLogger.defaultLogError(e);\r
102         }\r
103     }\r
104 \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
111         }\r
112         String result = fd.open();\r
113         if (result == null)\r
114             return;\r
115 \r
116         path = new Path(result);\r
117         savePrefs();\r
118 \r
119         try {\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
123                 return;\r
124             }\r
125             new ProgressMonitorDialog(getShell()).run(true, false, new IRunnableWithProgress() {\r
126                 @Override\r
127                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
128                     monitor.beginTask("Creating heap dump '" + dumpFile + "'", IProgressMonitor.UNKNOWN);\r
129                     try {\r
130                         Object bean = getBean();\r
131                         if (bean == null)\r
132                             return;\r
133 \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
144                     } finally {\r
145                         monitor.done();\r
146                     }\r
147                 }\r
148             });\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
154         }\r
155     }\r
156 \r
157     private static Object getBean() {\r
158         Class<?> beanClass = getBeanClass();\r
159         if (beanClass == null)\r
160             return null;\r
161         try {\r
162             Object bean = ManagementFactory.newPlatformMXBeanProxy(\r
163                     ManagementFactory.getPlatformMBeanServer(),\r
164                     "com.sun.management:type=HotSpotDiagnostic",\r
165                     beanClass);\r
166             return bean;\r
167         } catch (IOException e) {\r
168             return null;\r
169         }\r
170     }\r
171 \r
172     private static Class<?> getBeanClass() {\r
173         try {\r
174             Class<?> clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");\r
175             return clazz;\r
176         } catch (ClassNotFoundException e) {\r
177             return null;\r
178         }\r
179     }\r
180 \r
181 }