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 // TODO: make class non-abstract by removing isInputValid()
29 public abstract class BaseJob implements JobI
31 protected final long internalId = MathUtils.getUID();
33 protected final List<SequenceI> inputSeqs;
35 protected JobStatus status = JobStatus.CREATED;
37 protected String log = "";
39 protected String errorLog = "";
41 /* FIXME: server job is not specific to the BaseJob and should preferably
42 * be managed by classes using clients (tasks). */
43 protected WebServiceJobHandle serverJob;
45 public BaseJob(List<SequenceI> inputSeqs)
47 this.inputSeqs = inputSeqs;
54 public final long getInternalId()
60 * Return the list of input sequences associated with this job.
62 * @return input sequences
64 public List<SequenceI> getInputSequences()
70 * Check if inputs make a valid job.
72 * @return {@code true} if the input is valid.
74 // FIXME: method not necessary, may incorporate into task#prepare()
75 public abstract boolean isInputValid();
78 * Check if the job is completed, This includes jobs with invalid input,
79 * successful and unsuccessful termination.
81 * @return {@code true} if job is completed successfully or not
83 public boolean isCompleted()
85 return !isInputValid() || getStatus().isDone();
89 public final JobStatus getStatus()
95 * Set new status of the job and notify listeners of the change. Job status is
96 * managed internally by tasks and should not be modified outside the task
97 * which created this job.
102 public final void setStatus(JobStatus status)
104 JobStatus oldStatus = this.status;
105 this.status = status;
106 pcs.firePropertyChange("status", oldStatus, status);
110 public final String getLog()
116 * Set log text and notify listeners of the change. Log is managed by tasks
117 * which created the job and should not be modified by other classes.
122 public final void setLog(String log)
124 String oldLog = this.log;
126 pcs.firePropertyChange("log", oldLog, log);
130 public final String getErrorLog()
136 * Set error log text and notify listeners of the change. Error log is managed
137 * by tasks which created the job and should not be modified by other classes.
141 public final void setErrorLog(String errorLog)
143 String oldLog = this.errorLog;
144 this.errorLog = errorLog;
145 pcs.firePropertyChange("errorLog", oldLog, errorLog);
149 * Return the job handle that identifies this job running on the server or
150 * {@code null} if the job was not submitted.
152 * @return server job handle
154 public final WebServiceJobHandle getServerJob()
160 * Set the server job handle once the job was submitted to the server. The
161 * handler is managed by the task which created this job and should not be
162 * modified by other classes.
166 public final void setServerJob(WebServiceJobHandle job)
168 this.serverJob = job;
171 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
174 * Register an observer that will be notified of changes to status, log and
178 * property change listener
180 public final void addPropertyChangeListener(PropertyChangeListener listener)
182 pcs.addPropertyChangeListener(listener);
186 * Remove the property listener from this object.
191 public final void removePropertyChangeListener(PropertyChangeListener listener)
193 pcs.removePropertyChangeListener(listener);