package simulator.workload.reader.xmlJob; import java.io.File; import java.io.IOException; import java.io.StringReader; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.ResolverException; import org.exolab.castor.xml.Unmarshaller; import org.exolab.castor.xml.ValidationException; import org.exolab.castor.xml.XMLContext; import org.qcg.broker.schemas.jobdesc.QcgJob; /** * * @author Marcin Krystek * */ public class QcgXmlJobReader implements XMLJobReader { protected static Unmarshaller unmarshaller; static { XMLContext context = new XMLContext(); try { context.addPackage("org.qcg.broker.schemas.jobdesc.QcgJob"); unmarshaller = context.createUnmarshaller(); } catch (ResolverException e) { e.printStackTrace(); unmarshaller = null; } } protected WLFileAccess workload; public QcgXmlJobReader(File dir) throws IOException{ if(dir == null) throw new IOException("Set directory name with xml's and file name prefix."); String dirName = dir.getName(); if(!dir.exists()) throw new IOException("Directory "+dirName + " doeas not exist."); if(dir.isDirectory()){ this.workload = new DirReader(dir); } else { throw new IOException(dirName + " is not a directory or tar archive."); } } public void close() throws IOException { this.workload.close(); } public String getCurrentPosition() { return this.workload.getFileName(); } public String readRaw() throws IOException{ return this.workload.read(); } public QcgJob read() throws IOException { String s = readRaw(); StringReader reader = new StringReader(s); QcgJob job = null; try { job = (QcgJob) unmarshaller.unmarshal(reader); } catch (MarshalException e) { new IOException(e.getMessage()); } catch (ValidationException e) { new IOException(e.getMessage()); } finally { reader.close(); } return job; } public void reset() throws IOException { } }