1 package jalview.ws2.actions;
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
7 import jalview.datamodel.SequenceI;
8 import jalview.util.MathUtils;
9 import jalview.ws2.actions.api.JobI;
10 import jalview.ws2.actions.api.TaskEventListener;
11 import jalview.ws2.api.JobStatus;
12 import jalview.ws2.api.WebServiceJobHandle;
15 * Basic implementation of the {@link JobI} interface which stores internal job
16 * id, status, log and error log and provides getters to those fields.
17 * Additionally, it stores sequences that will be submitted as job input and the
18 * handle to the job on the server. Extending classes can add extra fields in
19 * order to associate additional data with the job.
21 * Observers can be registered with this bean-like object to listen to changes
22 * to {@code status}, {@code log}, and {@code errorLog} properties. Typically,
23 * the events are delegated to the {@link TaskEventListener} objects observing
24 * the task that created this job.
28 public abstract class BaseJob implements JobI
30 protected final long internalId = MathUtils.getUID();
32 protected final List<SequenceI> inputSeqs;
34 protected JobStatus status = JobStatus.CREATED;
36 protected String log = "";
38 protected String errorLog = "";
40 /* FIXME: server job is not specific to the BaseJob and should preferably
41 * be managed by classes using clients (tasks). */
42 protected WebServiceJobHandle serverJob;
44 public BaseJob(List<SequenceI> inputSeqs)
46 this.inputSeqs = inputSeqs;
53 public final long getInternalId()
59 * Return the list of input sequences associated with this job.
61 * @return input sequences
63 public List<SequenceI> getInputSequences()
69 * Check if inputs make a valid job.
71 * @return {@code true} if the input is valid.
73 public abstract boolean isInputValid();
76 * Check if the job is completed, This includes jobs with invalid input,
77 * successful and unsuccessful termination.
79 * @return {@code true} if job is completed successfully or not
81 public boolean isCompleted()
83 return !isInputValid() || getStatus().isDone();
87 public final JobStatus getStatus()
93 * Set new status of the job and notify listeners of the change. Job status is
94 * managed internally by tasks and should not be modified outside the task
95 * which created this job.
100 public final void setStatus(JobStatus status)
102 JobStatus oldStatus = this.status;
103 this.status = status;
104 pcs.firePropertyChange("status", oldStatus, status);
108 public final String getLog()
114 * Set log text and notify listeners of the change. Log is managed by tasks
115 * which created the job and should not be modified by other classes.
120 public final void setLog(String log)
122 String oldLog = this.log;
124 pcs.firePropertyChange("log", oldLog, log);
128 public final String getErrorLog()
134 * Set error log text and notify listeners of the change. Error log is managed
135 * by tasks which created the job and should not be modified by other classes.
139 public final void setErrorLog(String errorLog)
141 String oldLog = this.errorLog;
142 this.errorLog = errorLog;
143 pcs.firePropertyChange("errorLog", oldLog, errorLog);
147 * Return the job handle that identifies this job running on the server or
148 * {@code null} if the job was not submitted.
150 * @return server job handle
152 public final WebServiceJobHandle getServerJob()
158 * Set the server job handle once the job was submitted to the server. The
159 * handler is managed by the task which created this job and should not be
160 * modified by other classes.
164 public final void setServerJob(WebServiceJobHandle job)
166 this.serverJob = job;
169 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
172 * Register an observer that will be notified of changes to status, log and
176 * property change listener
178 public final void addPropertyChangeListener(PropertyChangeListener listener)
180 pcs.addPropertyChangeListener(listener);
184 * Remove the property listener from this object.
189 public final void removePropertyChangeListener(PropertyChangeListener listener)
191 pcs.removePropertyChangeListener(listener);