| 1 | package de.uka.ipd.sdq.workflow.pcm.jobs; |
| 2 | |
| 3 | import java.io.File; |
| 4 | |
| 5 | import org.apache.log4j.Logger; |
| 6 | import org.eclipse.core.resources.IFolder; |
| 7 | import org.eclipse.core.resources.IProject; |
| 8 | import org.eclipse.core.resources.IResource; |
| 9 | import org.eclipse.core.resources.ResourcesPlugin; |
| 10 | import org.eclipse.core.runtime.CoreException; |
| 11 | import org.eclipse.core.runtime.IProgressMonitor; |
| 12 | import org.eclipse.jface.dialogs.MessageDialog; |
| 13 | import org.eclipse.ui.PlatformUI; |
| 14 | |
| 15 | import de.uka.ipd.sdq.workflow.IJob; |
| 16 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
| 17 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
| 18 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
| 19 | import de.uka.ipd.sdq.workflow.pcm.configurations.AbstractPCMWorkflowRunConfiguration; |
| 20 | |
| 21 | /** |
| 22 | * Creates an Eclipse plug-in project with default folders for source code and |
| 23 | * models. The project can be used to store analysis date, e.g. (transformed) |
| 24 | * PCM models, simulation code and so on. |
| 25 | * |
| 26 | * @author groenda |
| 27 | */ |
| 28 | public class CreatePluginProjectJob implements IJob { |
| 29 | |
| 30 | /** Logger for this class. */ |
| 31 | private Logger logger = Logger.getLogger(CreatePluginProjectJob.class); |
| 32 | |
| 33 | private boolean deleteProject; |
| 34 | private String myProjectId; |
| 35 | private boolean overwriteWithoutAsking; |
| 36 | |
| 37 | public CreatePluginProjectJob( |
| 38 | AbstractPCMWorkflowRunConfiguration configuration) { |
| 39 | super(); |
| 40 | |
| 41 | this.myProjectId = configuration.getStoragePluginID(); |
| 42 | this.deleteProject = configuration.isDeleteTemporaryDataAfterAnalysis(); |
| 43 | this.overwriteWithoutAsking = configuration.isOverwriteWithoutAsking(); |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * (non-Javadoc) |
| 48 | * |
| 49 | * @see |
| 50 | * de.uka.ipd.sdq.codegen.simucontroller.runconfig.ISimuComJob#execute() |
| 51 | */ |
| 52 | public void execute(IProgressMonitor monitor) throws UserCanceledException, |
| 53 | JobFailedException { |
| 54 | ensurePluginProjectNotExisting(monitor); |
| 55 | createContainerPlugin(monitor); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @throws UserCanceledException |
| 60 | * @throws JobFailedException |
| 61 | */ |
| 62 | private void ensurePluginProjectNotExisting(IProgressMonitor monitor) |
| 63 | throws UserCanceledException, JobFailedException { |
| 64 | if (pluginFolderExists() || getProject(this.myProjectId).exists()) { |
| 65 | if (!overwriteWithoutAsking && !userAcceptsDelete()) { |
| 66 | // abort execution |
| 67 | throw new UserCanceledException("Aborted by user"); |
| 68 | } else { |
| 69 | try { |
| 70 | deleteProject(monitor, getProject(this.myProjectId)); |
| 71 | } catch (CoreException e) { |
| 72 | throw new JobFailedException("Removing old project failed", |
| 73 | e); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return true, if the folder used for the simulation plugin exists in the |
| 81 | * filesystem, false otherwise |
| 82 | */ |
| 83 | private boolean pluginFolderExists() { |
| 84 | File pluginFolder = ResourcesPlugin.getWorkspace().getRoot() |
| 85 | .getRawLocation().append(myProjectId).toFile(); |
| 86 | |
| 87 | return pluginFolder.exists(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * ask the user if the plugin folder should be deleted using a message |
| 92 | * dialog |
| 93 | * |
| 94 | * @return true, if the user selected "delete", false otherwise |
| 95 | */ |
| 96 | private boolean userAcceptsDelete() { |
| 97 | UserMessageRunner runner = new UserMessageRunner(); |
| 98 | PlatformUI.getWorkbench().getDisplay().syncExec(runner); |
| 99 | |
| 100 | return runner.shouldDelete(); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * (non-Javadoc) |
| 105 | * |
| 106 | * @see |
| 107 | * de.uka.ipd.sdq.codegen.simucontroller.runconfig.ISimuComJob#getName() |
| 108 | */ |
| 109 | public String getName() { |
| 110 | return "Create Plugin Project"; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * (non-Javadoc) |
| 115 | * |
| 116 | * @see |
| 117 | * de.uka.ipd.sdq.codegen.simucontroller.runconfig.ISimuComJob#rollback() |
| 118 | */ |
| 119 | public void rollback(IProgressMonitor monitor) |
| 120 | throws RollbackFailedException { |
| 121 | if (deleteProject) { |
| 122 | IProject myProject = getProject(this.myProjectId); |
| 123 | if (myProject == null) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | try { |
| 128 | deleteProject(monitor, myProject); |
| 129 | } catch (CoreException e) { |
| 130 | throw new RollbackFailedException("Delete project failed", e); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @param monitor |
| 137 | * @param myProject |
| 138 | * @throws RollbackFailedException |
| 139 | */ |
| 140 | private void deleteProject(IProgressMonitor monitor, IProject myProject) |
| 141 | throws CoreException { |
| 142 | logger.info("Deleting project " + myProject.getName()); |
| 143 | |
| 144 | myProject.close(monitor); |
| 145 | myProject.delete(IResource.ALWAYS_DELETE_PROJECT_CONTENT, monitor); |
| 146 | ResourcesPlugin.getWorkspace().getRoot().refreshLocal(1, monitor); |
| 147 | |
| 148 | if (pluginFolderExists()) { |
| 149 | // Eclipse failed in fully cleaning the directory |
| 150 | clearPluginFolder(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * deletes a folder and all of its contents recursively |
| 156 | * |
| 157 | * @param folder |
| 158 | * the folder to be deleted |
| 159 | * @return true on success, false otherwise |
| 160 | */ |
| 161 | private boolean deleteFolder(File folder) { |
| 162 | if (folder.isDirectory()) { |
| 163 | for (File child : folder.listFiles()) { |
| 164 | if (!deleteFolder(child)) { |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // empty folders can be deleted directly |
| 171 | return folder.delete(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * clears the simulation plugin folder |
| 176 | */ |
| 177 | private void clearPluginFolder() { |
| 178 | File pluginFolder = ResourcesPlugin.getWorkspace().getRoot() |
| 179 | .getRawLocation().append(this.myProjectId).toFile(); |
| 180 | |
| 181 | deleteFolder(pluginFolder); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * returns a new project to be used for the simulation |
| 186 | * |
| 187 | * @return a handle to the project to be used for the simulation |
| 188 | */ |
| 189 | public static IProject getProject(String projectId) { |
| 190 | return ResourcesPlugin.getWorkspace().getRoot().getProject(projectId); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * The function implements all steps, which are necessary for the creation |
| 195 | * of a Plugin Project |
| 196 | * |
| 197 | * @param projectId |
| 198 | * The ID of the new project |
| 199 | * @param monitor |
| 200 | * The progress monitor which displays progress |
| 201 | * @return - container project (Plug-In) |
| 202 | */ |
| 203 | public void createContainerPlugin(IProgressMonitor monitor) |
| 204 | throws JobFailedException { |
| 205 | try { |
| 206 | IProject project = getProject(this.myProjectId); |
| 207 | |
| 208 | IFolder srcFolder = project.getFolder("src"); |
| 209 | IFolder manifestFolder = project.getFolder("META-INF"); |
| 210 | |
| 211 | // create resources |
| 212 | createProject(project, monitor); |
| 213 | createFolder(project, srcFolder); |
| 214 | createFolder(project, manifestFolder); |
| 215 | } catch (CoreException e) { |
| 216 | throw new JobFailedException( |
| 217 | "Generation of the Eclipse project failed", e); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | private void createFolder(IProject project, IFolder folder) |
| 222 | throws CoreException { |
| 223 | if (project.isOpen() && !folder.exists()) { |
| 224 | logger.debug("Creating folder " + folder.getName()); |
| 225 | folder.create(false, true, null); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private void createProject(IProject project, IProgressMonitor monitor) |
| 230 | throws CoreException, JobFailedException { |
| 231 | if (project.exists()) |
| 232 | throw new JobFailedException( |
| 233 | "Tried to create an existing project. Preceeding cleanup failed"); |
| 234 | |
| 235 | logger.debug("Creating Eclipse workspace project " + project.getName()); |
| 236 | project.create(monitor); |
| 237 | project.open(monitor); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Helper class that allows a message box to appear from a non user |
| 242 | * interface thread because the workbench shell is otherwise not accessible. |
| 243 | * |
| 244 | * @author Philipp Meier |
| 245 | */ |
| 246 | private class UserMessageRunner implements Runnable { |
| 247 | private boolean myshouldDelete = false; |
| 248 | |
| 249 | public UserMessageRunner() { |
| 250 | super(); |
| 251 | } |
| 252 | |
| 253 | public boolean shouldDelete() { |
| 254 | return myshouldDelete; |
| 255 | } |
| 256 | |
| 257 | public void run() { |
| 258 | String[] options = { "Delete and Continue", "Abort" }; |
| 259 | MessageDialog dlg = new MessageDialog(PlatformUI.getWorkbench() |
| 260 | .getActiveWorkbenchWindow().getShell(), |
| 261 | "Temporary analysis project folder already exists", null, |
| 262 | "The project used for the analysis already exists. Should " |
| 263 | + myProjectId |
| 264 | + " and all of its contents be deleted?", |
| 265 | MessageDialog.QUESTION, options, 1); |
| 266 | |
| 267 | // check if the user selected Delete |
| 268 | if (dlg.open() == 0) { |
| 269 | myshouldDelete = true; |
| 270 | } else { |
| 271 | myshouldDelete = false; |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | } |