package simulator.workload.reader.archive; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.util.Map; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.Marshaller; 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 simulator.workload.reader.archive.swf.SWFFields; import org.qcg.broker.schemas.jobdesc.FileType; import org.qcg.broker.schemas.jobdesc.ResourceConsumptionProfileType; import org.qcg.broker.schemas.jobdesc.Task; import org.qcg.broker.schemas.jobdesc.Workflow; import dcworms.schedframe.scheduling.utils.ApplicationProfileDescription; /** * * @author Marcin Krystek * */ public abstract class QcgWAJobReader extends AbstractWAReader { protected String currntJobID; protected Marshaller marshaller; protected Unmarshaller unmarshaller; 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(); unmarshaller = context.createUnmarshaller(); } catch (ResolverException e) { e.printStackTrace(); this.marshaller = null; unmarshaller = null; } } public org.qcg.broker.schemas.jobdesc.Job read() throws IOException { Task task = null; if(taskData == null) { taskData = waParser.readTask(); if(taskData == null) return null; currntJobID = taskData[SWFFields.DATA_JOB_NUMBER]; } org.qcg.broker.schemas.jobdesc.Job job = new org.qcg.broker.schemas.jobdesc.Job(); String appID = this.waParser.getIDMapping(currntJobID)[0]; job.setId(appID); while(taskData != null && this.waParser.getIDMapping(currntJobID)[0].equals(appID)){ task = createTask(taskData); if(task != null){ Workflow workflow = task.getWorkflow(); if(workflow != null){ String precTaskId = workflow.getParent(0).getContent(); workflow.getParent(0).setContent(waParser.getIDMapping(precTaskId)[0] + "_" + waParser.getIDMapping(precTaskId)[1]); } job.addTask(task); } taskData = waParser.readTask(); if(taskData != null) currntJobID = taskData[SWFFields.DATA_JOB_NUMBER]; } return job; } public String readRaw() throws IOException{ org.qcg.broker.schemas.jobdesc.Job 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; } public String mergeSwfAndAppProfile(Map applicationProfiles, String swfJobDesc) throws IOException{ StringReader reader = new StringReader(swfJobDesc); org.qcg.broker.schemas.jobdesc.Job job = null; try { job = (org.qcg.broker.schemas.jobdesc.Job)unmarshaller.unmarshal(reader); } catch (MarshalException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ValidationException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i = 0; i < job.getTaskCount(); i++){ try { ApplicationProfileDescription appProfileDescription = applicationProfiles.get(job.getTask(i).getExecution().getExecutable().getApplication().getAppProperty(0).getContent()); if(appProfileDescription != null){ Task patternTask = appProfileDescription.getDescription(); if(patternTask != null){ ResourceConsumptionProfileType[] rcp = patternTask.getExecution().getResourceConsumptionProfile(); job.getTask(i).getExecution().setResourceConsumptionProfile(rcp); FileType script = new FileType(); script.setName(patternTask.getId()); job.getTask(i).getExecution().addStdin(script); } } } catch (Exception e){ continue; } } 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(); } }