| 1 | package de.uka.ipd.sdq.workflow.pcm.jobs; |
| 2 | |
| 3 | import java.io.IOException; |
| 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.runtime.CoreException; |
| 9 | import org.eclipse.core.runtime.IProgressMonitor; |
| 10 | import org.eclipse.emf.common.util.URI; |
| 11 | import org.eclipse.emf.ecore.resource.Resource; |
| 12 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 13 | |
| 14 | import de.uka.ipd.sdq.workflow.IBlackboardInteractingJob; |
| 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.mdsd.blackboard.MDSDBlackboard; |
| 20 | import de.uka.ipd.sdq.workflow.pcm.blackboard.PCMResourceSetPartition; |
| 21 | import de.uka.ipd.sdq.workflow.pcm.configurations.AbstractPCMWorkflowRunConfiguration; |
| 22 | |
| 23 | /** |
| 24 | * Job to create a working copy of the models to simulate. |
| 25 | * This ensures that any downstream job changing the models |
| 26 | * does not modify the original models. |
| 27 | * |
| 28 | * Prerequisite of this job: |
| 29 | * This job copies the models to the configured project created in |
| 30 | * the workflow. It has to exist to be able to store the model copy |
| 31 | * into it. |
| 32 | * |
| 33 | * @author Benjamin Klatt |
| 34 | * |
| 35 | */ |
| 36 | public class CreateWorkingCopyOfModelsJob implements IJob, |
| 37 | IBlackboardInteractingJob<MDSDBlackboard> { |
| 38 | |
| 39 | /** The logger for this class */ |
| 40 | private static final Logger logger = Logger.getLogger(CreateWorkingCopyOfModelsJob.class); |
| 41 | |
| 42 | /** The blackboard to interact with */ |
| 43 | private MDSDBlackboard blackboard = null; |
| 44 | |
| 45 | /** The work flow configuration to get the required information from */ |
| 46 | private AbstractPCMWorkflowRunConfiguration configuration; |
| 47 | |
| 48 | /** |
| 49 | * Constructor requiring the necessary configuration object. |
| 50 | * |
| 51 | * @param configuration The configuration for this job. |
| 52 | */ |
| 53 | public CreateWorkingCopyOfModelsJob(AbstractPCMWorkflowRunConfiguration configuration) { |
| 54 | this.configuration = configuration; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Execute this job and create the model copy. |
| 59 | */ |
| 60 | public void execute(IProgressMonitor monitor) throws JobFailedException, |
| 61 | UserCanceledException { |
| 62 | |
| 63 | assert (this.configuration != null); |
| 64 | IProject project = CreatePluginProjectJob.getProject(this.configuration |
| 65 | .getStoragePluginID()); |
| 66 | assert (project != null); |
| 67 | |
| 68 | // prepare the target path |
| 69 | IFolder modelFolder = project.getFolder("model"); |
| 70 | if (project.isOpen() && !modelFolder.exists()) { |
| 71 | logger.debug("Creating folder " + modelFolder.getName()); |
| 72 | try { |
| 73 | modelFolder.create(false, true, null); |
| 74 | } catch (CoreException e) { |
| 75 | logger.error("unable to create model folder"); |
| 76 | throw new JobFailedException(e); |
| 77 | } |
| 78 | } |
| 79 | String modelBasePath = "file:/"+modelFolder.getLocation().toOSString(); |
| 80 | |
| 81 | // access the resources |
| 82 | PCMResourceSetPartition partition = (PCMResourceSetPartition) this.blackboard.getPartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID); |
| 83 | ResourceSet resourceSet = partition.getResourceSet(); |
| 84 | for (Resource ressource : resourceSet.getResources()) { |
| 85 | |
| 86 | // we only need to copy the file models |
| 87 | if (ressource.getURI().scheme() != "pathmap") { |
| 88 | URI uri = ressource.getURI(); |
| 89 | String relativePath = uri.lastSegment(); |
| 90 | URI newURI = URI.createURI(modelBasePath +"/"+ relativePath); |
| 91 | ressource.setURI(newURI); |
| 92 | |
| 93 | try { |
| 94 | ressource.save(null); |
| 95 | partition.setContents(ressource.getURI(), ressource.getContents()); |
| 96 | } catch (IOException e) { |
| 97 | logger.error("Unable to store resource "+ressource.getURI(),e); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | try { |
| 102 | partition.storeAllResources(); |
| 103 | } catch (IOException e) { |
| 104 | logger.error("unable to store all resources",e); |
| 105 | throw new JobFailedException("Unable to store all Resources",e); |
| 106 | } |
| 107 | // partition.resolveAllProxies(); |
| 108 | // this.blackboard.removePartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID); |
| 109 | // this.blackboard.addPartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID, partition); |
| 110 | } |
| 111 | |
| 112 | public String getName() { |
| 113 | return "Create working copy of models"; |
| 114 | } |
| 115 | |
| 116 | public void rollback(IProgressMonitor monitor) |
| 117 | throws RollbackFailedException { |
| 118 | // nothing to roll back |
| 119 | } |
| 120 | |
| 121 | public void setBlackboard(MDSDBlackboard blackboard) { |
| 122 | this.blackboard = blackboard; |
| 123 | } |
| 124 | |
| 125 | } |