1 | package simulator.workload.writer.xmlJob; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileWriter; |
---|
5 | import java.io.IOException; |
---|
6 | |
---|
7 | import org.apache.commons.logging.Log; |
---|
8 | import org.apache.commons.logging.LogFactory; |
---|
9 | import org.exolab.castor.xml.MarshalException; |
---|
10 | import org.exolab.castor.xml.ValidationException; |
---|
11 | |
---|
12 | import simulator.workload.fileFilters.QcgJobFileNameFilter; |
---|
13 | |
---|
14 | |
---|
15 | /** |
---|
16 | * |
---|
17 | * @author Marcin Krystek |
---|
18 | * |
---|
19 | */ |
---|
20 | |
---|
21 | public class QcgXmlJobWriter implements XMLJobWriter<org.qcg.broker.schemas.jobdesc.Job>{ |
---|
22 | |
---|
23 | private Log log = LogFactory.getLog(QcgXmlJobWriter.class); |
---|
24 | public boolean overwrite; |
---|
25 | public String fileNamePrefix; |
---|
26 | public File directory; |
---|
27 | |
---|
28 | public QcgXmlJobWriter(String dirName, String fileNamePrefix){ |
---|
29 | this.overwrite = true; |
---|
30 | this.fileNamePrefix = fileNamePrefix; |
---|
31 | this.directory = new File(dirName); |
---|
32 | |
---|
33 | if(!directory.exists()) |
---|
34 | directory.mkdirs(); |
---|
35 | |
---|
36 | } |
---|
37 | |
---|
38 | public boolean write(org.qcg.broker.schemas.jobdesc.Job job) throws IOException{ |
---|
39 | String fileName = QcgJobFileNameFilter.FILE_NAME_PREFIX+"_"+job.getId()+".xml"; |
---|
40 | |
---|
41 | File file = new File(directory, fileName); |
---|
42 | if(file.exists()) { |
---|
43 | if(this.overwrite){ |
---|
44 | if(!file.delete()){ |
---|
45 | if(log.isErrorEnabled()) |
---|
46 | log.error("File " + fileName + " can not be deleted."); |
---|
47 | return false; |
---|
48 | } |
---|
49 | if(!file.createNewFile()){ |
---|
50 | if(log.isErrorEnabled()) |
---|
51 | log.error("File " + fileName + " can not be created."); |
---|
52 | return false; |
---|
53 | } |
---|
54 | } else { |
---|
55 | throw new IOException("File "+fileName + " exists and can not be removed. Set overwrite attribute value to true."); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | FileWriter writer = new FileWriter(file); |
---|
60 | |
---|
61 | try { |
---|
62 | |
---|
63 | job.marshal(writer); |
---|
64 | |
---|
65 | } catch (MarshalException e) { |
---|
66 | throw new IOException(e.getMessage()); |
---|
67 | } catch (ValidationException e) { |
---|
68 | throw new IOException(e.getMessage()); |
---|
69 | } finally { |
---|
70 | writer.close(); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | return true; |
---|
75 | } |
---|
76 | |
---|
77 | public void setOverwrite(boolean overwrite) { |
---|
78 | this.overwrite = overwrite; |
---|
79 | } |
---|
80 | |
---|
81 | public void close() throws IOException{}; |
---|
82 | } |
---|