1 | package de.uka.ipd.sdq.codegen.simucontroller.workflow.jobs; |
2 | |
3 | import java.io.ByteArrayInputStream; |
4 | import java.io.ByteArrayOutputStream; |
5 | import java.io.PrintStream; |
6 | |
7 | import org.apache.commons.lang.StringUtils; |
8 | import org.eclipse.core.resources.IFile; |
9 | import org.eclipse.core.resources.IProject; |
10 | import org.eclipse.core.runtime.CoreException; |
11 | import org.eclipse.core.runtime.IProgressMonitor; |
12 | |
13 | import de.uka.ipd.sdq.workflow.IJob; |
14 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
15 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
16 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
17 | import de.uka.ipd.sdq.workflow.pcm.configurations.AbstractCodeGenerationWorkflowRunConfiguration; |
18 | import de.uka.ipd.sdq.workflow.pcm.jobs.CreatePluginProjectJob; |
19 | |
20 | public abstract class AbstractCreateMetaDataFilesJob { |
21 | |
22 | protected AbstractCodeGenerationWorkflowRunConfiguration configuration; |
23 | public static final String F_MANIFEST = "MANIFEST.MF"; |
24 | public static final String F_MANIFEST_FP = "META-INF/" + F_MANIFEST; |
25 | public static final String F_PLUGIN = "plugin.xml"; |
26 | public static final String F_FRAGMENT = "fragment.xml"; |
27 | public static final String F_PROPERTIES = ".properties"; |
28 | public static final String F_BUILD = "build" + F_PROPERTIES; |
29 | |
30 | public AbstractCreateMetaDataFilesJob() { |
31 | super(); |
32 | } |
33 | |
34 | public void execute(IProgressMonitor monitor) throws JobFailedException, |
35 | UserCanceledException { |
36 | IProject project = CreatePluginProjectJob.getProject(configuration.getStoragePluginID()); |
37 | |
38 | try { |
39 | createPluginXml(project); |
40 | createManifestMf(project); |
41 | createBuildProperties(project); |
42 | } catch (CoreException e) { |
43 | throw new JobFailedException("Failed to create plugin metadata files",e); |
44 | } |
45 | } |
46 | |
47 | public String getName() { |
48 | return "Create SimuCom Metadata Files"; |
49 | } |
50 | |
51 | public void rollback(IProgressMonitor monitor) throws RollbackFailedException { |
52 | // Nothing to do |
53 | } |
54 | |
55 | private void createPluginXml(IProject project) throws CoreException { |
56 | |
57 | ByteArrayOutputStream baos; |
58 | PrintStream out; |
59 | |
60 | baos = new ByteArrayOutputStream(); |
61 | out = new PrintStream(baos); |
62 | |
63 | |
64 | writePluginXmlContent(out); |
65 | |
66 | out.close(); |
67 | |
68 | IFile pluginXml = project.getFile(F_PLUGIN); |
69 | if (!pluginXml.exists()) |
70 | pluginXml.create(new ByteArrayInputStream(baos.toByteArray()), |
71 | true, null); |
72 | } |
73 | |
74 | protected abstract void writePluginXmlContent(PrintStream out); |
75 | |
76 | private void createBuildProperties(IProject project) throws CoreException { |
77 | |
78 | ByteArrayOutputStream baos; |
79 | PrintStream out; |
80 | |
81 | baos = new ByteArrayOutputStream(); |
82 | out = new PrintStream(baos); |
83 | |
84 | writeBuildPropertiesContent(out); |
85 | |
86 | out.close(); |
87 | |
88 | IFile buildProperties = project.getFile(F_BUILD); |
89 | if (!buildProperties.exists()) |
90 | buildProperties.create( |
91 | new ByteArrayInputStream(baos.toByteArray()), true, null); |
92 | } |
93 | |
94 | protected abstract void writeBuildPropertiesContent(PrintStream out); |
95 | |
96 | |
97 | private void createManifestMf(IProject project) throws CoreException { |
98 | |
99 | ByteArrayOutputStream baos; |
100 | PrintStream out; |
101 | |
102 | baos = new ByteArrayOutputStream(); |
103 | out = new PrintStream(baos); |
104 | |
105 | out.println("Manifest-Version: 1.0"); //$NON-NLS-1$ |
106 | out.println("Bundle-ManifestVersion: 2"); //$NON-NLS-1$ |
107 | out.println("Bundle-Name: SimuCom Instance Plug-in"); //$NON-NLS-1$ |
108 | out.println("Bundle-SymbolicName: " + project.getName() + ";singleton:=true"); //$NON-NLS-1$ |
109 | out.println("Bundle-Version: 1.0.0"); //$NON-NLS-1$ |
110 | out.println("Bundle-Activator: " + getBundleActivator()); |
111 | |
112 | out.print("Require-Bundle: "); |
113 | |
114 | out.println(StringUtils.join(getRequiredBundles(), ",\n ")); |
115 | |
116 | |
117 | |
118 | out.println("Eclipse-LazyStart: true"); //$NON-NLS-1$ |
119 | out.println("Bundle-ClassPath: bin/,"); |
120 | out.println(" ."); |
121 | //out.println("Export-Package: main"); |
122 | |
123 | out.close(); |
124 | |
125 | IFile manifestMf = project.getFile(F_MANIFEST_FP); |
126 | if (!manifestMf.exists()) |
127 | manifestMf.create(new ByteArrayInputStream(baos.toByteArray()), |
128 | true, null); |
129 | } |
130 | |
131 | protected abstract String[] getRequiredBundles(); |
132 | |
133 | protected abstract String getBundleActivator(); |
134 | |
135 | } |