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