source: DCWoRMS/trunk/src/simulator/workload/reader/xmlJob/QcgXmlJobReader.java @ 477

Revision 477, 1.9 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.workload.reader.xmlJob;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.StringReader;
6
7import org.exolab.castor.xml.MarshalException;
8import org.exolab.castor.xml.ResolverException;
9import org.exolab.castor.xml.Unmarshaller;
10import org.exolab.castor.xml.ValidationException;
11import org.exolab.castor.xml.XMLContext;
12
13
14import org.qcg.broker.schemas.jobdesc.QcgJob;
15
16/**
17 *
18 * @author Marcin Krystek
19 *
20 */
21
22public class QcgXmlJobReader 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 QcgXmlJobReader(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 {
51                        throw new IOException(dirName + " is not a directory or tar archive.");
52                }
53        }
54
55        public void close() throws IOException {
56                this.workload.close();
57        }
58
59        public String getCurrentPosition() {
60                return this.workload.getFileName();
61        }
62       
63        public String readRaw() throws IOException{
64                return this.workload.read();
65        }
66
67        public QcgJob read() throws IOException {
68
69                String s = readRaw();
70                StringReader reader = new StringReader(s);
71                QcgJob job = null;
72               
73                try {
74               
75                        job = (QcgJob) unmarshaller.unmarshal(reader);
76               
77                } catch (MarshalException e) {
78                        new IOException(e.getMessage());
79                } catch (ValidationException e) {
80                        new IOException(e.getMessage());
81                } finally {
82                        reader.close();
83                }
84               
85                return job;
86        }
87
88        public void reset() throws IOException {
89        }
90}
Note: See TracBrowser for help on using the repository browser.