| 1 | package de.uka.ipd.sdq.codegen.simucontroller.workflow.jobs; |
| 2 | |
| 3 | import org.eclipse.core.internal.events.BuildCommand; |
| 4 | import org.eclipse.core.resources.ICommand; |
| 5 | import org.eclipse.core.resources.IMarker; |
| 6 | import org.eclipse.core.resources.IProject; |
| 7 | import org.eclipse.core.resources.IProjectDescription; |
| 8 | import org.eclipse.core.resources.IResource; |
| 9 | import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 10 | import org.eclipse.core.resources.ResourcesPlugin; |
| 11 | import org.eclipse.core.runtime.CoreException; |
| 12 | import org.eclipse.core.runtime.IPath; |
| 13 | import org.eclipse.core.runtime.IProgressMonitor; |
| 14 | import org.eclipse.jdt.core.IClasspathEntry; |
| 15 | import org.eclipse.jdt.core.IJavaProject; |
| 16 | import org.eclipse.jdt.core.JavaCore; |
| 17 | import org.eclipse.jdt.core.JavaModelException; |
| 18 | import org.eclipse.jdt.launching.JavaRuntime; |
| 19 | import org.eclipse.pde.core.plugin.PluginRegistry; |
| 20 | import org.eclipse.pde.internal.core.ClasspathComputer; |
| 21 | import org.eclipse.pde.internal.core.natures.PDE; |
| 22 | |
| 23 | import de.uka.ipd.sdq.workflow.IJob; |
| 24 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
| 25 | import de.uka.ipd.sdq.workflow.pcm.configurations.AbstractCodeGenerationWorkflowRunConfiguration; |
| 26 | import de.uka.ipd.sdq.workflow.pcm.jobs.CreatePluginProjectJob; |
| 27 | |
| 28 | @SuppressWarnings("restriction") |
| 29 | public class CompilePluginCodeJob implements IJob { |
| 30 | |
| 31 | private AbstractCodeGenerationWorkflowRunConfiguration configuration; |
| 32 | |
| 33 | public CompilePluginCodeJob( |
| 34 | AbstractCodeGenerationWorkflowRunConfiguration configuration) { |
| 35 | super(); |
| 36 | |
| 37 | this.configuration = configuration; |
| 38 | } |
| 39 | |
| 40 | public void execute(IProgressMonitor monitor) throws JobFailedException { |
| 41 | assert (this.configuration != null); |
| 42 | |
| 43 | IProject project = CreatePluginProjectJob.getProject(this.configuration |
| 44 | .getStoragePluginID()); |
| 45 | assert (project != null); |
| 46 | |
| 47 | // create description |
| 48 | createDescription(project, monitor); |
| 49 | |
| 50 | // create JavaProject |
| 51 | setProjectToJavaProject(project); |
| 52 | |
| 53 | // set Plug-In class path |
| 54 | setClasspath(project); |
| 55 | |
| 56 | refreshPluginInWorkspace(monitor, project); |
| 57 | |
| 58 | buildProject(monitor, project); |
| 59 | |
| 60 | checkForErrors(project); |
| 61 | } |
| 62 | |
| 63 | /* (non-Javadoc) |
| 64 | * @See org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer.setClasspath(IProject) |
| 65 | */ |
| 66 | private void setClasspath(IProject project) throws JobFailedException { |
| 67 | try { |
| 68 | ClasspathComputer.setClasspath(project, PluginRegistry |
| 69 | .findModel(project)); |
| 70 | } catch (CoreException e) { |
| 71 | throw new JobFailedException("Failed to set JDT classpath",e); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Create the Java-Project from IProject and set "src", "bin" folder to |
| 77 | * classpath |
| 78 | */ |
| 79 | private void setProjectToJavaProject(IProject project) |
| 80 | throws JobFailedException { |
| 81 | // create class path entry |
| 82 | IJavaProject javaProject = JavaCore.create(project); |
| 83 | IPath srcPath = javaProject.getPath().append("src"); |
| 84 | IPath binPath = javaProject.getPath().append("bin"); |
| 85 | IClasspathEntry[] buildPath = { JavaCore.newSourceEntry(srcPath), |
| 86 | JavaRuntime.getDefaultJREContainerEntry() }; |
| 87 | try { |
| 88 | javaProject.setRawClasspath(buildPath, binPath, null); |
| 89 | } catch (JavaModelException e) { |
| 90 | throw new JobFailedException("Failed setting up JDT project",e); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Create a project description and set the JavaCore.NATURE_ID and |
| 96 | * PDE.PLUGIN_NATURE |
| 97 | */ |
| 98 | private void createDescription(IProject project, IProgressMonitor monitor) |
| 99 | throws JobFailedException { |
| 100 | IProjectDescription description = ResourcesPlugin.getWorkspace() |
| 101 | .newProjectDescription(project.getName()); |
| 102 | description.setNatureIds(new String[] { JavaCore.NATURE_ID, |
| 103 | PDE.PLUGIN_NATURE }); |
| 104 | description.setLocation(null); |
| 105 | // set java builders |
| 106 | ICommand command = description.newCommand(); |
| 107 | command.setBuilderName(JavaCore.BUILDER_ID); |
| 108 | description.setBuildSpec(new BuildCommand[] { (BuildCommand) command }); |
| 109 | try { |
| 110 | project.setDescription(description, monitor); |
| 111 | } catch (CoreException e) { |
| 112 | throw new JobFailedException("Failed setting Java and PDE nature and builders",e); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @param monitor |
| 118 | * @param project |
| 119 | * @throws JobFailedException |
| 120 | */ |
| 121 | private void buildProject(IProgressMonitor monitor, IProject project) |
| 122 | throws JobFailedException { |
| 123 | try { |
| 124 | project.build(IncrementalProjectBuilder.FULL_BUILD, monitor); |
| 125 | } catch (Exception e) { |
| 126 | throw new JobFailedException("Building plugin project failed", e); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param monitor |
| 132 | * @param project |
| 133 | * @throws JobFailedException |
| 134 | */ |
| 135 | private void refreshPluginInWorkspace(IProgressMonitor monitor, |
| 136 | IProject project) throws JobFailedException { |
| 137 | try { |
| 138 | project.refreshLocal(IResource.DEPTH_INFINITE, monitor); |
| 139 | } catch (Exception e) { |
| 140 | throw new JobFailedException("Refreshing plugin project failed", e); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @param project |
| 146 | * @throws JobFailedException |
| 147 | */ |
| 148 | private void checkForErrors(IProject project) throws JobFailedException { |
| 149 | try { |
| 150 | if (project.findMarkers(IMarker.PROBLEM, true, |
| 151 | IResource.DEPTH_INFINITE).length > 0) { |
| 152 | boolean failed = false; |
| 153 | IMarker[] markers = project.findMarkers(IMarker.PROBLEM, true, |
| 154 | IResource.DEPTH_INFINITE); |
| 155 | String errorList = ""; |
| 156 | for (IMarker marker : markers) { |
| 157 | if (((Integer) marker.getAttribute(IMarker.SEVERITY)) == IMarker.SEVERITY_ERROR) { |
| 158 | errorList += marker.getAttribute(IMarker.MESSAGE) |
| 159 | + "\n"; |
| 160 | failed = true; |
| 161 | } |
| 162 | } |
| 163 | if (failed) |
| 164 | throw new JobFailedException( |
| 165 | "Unable to build the simulation plugin. Failure Messages: " |
| 166 | + errorList); |
| 167 | } |
| 168 | } catch (CoreException e) { |
| 169 | throw new JobFailedException( |
| 170 | "Compile Plugin failed. Error finding project markers.", e); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | public String getName() { |
| 175 | return "Compile Plugin Code"; |
| 176 | } |
| 177 | |
| 178 | public void rollback(IProgressMonitor monitor) { |
| 179 | // do nothing |
| 180 | } |
| 181 | } |