package simulator.workload.writer.xmlJob; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.ValidationException; import simulator.workload.fileFilters.QcgJobFileNameFilter; import org.qcg.broker.schemas.jobdesc.QcgJob; /** * * @author Marcin Krystek * */ public class QcgXmlJobWriter implements XMLJobWriter{ private Log log = LogFactory.getLog(QcgXmlJobWriter.class); public boolean overwrite; public String fileNamePrefix; public File directory; public QcgXmlJobWriter(String dirName, String fileNamePrefix){ this.overwrite = true; this.fileNamePrefix = fileNamePrefix; this.directory = new File(dirName); if(!directory.exists()) directory.mkdirs(); } public boolean write(QcgJob job) throws IOException{ String fileName = QcgJobFileNameFilter.FILE_NAME_PREFIX+"_"+job.getAppId()+".xml"; File file = new File(directory, fileName); if(file.exists()) { if(this.overwrite){ if(!file.delete()){ if(log.isErrorEnabled()) log.error("File " + fileName + " can not be deleted."); return false; } if(!file.createNewFile()){ if(log.isErrorEnabled()) log.error("File " + fileName + " can not be created."); return false; } } else { throw new IOException("File "+fileName + " exists and can not be removed. Set overwrite attribute value to true."); } } FileWriter writer = new FileWriter(file); try { job.marshal(writer); } catch (MarshalException e) { throw new IOException(e.getMessage()); } catch (ValidationException e) { throw new IOException(e.getMessage()); } finally { writer.close(); } return true; } public void setOverwrite(boolean overwrite) { this.overwrite = overwrite; } public void close() throws IOException{}; }