View Javadoc

1   /*
2    * $Id: DefaultTaskStatus.java,v 1.1 2006/06/16 14:50:06 kea Exp $
3    */
4   
5   package org.astrogrid.status;
6   
7   import java.util.Vector;
8   import java.util.Date;
9   import java.security.Principal;
10  
11  /***
12   * Default implementation of task status.  Can also be used as a soapy bean
13   */
14  
15  public class DefaultTaskStatus implements TaskStatus
16  {
17     
18     /*** The ID of the task this is the status for */
19     String id = null;
20     /*** The owner of the task - who is running it */
21     Principal owner = null;
22     String stage = null;
23     String source = null;
24     String message = "";
25     Date timestamp = new Date();
26     
27     long progress = -1;
28     long progressMax = -1;
29     String progressText = "";
30     
31     Vector details = new Vector();
32     TaskStatus previous = null;
33     
34     /*** Bean (empty) constructor for SOAP messages, etc */
35     public DefaultTaskStatus() {
36     }
37     
38     /*** Constructs new task with given one as previous, and copies in owner, source, etc */
39     public DefaultTaskStatus(TaskStatus existing, String newStage) {
40        this.id = existing.getId();
41        this.owner = existing.getOwner();
42        this.stage = newStage;
43        this.source = existing.getSource();
44  
45        this.previous = existing;
46        
47     }
48     
49     public void setId(String id)  {     this.id = id;  }
50     
51     public String getId()   {     return id;   }
52     
53     public void setPrevious(TaskStatus previous) {
54        if (this == previous) {
55           throw new IllegalArgumentException("Setting previous to self; circular link");
56        }
57        this.previous = previous;
58     }
59     
60     public TaskStatus getPrevious()  {     return previous;  }
61     
62    
63     public void setMessage(String message) {     this.message = message; }
64     
65     public String getMessage() {     return message;   }
66     
67     public void setSource(String source)   {     this.source = source;   }
68     
69     public String getSource()  {     return source; }
70     
71     public void setStage(String stage)  {     this.stage = stage.trim();  }
72     
73     public String getStage()   {     return stage;  }
74     
75     public void setOwner(Principal owner)  {     this.owner = owner;  }
76     
77     public Principal getOwner()   {     return owner;  }
78     
79     /*** Returns true if the task has finished, whether it has completed successfully or not */
80     public boolean isFinished()
81     {
82        return (stage.toUpperCase().equals(COMPLETE.toUpperCase()) ||
83                stage.toUpperCase().equals(ABORTED.toUpperCase()) ||
84                stage.toUpperCase().equals(ERROR.toUpperCase()));
85     }
86     
87     public void setTimestamp(Date timestamp)  {     this.timestamp = timestamp;   }
88     
89     public Date getTimestamp() {     return timestamp; }
90     
91     public void setProgressMax(long progressMax) {     this.progressMax = progressMax;  }
92     
93     public long getProgressMax()  {     return progressMax;  }
94     
95     public void setProgress(long progress) {     this.progress = progress;  }
96     
97     public long getProgress()  {     return progress;  }
98     
99     public void setDetails(String[] newDetails) {
100       for (int i = 0; i < newDetails.length; i++)
101       {
102          details.add(newDetails[i]);
103       }
104    }
105    
106    public void addDetail(String message) {
107       details.add(message);
108    }
109    public String[] getDetails()  {
110       return (String[]) details.toArray(new String[] {});
111    }
112 
113    public void setProgressText(String progressText)   {     this.progressText = progressText;   }
114    
115    public String getProgressText()  {     return progressText; }
116    
117    /*** Returns the progress as a human readable string */
118    public String getProgressMsg() {
119       if (getProgress()==-1) {
120          return progressText;
121       }
122       else if (progressMax==-1) {
123          return progressText+" "+progress;
124       }
125       else {
126          int percent = (int) ( progress *100 / progressMax);
127          return progressText+" "+progressText+" of "+progressMax+ "("+percent+"%)";
128       }
129    }
130 
131    /*** Returns first status  */
132    public TaskStatus getFirst() {
133 
134       TaskStatus first = this;
135       while (first.getPrevious() != null) { first = first.getPrevious(); }
136       return first;
137    }
138 
139    /*** Start time is the timestamp of the first task */
140    public Date getStartTime() {
141       return getFirst().getTimestamp();
142    }
143    
144    public String toString() {
145       return "Status ["+getId()+"] "+getStage()+" @"+getTimestamp()+" from "+getSource()+" by "+getOwner(); //+" (prev="+getPrevious()+")";
146    }
147 }