1 | package simulator.workload.reader.archive; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.StringWriter; |
---|
5 | import java.io.Writer; |
---|
6 | |
---|
7 | import org.exolab.castor.xml.MarshalException; |
---|
8 | import org.exolab.castor.xml.Marshaller; |
---|
9 | import org.exolab.castor.xml.ResolverException; |
---|
10 | import org.exolab.castor.xml.ValidationException; |
---|
11 | import org.exolab.castor.xml.XMLContext; |
---|
12 | |
---|
13 | import simulator.workload.reader.archive.swf.SWFFields; |
---|
14 | import org.qcg.broker.schemas.jobdesc.QcgJob; |
---|
15 | import org.qcg.broker.schemas.jobdesc.Task; |
---|
16 | |
---|
17 | /** |
---|
18 | * |
---|
19 | * @author Marcin Krystek |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | public abstract class GrmsWAJobReader extends AbstractWAReader<Task, QcgJob> { |
---|
24 | |
---|
25 | protected String currntJobID; |
---|
26 | protected Marshaller marshaller; |
---|
27 | |
---|
28 | public GrmsWAJobReader(String fileName) throws NullPointerException, |
---|
29 | IOException { |
---|
30 | super(fileName); |
---|
31 | init(); |
---|
32 | } |
---|
33 | |
---|
34 | public GrmsWAJobReader(WAParser parser){ |
---|
35 | super(parser); |
---|
36 | init(); |
---|
37 | } |
---|
38 | |
---|
39 | protected void init(){ |
---|
40 | XMLContext context = new XMLContext(); |
---|
41 | try { |
---|
42 | context.addPackage("org.qcg.broker.schemas.jobdesc.QcgJob"); |
---|
43 | this.marshaller = context.createMarshaller(); |
---|
44 | } catch (ResolverException e) { |
---|
45 | e.printStackTrace(); |
---|
46 | this.marshaller = null; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public QcgJob read() throws IOException { |
---|
51 | Task task = null; |
---|
52 | |
---|
53 | if(taskData == null) { |
---|
54 | taskData = waParser.readTask(); |
---|
55 | if(taskData == null) return null; |
---|
56 | |
---|
57 | currntJobID = taskData[SWFFields.DATA_JOB_NUMBER]; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | QcgJob job = new QcgJob(); |
---|
62 | String appID = this.waParser.getIDMapping(currntJobID)[0]; |
---|
63 | job.setAppId(appID); |
---|
64 | |
---|
65 | |
---|
66 | while(taskData != null && this.waParser.getIDMapping(currntJobID)[0].equals(appID)){ |
---|
67 | |
---|
68 | task = createTask(taskData); |
---|
69 | if(task != null) |
---|
70 | job.addTask(task); |
---|
71 | taskData = waParser.readTask(); |
---|
72 | if(taskData != null) |
---|
73 | currntJobID = taskData[SWFFields.DATA_JOB_NUMBER]; |
---|
74 | } |
---|
75 | |
---|
76 | return job; |
---|
77 | } |
---|
78 | |
---|
79 | public String readRaw() throws IOException{ |
---|
80 | QcgJob job = null; |
---|
81 | |
---|
82 | if((job = read()) == null) |
---|
83 | return null; |
---|
84 | |
---|
85 | Writer w = new StringWriter(); |
---|
86 | try { |
---|
87 | this.marshaller.setWriter(w); |
---|
88 | this.marshaller.marshal(job); |
---|
89 | } catch (MarshalException e) { |
---|
90 | new IOException(e.getMessage()); |
---|
91 | } catch (ValidationException e) { |
---|
92 | new IOException(e.getMessage()); |
---|
93 | } |
---|
94 | |
---|
95 | return w.toString(); |
---|
96 | } |
---|
97 | |
---|
98 | public String getCurrentPosition() { |
---|
99 | return currntJobID; |
---|
100 | } |
---|
101 | |
---|
102 | } |
---|