1 | package simulator.utils; |
---|
2 | |
---|
3 | |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.Reader; |
---|
6 | import java.io.StringReader; |
---|
7 | import java.io.StringWriter; |
---|
8 | import java.io.Writer; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import javax.xml.parsers.ParserConfigurationException; |
---|
12 | import javax.xml.transform.Templates; |
---|
13 | import javax.xml.transform.Transformer; |
---|
14 | import javax.xml.transform.TransformerConfigurationException; |
---|
15 | import javax.xml.transform.TransformerException; |
---|
16 | import javax.xml.transform.TransformerFactory; |
---|
17 | import javax.xml.transform.stream.StreamResult; |
---|
18 | import javax.xml.transform.stream.StreamSource; |
---|
19 | import javax.xml.xpath.XPathExpressionException; |
---|
20 | |
---|
21 | import dcworms.schedframe.scheduling.utils.ApplicationProfileDescription; |
---|
22 | import dcworms.schedframe.scheduling.utils.JobDescription; |
---|
23 | import dcworms.schedframe.scheduling.utils.TaskDescription; |
---|
24 | |
---|
25 | /** |
---|
26 | * |
---|
27 | * @author Marcin Krystek |
---|
28 | * |
---|
29 | */ |
---|
30 | public class XsltTransformations { |
---|
31 | |
---|
32 | public static String INSTALLDIR = null; |
---|
33 | |
---|
34 | static { |
---|
35 | String key = "javax.xml.transform.TransformerFactory"; |
---|
36 | String value = |
---|
37 | //"org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; |
---|
38 | //"org.apache.xalan.processor.TransformerFactoryImpl"; |
---|
39 | "net.sf.saxon.TransformerFactoryImpl"; |
---|
40 | Properties props = System.getProperties(); |
---|
41 | props.put(key, value); |
---|
42 | System.setProperties(props); |
---|
43 | |
---|
44 | INSTALLDIR = System.getProperty("gssim.install.dir"); |
---|
45 | if(INSTALLDIR != null) |
---|
46 | INSTALLDIR = INSTALLDIR+"/"; |
---|
47 | else |
---|
48 | INSTALLDIR = ""; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | public XsltTransformations() throws ParserConfigurationException, XPathExpressionException{ |
---|
53 | } |
---|
54 | |
---|
55 | protected enum Transformers { |
---|
56 | EXTEND_JOB_DESC(INSTALLDIR+"simulator/xslt/ext_job_desc.xslt"), |
---|
57 | TASK2RESOURCE_REQUIREMENTS(INSTALLDIR+"simulator/xslt/task2resreq.xslt"), |
---|
58 | RESOURCE_REQUIREMENTS2RTG(INSTALLDIR+"simulator/xslt/resreq2rtg.xslt"), |
---|
59 | RTG2HOST_PARAMETERS(INSTALLDIR+"simulator/xslt/rtg2host_params.xslt"), |
---|
60 | HOST_PARAMETERS2RTG(INSTALLDIR+"simulator/xslt/host_params2rtg.xslt"), |
---|
61 | RTG2SCHEDULIING_PLAN(INSTALLDIR+"simulator/xslt/schedulingPlan.xslt"), |
---|
62 | EXTRACT_JOB_ID(INSTALLDIR+"simulator/xslt/extract_job_id.xslt"), |
---|
63 | JOB_POSTPROCESSING(INSTALLDIR+"simulator/xslt/job_postprocessing.xslt"); |
---|
64 | |
---|
65 | private String fileName; |
---|
66 | private Transformer transformer; |
---|
67 | |
---|
68 | private Transformers(String arg){ |
---|
69 | this.fileName = arg; |
---|
70 | this.transformer = null; |
---|
71 | } |
---|
72 | |
---|
73 | public Transformer geTransformer() throws TransformerConfigurationException{ |
---|
74 | if(this.transformer == null){ |
---|
75 | TransformerFactory tFactory = TransformerFactory.newInstance(); |
---|
76 | StreamSource ss = new StreamSource(this.fileName); |
---|
77 | Templates translet = tFactory.newTemplates(ss); |
---|
78 | this.transformer = translet.newTransformer(); |
---|
79 | } |
---|
80 | |
---|
81 | return this.transformer; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public String extendJobDescription(String jobDesc){ |
---|
86 | Transformer t = null; |
---|
87 | try { |
---|
88 | t = Transformers.EXTEND_JOB_DESC.geTransformer(); |
---|
89 | return transform(t, jobDesc); |
---|
90 | } catch (TransformerConfigurationException e) { |
---|
91 | e.printStackTrace(); |
---|
92 | } |
---|
93 | return null; |
---|
94 | } |
---|
95 | |
---|
96 | public String taskToResourceRequirements(String task, String jobId, String userDN, org.joda.time.DateTime submitionTime){ |
---|
97 | Transformer t = null; |
---|
98 | try { |
---|
99 | t = Transformers.TASK2RESOURCE_REQUIREMENTS.geTransformer(); |
---|
100 | t.clearParameters(); |
---|
101 | t.setParameter("JOB_ID", jobId); |
---|
102 | t.setParameter("USER_DN", userDN); |
---|
103 | |
---|
104 | org.exolab.castor.types.DateTime dt = new org.exolab.castor.types.DateTime(submitionTime.getMillis()); |
---|
105 | short milis = 0; |
---|
106 | dt.setMilliSecond(milis); |
---|
107 | t.setParameter("SUBMISSION_TIME", dt); |
---|
108 | |
---|
109 | return transform(t, task); |
---|
110 | |
---|
111 | } catch (TransformerConfigurationException e) { |
---|
112 | e.printStackTrace(); |
---|
113 | } |
---|
114 | return null; |
---|
115 | } |
---|
116 | |
---|
117 | public String resourceRequirementsToRTG(String resReq){ |
---|
118 | Transformer t = null; |
---|
119 | try { |
---|
120 | t = Transformers.RESOURCE_REQUIREMENTS2RTG.geTransformer(); |
---|
121 | return transform(t, resReq); |
---|
122 | } catch (TransformerConfigurationException e) { |
---|
123 | e.printStackTrace(); |
---|
124 | } |
---|
125 | return null; |
---|
126 | } |
---|
127 | |
---|
128 | public String RTGToHostParameters(String rtg){ |
---|
129 | Transformer t = null; |
---|
130 | try { |
---|
131 | t = Transformers.RTG2HOST_PARAMETERS.geTransformer(); |
---|
132 | return transform(t, rtg); |
---|
133 | } catch (TransformerConfigurationException e) { |
---|
134 | e.printStackTrace(); |
---|
135 | } |
---|
136 | return null; |
---|
137 | } |
---|
138 | |
---|
139 | public String hostParametersToRTG(String hostParameters){ |
---|
140 | Transformer t = null; |
---|
141 | try { |
---|
142 | t = Transformers.HOST_PARAMETERS2RTG.geTransformer(); |
---|
143 | return transform(t, hostParameters); |
---|
144 | } catch (TransformerConfigurationException e) { |
---|
145 | e.printStackTrace(); |
---|
146 | } |
---|
147 | return null; |
---|
148 | } |
---|
149 | |
---|
150 | public String RTGToSchedulingPlan(String rtg){ |
---|
151 | Transformer t = null; |
---|
152 | try { |
---|
153 | t = Transformers.RTG2SCHEDULIING_PLAN.geTransformer(); |
---|
154 | return transform(t, rtg); |
---|
155 | } catch (TransformerConfigurationException e) { |
---|
156 | e.printStackTrace(); |
---|
157 | } |
---|
158 | return null; |
---|
159 | } |
---|
160 | |
---|
161 | public JobDescription splitJobToTasks(String xmlJob) throws Exception{ |
---|
162 | |
---|
163 | int begin = 0; |
---|
164 | int end = 0; |
---|
165 | |
---|
166 | begin = xmlJob.indexOf("id=") + 4; |
---|
167 | end = xmlJob.indexOf("\"", begin); |
---|
168 | String jobId = xmlJob.substring(begin, end); |
---|
169 | |
---|
170 | JobDescription jobDesc = new JobDescription(xmlJob); |
---|
171 | jobDesc.setJobId(jobId); |
---|
172 | |
---|
173 | begin = 0; |
---|
174 | end = 0; |
---|
175 | int tb = 0; |
---|
176 | int te = 0; |
---|
177 | String taskId; |
---|
178 | while((begin = xmlJob.indexOf("<task", end)) != -1){ |
---|
179 | end = xmlJob.indexOf("</task>", begin); |
---|
180 | end += 7; |
---|
181 | String task = xmlJob.substring(begin, end); |
---|
182 | tb = task.indexOf("id=") + 4; |
---|
183 | te = task.indexOf("\"", tb); |
---|
184 | taskId = task.substring(tb, te); |
---|
185 | |
---|
186 | |
---|
187 | TaskDescription taskDescription = new TaskDescription(task); |
---|
188 | taskDescription.setTaskId(taskId); |
---|
189 | jobDesc.add(taskDescription); |
---|
190 | } |
---|
191 | |
---|
192 | return jobDesc; |
---|
193 | } |
---|
194 | |
---|
195 | public ApplicationProfileDescription getApplicationProfileDescription(String xmlJob){ |
---|
196 | ApplicationProfileDescription appProfDesc = new ApplicationProfileDescription(xmlJob); |
---|
197 | return appProfDesc; |
---|
198 | } |
---|
199 | |
---|
200 | public String extractJobId(String job){ |
---|
201 | Transformer t = null; |
---|
202 | |
---|
203 | try { |
---|
204 | |
---|
205 | t = Transformers.EXTRACT_JOB_ID.geTransformer(); |
---|
206 | return transform(t, job); |
---|
207 | |
---|
208 | } catch (TransformerConfigurationException e) { |
---|
209 | e.printStackTrace(); |
---|
210 | } |
---|
211 | |
---|
212 | return null; |
---|
213 | } |
---|
214 | |
---|
215 | public String jobPostProcessing(String job, String taskId, String executionDuration){ |
---|
216 | Transformer t = null; |
---|
217 | |
---|
218 | try { |
---|
219 | |
---|
220 | t = Transformers.JOB_POSTPROCESSING.geTransformer(); |
---|
221 | t.setParameter("TASK_ID", taskId); |
---|
222 | t.setParameter("EXEC_DURATION_VALUE", executionDuration); |
---|
223 | |
---|
224 | return transform(t, job); |
---|
225 | |
---|
226 | } catch (TransformerConfigurationException e) { |
---|
227 | e.printStackTrace(); |
---|
228 | } |
---|
229 | |
---|
230 | return null; |
---|
231 | } |
---|
232 | |
---|
233 | protected String transform(Transformer t, String input){ |
---|
234 | Writer writer = new StringWriter(); |
---|
235 | Reader reader = new StringReader(input); |
---|
236 | |
---|
237 | try { |
---|
238 | writer = new StringWriter(); |
---|
239 | reader = new StringReader(input); |
---|
240 | |
---|
241 | t.transform(new StreamSource(reader), new StreamResult(writer)); |
---|
242 | |
---|
243 | |
---|
244 | return writer.toString(); |
---|
245 | } catch (TransformerConfigurationException e) { |
---|
246 | e.printStackTrace(); |
---|
247 | } catch (TransformerException e) { |
---|
248 | e.printStackTrace(); |
---|
249 | } finally { |
---|
250 | try { |
---|
251 | writer.close(); |
---|
252 | reader.close(); |
---|
253 | } catch (IOException e) { |
---|
254 | e.printStackTrace(); |
---|
255 | } |
---|
256 | |
---|
257 | } |
---|
258 | return null; |
---|
259 | } |
---|
260 | } |
---|