1 | package simulator.workload.reader.xmlJob; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.StringReader; |
---|
6 | |
---|
7 | import org.exolab.castor.xml.MarshalException; |
---|
8 | import org.exolab.castor.xml.ResolverException; |
---|
9 | import org.exolab.castor.xml.Unmarshaller; |
---|
10 | import org.exolab.castor.xml.ValidationException; |
---|
11 | import org.exolab.castor.xml.XMLContext; |
---|
12 | |
---|
13 | |
---|
14 | import org.qcg.broker.schemas.jobdesc.QcgJob; |
---|
15 | |
---|
16 | /** |
---|
17 | * |
---|
18 | * @author Marcin Krystek |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | public class Grms3XmlJobReader implements XMLJobReader<QcgJob> { |
---|
23 | |
---|
24 | protected static Unmarshaller unmarshaller; |
---|
25 | static { |
---|
26 | XMLContext context = new XMLContext(); |
---|
27 | try { |
---|
28 | context.addPackage("org.qcg.broker.schemas.jobdesc.QcgJob"); |
---|
29 | unmarshaller = context.createUnmarshaller(); |
---|
30 | } catch (ResolverException e) { |
---|
31 | e.printStackTrace(); |
---|
32 | unmarshaller = null; |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | protected WLFileAccess workload; |
---|
37 | |
---|
38 | |
---|
39 | public Grms3XmlJobReader(File dir) throws IOException{ |
---|
40 | if(dir == null) |
---|
41 | throw new IOException("Set directory name with xml's and file name prefix."); |
---|
42 | |
---|
43 | String dirName = dir.getName(); |
---|
44 | |
---|
45 | if(!dir.exists()) |
---|
46 | throw new IOException("Directory "+dirName + " doeas not exist."); |
---|
47 | |
---|
48 | if(dir.isDirectory()){ |
---|
49 | this.workload = new DirReader(dir); |
---|
50 | } else if(dir.getName().endsWith(".tar")){ |
---|
51 | this.workload = new TarReader(dir); |
---|
52 | } else { |
---|
53 | throw new IOException(dirName + " is not a directory or tar archive."); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | public void close() throws IOException { |
---|
58 | this.workload.close(); |
---|
59 | } |
---|
60 | |
---|
61 | public String getCurrentPosition() { |
---|
62 | return this.workload.getFileName(); |
---|
63 | } |
---|
64 | |
---|
65 | public String readRaw() throws IOException{ |
---|
66 | return this.workload.read(); |
---|
67 | } |
---|
68 | |
---|
69 | public QcgJob read() throws IOException { |
---|
70 | |
---|
71 | String s = readRaw(); |
---|
72 | StringReader reader = new StringReader(s); |
---|
73 | QcgJob job = null; |
---|
74 | |
---|
75 | try { |
---|
76 | |
---|
77 | job = (QcgJob) unmarshaller.unmarshal(reader); |
---|
78 | |
---|
79 | } catch (MarshalException e) { |
---|
80 | new IOException(e.getMessage()); |
---|
81 | } catch (ValidationException e) { |
---|
82 | new IOException(e.getMessage()); |
---|
83 | } finally { |
---|
84 | reader.close(); |
---|
85 | } |
---|
86 | |
---|
87 | return job; |
---|
88 | } |
---|
89 | |
---|
90 | public void reset() throws IOException { |
---|
91 | } |
---|
92 | } |
---|