package com.onpositive.gae.profiler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import com.onpositive.gae.tools.UIUtils;
public class StopProfilingAction extends Action implements
IWorkbenchWindowActionDelegate {
private static final String BUILDER = "com.onpositive.profiler.gae.builder";
public StopProfilingAction() {
setText("Disable profiling instrumentation");
}
public void dispose() {
}
public void init(IWorkbenchWindow window) {
}
public void run() {
IJavaProject javaGaeProject = UIUtils.getJavaGaeProject();
stopProfiling(javaGaeProject);
}
public void run(IAction action) {
run();
}
public void stopProfiling(final IJavaProject javaGaeProject) {
try {
if (javaGaeProject == null) {
return;
}
IProjectDescription description = javaGaeProject.getProject()
.getDescription();
ICommand[] buildSpec = description.getBuildSpec();
ArrayList<ICommand> arrayList = new ArrayList<ICommand>(Arrays
.asList(buildSpec));
for (Iterator<ICommand> iterator = arrayList.iterator(); iterator
.hasNext();) {
ICommand iCommand = (ICommand) iterator.next();
if (iCommand.getBuilderName().equals(BUILDER)) {
iterator.remove();
}
}
description.setBuildSpec(arrayList.toArray(new ICommand[arrayList
.size()]));
javaGaeProject.getProject().setDescription(description,
new NullProgressMonitor());
boolean deleteCallDictinary = DictionaryManager
.deleteCallDictinary(javaGaeProject.getProject());
if (!deleteCallDictinary) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
MessageDialog.openError(Display.getCurrent()
.getActiveShell(),
"Unable to cleanup profiling dictionary",
"Unable to cleanup profiling dictionary");
}
});
}
Job job = new Job("Clean") {
protected IStatus run(IProgressMonitor monitor) {
try {
javaGaeProject.getProject().build(
IncrementalProjectBuilder.CLEAN_BUILD, monitor);
} catch (CoreException e) {
return e.getStatus();
}
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Note", "You should deploy your application to Google App Engine to stop profiling on it\n or restart local server if you would like to stop profiling of local application.");
}
});
return Status.OK_STATUS;
}
};
job.schedule();
ProfilerDecorator.changed(javaGaeProject);
} catch (Exception e) {
Activator.getDefault().log(e);
}
}
public void selectionChanged(IAction action, ISelection selection) {
}
}