|
Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 SAP AG. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* SAP AG - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.mat.ui.acquire; |
| 12 |
|
| 13 |
import java.io.File; |
| 14 |
import java.lang.reflect.InvocationTargetException; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.List; |
| 17 |
import java.util.logging.Level; |
| 18 |
import java.util.logging.Logger; |
| 19 |
|
| 20 |
import org.eclipse.core.commands.AbstractHandler; |
| 21 |
import org.eclipse.core.commands.ExecutionEvent; |
| 22 |
import org.eclipse.core.runtime.CoreException; |
| 23 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 24 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 25 |
import org.eclipse.core.runtime.IStatus; |
| 26 |
import org.eclipse.core.runtime.Path; |
| 27 |
import org.eclipse.core.runtime.Platform; |
| 28 |
import org.eclipse.core.runtime.Status; |
| 29 |
import org.eclipse.jface.action.Action; |
| 30 |
import org.eclipse.jface.action.IAction; |
| 31 |
import org.eclipse.jface.dialogs.ErrorDialog; |
| 32 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 33 |
import org.eclipse.jface.operation.IRunnableContext; |
| 34 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 35 |
import org.eclipse.jface.viewers.ISelection; |
| 36 |
import org.eclipse.jface.wizard.Wizard; |
| 37 |
import org.eclipse.jface.wizard.WizardDialog; |
| 38 |
import org.eclipse.mat.snapshot.acquire.IHeapDumpProvider; |
| 39 |
import org.eclipse.mat.snapshot.acquire.VmInfo; |
| 40 |
import org.eclipse.mat.ui.Messages; |
| 41 |
import org.eclipse.mat.ui.editor.PathEditorInput; |
| 42 |
import org.eclipse.mat.ui.util.ErrorHelper; |
| 43 |
import org.eclipse.mat.ui.util.ProgressMonitorWrapper; |
| 44 |
import org.eclipse.mat.util.IProgressListener; |
| 45 |
import org.eclipse.swt.widgets.Shell; |
| 46 |
import org.eclipse.ui.IEditorDescriptor; |
| 47 |
import org.eclipse.ui.IWorkbenchWindow; |
| 48 |
import org.eclipse.ui.IWorkbenchWindowActionDelegate; |
| 49 |
import org.eclipse.ui.PlatformUI; |
| 50 |
import org.eclipse.ui.ide.IDE; |
| 51 |
|
| 52 |
public class AcquireSnapshotAction extends Action implements IWorkbenchWindowActionDelegate |
| 53 |
{ |
| 54 |
|
| 55 |
public void run() |
| 56 |
{ |
| 57 |
final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 58 |
|
| 59 |
List<IHeapDumpProvider> dumpProviders = getProviders(); |
| 60 |
if (dumpProviders == null || dumpProviders.size() == 0) |
| 61 |
{ |
| 62 |
showError(Messages.AcquireSnapshotAction_NoProviderError); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
final AcquireDialog acquireDialog = new AcquireDialog(dumpProviders); |
| 67 |
|
| 68 |
Wizard wizard = new Wizard() { |
| 69 |
public boolean performFinish() |
| 70 |
{ |
| 71 |
|
| 72 |
VmInfo selectedProcess = acquireDialog.getProcess(); |
| 73 |
|
| 74 |
try |
| 75 |
{ |
| 76 |
String selectedPath = acquireDialog.getSelectedPath(); |
| 77 |
File preferredLocation = new File(selectedPath); |
| 78 |
if (!validatePath(preferredLocation)) return false; |
| 79 |
|
| 80 |
// request the heap dump and check if result is OK |
| 81 |
AcquireDumpOperation dumpOperation = new AcquireDumpOperation(selectedProcess, preferredLocation, getContainer()); |
| 82 |
if (!dumpOperation.run().isOK()) return false; |
| 83 |
|
| 84 |
File destFile = dumpOperation.getResult(); |
| 85 |
|
| 86 |
// open the heapdump |
| 87 |
Path path = new Path(destFile.getAbsolutePath()); |
| 88 |
IEditorDescriptor descriptor = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(path.toOSString()); |
| 89 |
|
| 90 |
try |
| 91 |
{ |
| 92 |
IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), new PathEditorInput(path), descriptor.getId(), |
| 93 |
true); |
| 94 |
if (PlatformUI.getWorkbench().getIntroManager().getIntro() != null) |
| 95 |
{ |
| 96 |
// if this action was called with open welcome page |
| 97 |
// - set it to standby mode. |
| 98 |
PlatformUI.getWorkbench().getIntroManager().setIntroStandby(PlatformUI.getWorkbench().getIntroManager().getIntro(), true); |
| 99 |
} |
| 100 |
} |
| 101 |
catch (Exception e) |
| 102 |
{ |
| 103 |
ErrorHelper.logThrowableAndShowMessage(e, Messages.AcquireSnapshotAction_UnableToOpenEditor + path); |
| 104 |
} |
| 105 |
|
| 106 |
if (new File(acquireDialog.getSelectedPath()).exists()) acquireDialog.saveSettings(); |
| 107 |
|
| 108 |
} |
| 109 |
catch (Exception e) |
| 110 |
{ |
| 111 |
ErrorHelper.logThrowableAndShowMessage(e); |
| 112 |
return false; |
| 113 |
} |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
private boolean validatePath(File destFile) |
| 118 |
{ |
| 119 |
if (destFile.exists()) |
| 120 |
{ |
| 121 |
if (MessageDialog.openConfirm(shell, Messages.AcquireSnapshotAction_Confirmation, Messages.AcquireSnapshotAction_FileAlreadyExists)) |
| 122 |
{ |
| 123 |
destFile.delete(); |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
else if (!destFile.getParentFile().exists()) |
| 131 |
{ |
| 132 |
if (MessageDialog.openConfirm(shell, Messages.AcquireSnapshotAction_Confirmation, Messages.AcquireSnapshotAction_DirectoryDoesntExist)) |
| 133 |
{ |
| 134 |
if (!destFile.getParentFile().mkdirs()) |
| 135 |
{ |
| 136 |
showError(Messages.AcquireSnapshotAction_UnableToCreateDirectory); |
| 137 |
destFile = null; |
| 138 |
} |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
return false; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
}; |
| 150 |
|
| 151 |
wizard.addPage(acquireDialog); |
| 152 |
wizard.setWindowTitle(Messages.AcquireSnapshotAction_AcquireDialogName); |
| 153 |
wizard.setNeedsProgressMonitor(true); |
| 154 |
|
| 155 |
new WizardDialog(shell, wizard).open(); |
| 156 |
} |
| 157 |
|
| 158 |
private List<IHeapDumpProvider> getProviders() |
| 159 |
{ |
| 160 |
IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.mat.api.heapDumpProvider"); //$NON-NLS-1$ |
| 161 |
if (config.length == 0) return null; |
| 162 |
|
| 163 |
List<IHeapDumpProvider> providers = new ArrayList<IHeapDumpProvider>(); |
| 164 |
for (IConfigurationElement configurationElement : config) |
| 165 |
{ |
| 166 |
String bundleName = configurationElement.getContributor().getName(); |
| 167 |
Logger.getLogger(getClass().getName()).info("Loaded heapDumpProvider from " + bundleName); //$NON-NLS-1$ |
| 168 |
|
| 169 |
try |
| 170 |
{ |
| 171 |
Object provider = configurationElement.createExecutableExtension("impl"); //$NON-NLS-1$ |
| 172 |
providers.add((IHeapDumpProvider) provider); |
| 173 |
} |
| 174 |
catch (CoreException e) |
| 175 |
{ |
| 176 |
Logger.getLogger(getClass().getName()).log(Level.SEVERE, Messages.AcquireSnapshotAction_FailedToCreateProvider, e); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return providers; |
| 181 |
} |
| 182 |
|
| 183 |
private void showError(String msg) |
| 184 |
{ |
| 185 |
ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", null, ErrorHelper.createErrorStatus(msg)); //$NON-NLS-1$ |
| 186 |
} |
| 187 |
|
| 188 |
public void dispose() |
| 189 |
{} |
| 190 |
|
| 191 |
public void init(IWorkbenchWindow window) |
| 192 |
{} |
| 193 |
|
| 194 |
public void run(IAction action) |
| 195 |
{ |
| 196 |
run(); |
| 197 |
} |
| 198 |
|
| 199 |
public void selectionChanged(IAction action, ISelection selection) |
| 200 |
{} |
| 201 |
|
| 202 |
public static class Handler extends AbstractHandler |
| 203 |
{ |
| 204 |
|
| 205 |
public Handler() |
| 206 |
{} |
| 207 |
|
| 208 |
public Object execute(ExecutionEvent executionEvent) |
| 209 |
{ |
| 210 |
new AcquireSnapshotAction().run(); |
| 211 |
return null; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
static class AcquireDumpOperation implements IRunnableWithProgress |
| 216 |
{ |
| 217 |
private IStatus status; |
| 218 |
private IRunnableContext context; |
| 219 |
private VmInfo vmInfo; |
| 220 |
private File preferredLocation; |
| 221 |
private File result; |
| 222 |
|
| 223 |
public AcquireDumpOperation(VmInfo vmInfo, File preferredLocation, IRunnableContext context) |
| 224 |
{ |
| 225 |
this.vmInfo = vmInfo; |
| 226 |
this.preferredLocation = preferredLocation; |
| 227 |
this.context = context; |
| 228 |
} |
| 229 |
|
| 230 |
private IStatus doOperation(IProgressMonitor monitor) |
| 231 |
{ |
| 232 |
IProgressListener listener = new ProgressMonitorWrapper(monitor); |
| 233 |
try |
| 234 |
{ |
| 235 |
result = vmInfo.getHeapDumpProvider().acquireDump(vmInfo, preferredLocation, listener); |
| 236 |
|
| 237 |
if (listener.isCanceled()) return Status.CANCEL_STATUS; |
| 238 |
} |
| 239 |
catch (InterruptedException ignore) |
| 240 |
{ |
| 241 |
// $JL-EXC$ |
| 242 |
} |
| 243 |
catch (Exception e) |
| 244 |
{ |
| 245 |
return ErrorHelper.createErrorStatus(e); |
| 246 |
} |
| 247 |
|
| 248 |
return Status.OK_STATUS; |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
private File getResult() |
| 253 |
{ |
| 254 |
return result; |
| 255 |
} |
| 256 |
|
| 257 |
public final void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException |
| 258 |
{ |
| 259 |
status = doOperation(monitor); |
| 260 |
} |
| 261 |
|
| 262 |
public final IStatus run() |
| 263 |
{ |
| 264 |
try |
| 265 |
{ |
| 266 |
context.run(true, true, this); |
| 267 |
} |
| 268 |
catch (Exception e) |
| 269 |
{ |
| 270 |
status = ErrorHelper.createErrorStatus(Messages.AcquireSnapshotAction_UnexpectedException, e); |
| 271 |
} |
| 272 |
|
| 273 |
// report error if any occurred |
| 274 |
if (!status.isOK() && status != Status.CANCEL_STATUS) |
| 275 |
ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", null, status); //$NON-NLS-1$ |
| 276 |
|
| 277 |
return status; |
| 278 |
} |
| 279 |
} |
| 280 |
} |