//----------------------------------------------------------------------
//This code is developed as part of the Java CoG Kit project
//The terms of the license can be found at http://www.cogkit.org/license
//This message may not be removed or altered.
//----------------------------------------------------------------------

/*
 * Created on Apr 21, 2009
 */
package org.globus.cog.abstraction.coaster.service.job.manager;

import org.globus.cog.abstraction.impl.common.StatusImpl;
import org.globus.cog.abstraction.impl.common.execution.WallTime;
import org.globus.cog.abstraction.interfaces.JobSpecification;
import org.globus.cog.abstraction.interfaces.Status;
import org.globus.cog.abstraction.interfaces.Task;

public class Job implements Comparable<Job> {
    private int id;
    private Task task;
    /** 
       If not 1, number of MPI CPUs required
       Set by JobSpecification attribute "hostcount" 
     */
    int cpus; 
    private TimeInterval walltime;
    private Time starttime, endtime;
    private boolean done;

    private static int sid;
    
    public Job() {
    }

    public Job(Task task) {
        setTask(task);
        JobSpecification spec = 
            (JobSpecification) task.getSpecification();
        Object tmp = spec.getAttribute("hostcount");
        if (tmp == null)
            cpus = 1;
        else
            cpus = Integer.parseInt((String) tmp);
        synchronized (Job.class) {
            id = sid++;
        }
    }

    public Job(Task task, int cpus) {
        setTask(task);
        this.cpus = cpus;
    }
    
    void setTask(Task task) {
        this.task = task;
       
        JobSpecification spec = 
            (JobSpecification) task.getSpecification();
        Object tmp = spec.getAttribute("maxwalltime");
        if (tmp == null) {
            this.walltime = TimeInterval.fromSeconds(10 * 60);
        }
        else {
            WallTime wt = new WallTime(tmp.toString());
            this.walltime = TimeInterval.fromSeconds(wt.getSeconds());
        }
    }
    
    public Time getEndTime() {
        if (endtime == null) {
            if (starttime == null) {
                return null;
            }
            else {
                return starttime.add(walltime);
            }
        }
        else {
            return endtime;
        }
    }
    
    public void setEndTime(Time endtime) {
        this.endtime = endtime;
    }
    
    public Time getStartTime() {
        return starttime;
    }
    
    public void setStartTime(Time t) {
        this.starttime = t;
    }

    public boolean isDone() {
        return done;
    }

    public int compareTo(Job other) {
        // Job other = (Job) obj;
        TimeInterval diff = walltime.subtract(other.walltime);
        if (diff.getMilliseconds() == 0) {
            return id - other.id;
        }
        else {
            return (int) diff.getMilliseconds();
        }
    }
    
    public TimeInterval getMaxWallTime() {
        return walltime;
    }
    
    public void setMaxWallTime(TimeInterval walltime) {
        this.walltime = walltime;
    }

    public String toString() {
        return id + ":" + walltime;
    }

    public void start() {
        starttime = Time.now();
        endtime = Time.now().add(walltime);
    }

    public void fail(String message, Exception e) {
        task.setStatus(new StatusImpl(Status.FAILED, message, e));
    }
    
    public Task getTask() {
        return task;
    }

    public int getCpus() {
        return cpus;
    }
}

@@ -419,16 +419,49 @@
 nager;%0A%0A
+import org.apache.log4j.Logger;%0A%0A
 import o
@@ -790,16 +790,86 @@
 %3CJob%3E %7B%0A
+    public static final Logger logger = Logger.getLogger(Job.class);%0A%0A
     priv
@@ -910,17 +910,16 @@
 %0A    /**
- 
 %0A       
@@ -1008,17 +1008,16 @@
 stcount%22
- 
 %0A     */
@@ -1030,17 +1030,16 @@
 nt cpus;
- 
 %0A    pri
@@ -1158,20 +1158,16 @@
 nt sid;%0A
-    
 %0A    pub
@@ -1259,33 +1259,32 @@
 ification spec =
- 
 %0A            (Jo
@@ -1653,28 +1653,24 @@
 cpus;%0A    %7D%0A
-    
 %0A    void se
@@ -1714,23 +1714,16 @@
 = task;%0A
-       
 %0A       
@@ -1746,17 +1746,16 @@
 n spec =
- 
 %0A       
@@ -2108,36 +2108,32 @@
         %7D%0A    %7D%0A
-    
 %0A    public Time
@@ -2401,36 +2401,32 @@
         %7D%0A    %7D%0A
-    
 %0A    public void
@@ -2483,36 +2483,32 @@
  endtime;%0A    %7D%0A
-    
 %0A    public Time
@@ -2549,36 +2549,32 @@
 tarttime;%0A    %7D%0A
-    
 %0A    public void
@@ -2988,28 +2988,24 @@
     %7D%0A    %7D%0A
-    
 %0A    public 
@@ -3059,36 +3059,32 @@
 walltime;%0A    %7D%0A
-    
 %0A    public void
@@ -3208,142 +3208,635 @@
     
-return id + %22:%22 + walltime;%0A    %7D%0A%0A    public void start() %7B%0A        starttime = Time.now();%0A        endtime = Time.now().add(walltime
+StringBuilder sb = new StringBuilder(128);%0A        sb.append(%22Job(id:%22);%0A        sb.append(id);%0A        sb.append(%22 %22);%0A        sb.append(walltime);%0A        if (cpus != 1) %7B%0A            sb.append(%22 %5B%22);%0A            sb.append(cpus);%0A            sb.append(%22%5D%22);%0A        %7D%0A        sb.append(%22)%22);%0A        return sb.toString();%0A    %7D%0A%0A    public void start() %7B%0A        starttime = Time.now();%0A        endtime = Time.now().add(walltime);%0A        if (logger.isDebugEnabled())%0A            logger.debug(this.toString() + %22 start: %22 +%0A                         starttime.getSeconds() + %22-%22 +%0A                         endtime.getSeconds()
 );%0A 
@@ -3966,20 +3966,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

Improve toString() and logging