JAL-3878 Move CredentialType class to jalview.ws2.api package
[jalview.git] / src / jalview / ws2 / api / WebServiceJob.java
1 package jalview.ws2.api;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.util.Date;
6
7 import jalview.util.MathUtils;
8
9 /**
10  * {@code WebServiceJob} represents a job running on a remote server. The object
11  * contains all the information needed to associate the job with an originating
12  * client and url, service being run and to poll the job and retrieve the
13  * results from the server. The {@code WebServiceJob} object is provided by the
14  * {@link WebServiceClientI#submit} method when the job is created.
15  * 
16  * @see WebServiceClientI
17  * 
18  * @author mmwarowny
19  */
20 public class WebServiceJob
21 {
22   /** Unique id used internally by Jalview */
23   private final long internalId = MathUtils.getUID();
24
25   /** Name of the related client */
26   private final String serviceClient;
27
28   /** Name of the related service */
29   private final String serviceName;
30
31   /** URL the job is valid for */
32   private final String url;
33
34   /** External job id as given by the server */
35   private final String jobId;
36
37   /** Current status of the job */
38   private JobStatus status = JobStatus.READY;
39
40   private String log = "";
41
42   private String errorLog = "";
43
44   private Date creationTime = new Date();
45
46   public WebServiceJob(String serviceClient, String serviceName,
47       String url, String jobId)
48   {
49     this.serviceClient = serviceClient;
50     this.serviceName = serviceName;
51     this.url = url;
52     this.jobId = jobId;
53   }
54
55   /**
56    * Get a unique job id used internally by Jalview.
57    * 
58    * @return unique job id
59    */
60   public long getInternalId()
61   {
62     return internalId;
63   }
64
65   /**
66    * Get a URL this job originates from.
67    * 
68    * @return job URL
69    */
70   public String getUrl()
71   {
72     return url;
73   }
74
75   /**
76    * Get an id assigned to the job by the server.
77    * 
78    * @return job id handle
79    */
80   public String getJobId()
81   {
82     return jobId;
83   }
84
85   /**
86    * @return job status
87    */
88   public JobStatus getStatus()
89   {
90     return status;
91   }
92
93   /**
94    * Update status of the job and notify the listeners. This method should only
95    * be used by {@link WebServiceClientI} implementations when polling the job.
96    * 
97    * @param status
98    *          new job status
99    */
100   public void setStatus(JobStatus status)
101   {
102     JobStatus oldStatus = this.status;
103     this.status = status;
104     pcs.firePropertyChange("status", oldStatus, status);
105   }
106
107   /**
108    * @return standard progress log
109    */
110   public String getLog()
111   {
112     return log;
113   }
114
115   /**
116    * Update log string with data received from the server and notify the
117    * listeners. This method should only be used by {@link WebServiceClientI}
118    * implementations when polling the job.
119    * 
120    * @param log
121    *          new log string
122    */
123   public void setLog(String log)
124   {
125     String oldLog = this.log;
126     this.log = log;
127     pcs.firePropertyChange("log", oldLog, log);
128   }
129
130   /**
131    * @return error log
132    */
133   public String getErrorLog()
134   {
135     return errorLog;
136   }
137
138   /**
139    * Update error log string with data received from the server and notify the
140    * listeners. This method should only be used by {@link WebServiceClientI}
141    * implementations when polling the job
142    * 
143    * @param errorLog
144    *          new error log string
145    */
146   public void setErrorLog(String errorLog)
147   {
148     String oldLog = this.errorLog;
149     this.errorLog = errorLog;
150     pcs.firePropertyChange("errorLog", oldLog, errorLog);
151   }
152
153   /**
154    * @return Job creation time
155    */
156   public Date getCreationTime()
157   {
158     return creationTime;
159   }
160
161   public String toString()
162   {
163     return String.format("%s:%s [%s] Created %s", serviceClient, serviceName,
164         jobId, creationTime);
165   }
166
167   /* Methods related to bean listeners */
168   private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
169
170   public void addPropertyChangeListener(PropertyChangeListener listener)
171   {
172     pcs.addPropertyChangeListener(listener);
173   }
174
175   public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
176   {
177     pcs.addPropertyChangeListener(propertyName, listener);
178   }
179
180   public void removePropertyChangeListener(PropertyChangeListener listener)
181   {
182     pcs.removePropertyChangeListener(listener);
183   }
184
185   public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
186   {
187     pcs.removePropertyChangeListener(propertyName, listener);
188   }
189 }