package simulator.workload.reader.archive; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.ResolverException; import org.exolab.castor.xml.ValidationException; import org.exolab.castor.xml.XMLContext; import simulator.workload.reader.archive.swf.SWFFields; import org.qcg.broker.schemas.jobdesc.QcgJob; import org.qcg.broker.schemas.jobdesc.Task; /** * * @author Marcin Krystek * */ public abstract class QcgWAJobReader extends AbstractWAReader { protected String currntJobID; protected Marshaller marshaller; public QcgWAJobReader(String fileName) throws NullPointerException, IOException { super(fileName); init(); } public QcgWAJobReader(WAParser parser){ super(parser); init(); } protected void init(){ XMLContext context = new XMLContext(); try { context.addPackage("org.qcg.broker.schemas.jobdesc.QcgJob"); this.marshaller = context.createMarshaller(); } catch (ResolverException e) { e.printStackTrace(); this.marshaller = null; } } public QcgJob read() throws IOException { Task task = null; if(taskData == null) { taskData = waParser.readTask(); if(taskData == null) return null; currntJobID = taskData[SWFFields.DATA_JOB_NUMBER]; } QcgJob job = new QcgJob(); String appID = this.waParser.getIDMapping(currntJobID)[0]; job.setAppId(appID); while(taskData != null && this.waParser.getIDMapping(currntJobID)[0].equals(appID)){ task = createTask(taskData); if(task != null) job.addTask(task); taskData = waParser.readTask(); if(taskData != null) currntJobID = taskData[SWFFields.DATA_JOB_NUMBER]; } return job; } public String readRaw() throws IOException{ QcgJob job = null; if((job = read()) == null) return null; Writer w = new StringWriter(); try { this.marshaller.setWriter(w); this.marshaller.marshal(job); } catch (MarshalException e) { new IOException(e.getMessage()); } catch (ValidationException e) { new IOException(e.getMessage()); } return w.toString(); } public String getCurrentPosition() { return currntJobID; } }