1 | package de.uka.ipd.sdq.codegen.simucontroller.workflow.jobs; |
2 | |
3 | import java.io.File; |
4 | import java.io.FileFilter; |
5 | import java.io.FileInputStream; |
6 | import java.io.FilenameFilter; |
7 | import java.io.IOException; |
8 | import java.net.URI; |
9 | |
10 | import org.eclipse.core.runtime.IProgressMonitor; |
11 | |
12 | import de.uka.ipd.sdq.workflow.IJobWithResult; |
13 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
14 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
15 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
16 | import de.uka.ipd.sdq.workflow.pcm.configurations.AbstractCodeGenerationWorkflowRunConfiguration; |
17 | import de.uka.ipd.sdq.workflow.pcm.jobs.CreatePluginProjectJob; |
18 | import edu.rice.cs.util.jar.JarBuilder; |
19 | |
20 | public class BuildPluginJarJob implements IJobWithResult<byte[]> { |
21 | |
22 | private byte[] result = null; |
23 | private AbstractCodeGenerationWorkflowRunConfiguration configuration; |
24 | |
25 | public BuildPluginJarJob(AbstractCodeGenerationWorkflowRunConfiguration configuration){ |
26 | super(); |
27 | |
28 | this.configuration = configuration; |
29 | } |
30 | |
31 | public byte[] getResult() { |
32 | return result; |
33 | } |
34 | |
35 | public void execute(IProgressMonitor monitor) throws JobFailedException, UserCanceledException { |
36 | URI location = null; |
37 | try { |
38 | location = CreatePluginProjectJob.getProject(this.configuration.getStoragePluginID()).getLocationURI(); |
39 | String jarLocation = new File(location).getAbsolutePath() + File.separator + "simucominstance.jar"; |
40 | JarBuilder builder = new JarBuilder(new File(jarLocation)); |
41 | addCompiledClasses(location, builder); |
42 | addMetadataFiles(location, builder); |
43 | addModelFiles(location, builder); |
44 | builder.close(); |
45 | this.result = loadBundle(new File(location).getAbsolutePath() + File.separator + "simucominstance.jar"); |
46 | } catch (IOException e) { |
47 | throw new JobFailedException("Compile Plugin failed. Error creating JAR archive.", e); |
48 | } |
49 | } |
50 | |
51 | /** |
52 | * @param location |
53 | * @param builder |
54 | */ |
55 | private void addMetadataFiles(URI location, JarBuilder builder) { |
56 | builder.addDirectoryRecursive(new File(location),"",new FileFilter(){ |
57 | public boolean accept(File pathname) { |
58 | return pathname.getName().toUpperCase().contains("META-INF") || pathname.getName().toUpperCase().contains("MANIFEST") || pathname.getName().contains("plugin.xml"); |
59 | } |
60 | }); |
61 | } |
62 | |
63 | /** |
64 | * @param location |
65 | * @param builder |
66 | */ |
67 | private void addModelFiles(URI location, JarBuilder builder) { |
68 | builder.addDirectoryRecursive(new File(location).listFiles(new FilenameFilter() { |
69 | public boolean accept(File dir, String name) { |
70 | return name.equals("model"); |
71 | } |
72 | })[0], "model"); |
73 | } |
74 | |
75 | /** |
76 | * @param location |
77 | * @param builder |
78 | */ |
79 | private void addCompiledClasses(URI location, JarBuilder builder) { |
80 | builder.addDirectoryRecursive(new File(location).listFiles(new FilenameFilter(){ |
81 | public boolean accept(File dir, String name) { |
82 | return name.contains("bin"); |
83 | } |
84 | })[0], ""); |
85 | } |
86 | |
87 | public String getName() { |
88 | return "Building simulation plugin JAR archive"; |
89 | } |
90 | |
91 | public void rollback(IProgressMonitor monitor) throws RollbackFailedException { |
92 | } |
93 | |
94 | private byte[] loadBundle(String location) throws IOException { |
95 | byte[] result = null; |
96 | File bundleFile = new File(location); |
97 | result = new byte[(int) bundleFile.length()]; |
98 | FileInputStream fis = new FileInputStream(bundleFile); |
99 | fis.read(result); |
100 | fis.close(); |
101 | return result; |
102 | } |
103 | |
104 | } |