Monitor.java :  » Media » projectopenfarm » openfarm » transmission » ws » jobmonitoring » Java Open Source

Java Open Source » Media » projectopenfarm 
projectopenfarm » openfarm » transmission » ws » jobmonitoring » Monitor.java
/**
 * Copyright (c) 2009 - 2010 - OpenFARM
 * Licence: MIT
 *
 * @Author Filipe Martins
 * @Date 11-May-2010 
 */
package openfarm.transmission.ws.jobmonitoring;

import java.util.Collection;
import java.util.Map;

import openfarm.multithreading.machine.IJobThread;
import openfarm.multithreading.machine.Job;
import openfarmtools.interpreter.exceptions.JobStateException;
import openfarmtools.metadata.pojos.IPojo;

public class Monitor implements IPojo
{
  private static final long serialVersionUID = 2381207227810039114L;
  private InterpreterJobState jobStatePojo = new InterpreterJobState();
    
  
  public Monitor(Collection<Map<String, IJobThread>> values)
  {        
    report(values);  
  }

  private void report(Collection<Map<String, IJobThread>> values)
  {  
    //Key = jobId & Value = JobThread
    for(Map<String,IJobThread> v : values)
    {
      for(String key : v.keySet())
      {
        IJobThread jobThread = v.get(key);      
            
        if(jobThread.getTemplate().isStopped())
          jobStatePojo.getJobsCanceled().add(jobThread.getJob());
        else if(jobThread.getTemplate().isDone())
          jobStatePojo.getJobsDone().add(jobThread.getJob());
        else
          jobStatePojo.getJobsRunning().add(jobThread.getJob());  
      }
    }
  }  
  
  public String toString()
  {    
    String jobsRunning = "Jobs Running: " + jobStatePojo.getJobsRunning()+ "\n";
    String jobsDone = "Jobs completed successfuly: " + jobStatePojo.getJobsDone() + "\n";
    String jobsCanceled = "Jobs canceled: " + jobStatePojo.getJobsCanceled() + "\n";      
    
    return jobsRunning + jobsDone + jobsCanceled;
  }
  
  public CurrentState getJobState(String jobId) throws JobStateException
  {
    Job job = new Job();
    job.setJobId(jobId);
    
    if(jobStatePojo.getJobsRunning().contains(job))
      return CurrentState.RUNNING;
    
    if(jobStatePojo.getJobsDone().contains(job))
      return CurrentState.DONE;
    
    if(jobStatePojo.getJobsCanceled().contains(job))
      return CurrentState.CANCELED;
        
    throw new JobStateException(jobId+" is not under any state");
  }
  
  
  //Sets and Gets
  public InterpreterJobState getAllStates()
  {
    return this.jobStatePojo;
  }

  public void setJobState(InterpreterJobState jobState)
  {
    this.jobStatePojo = jobState;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.