1 package com.atlassian.scheduler.core.status;
2
3 import com.atlassian.scheduler.JobRunner;
4 import com.atlassian.scheduler.config.JobId;
5 import com.atlassian.scheduler.config.JobRunnerKey;
6 import com.atlassian.scheduler.config.RunMode;
7 import com.atlassian.scheduler.config.Schedule;
8 import com.atlassian.scheduler.core.AbstractSchedulerService;
9 import com.atlassian.scheduler.status.JobDetails;
10
11 import javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13 import java.util.Date;
14
15 import static com.atlassian.util.concurrent.Assertions.notNull;
16
17 /**
18 * Converts a scheduler implementation's internal representation of a job into a
19 * {@code JobDetails}. This class produces {@code LazyJobDetails} instances when
20 * the {@code JobRunner} is registered as opposed to deserializing the parameters
21 * immediately.
22 *
23 * @param <T> The type that the scheduler implementation uses as its internal representation
24 * of a job.
25 * @since v1.0
26 */
27 public abstract class AbstractJobDetailsFactory<T> {
28 private final AbstractSchedulerService schedulerService;
29
30 protected AbstractJobDetailsFactory(AbstractSchedulerService schedulerService) {
31 this.schedulerService = notNull("schedulerService", schedulerService);
32 }
33
34 /**
35 * Transforms the scheduler's internal representation of a job into a {@link JobDetails}.
36 * This will attempt to reconstruct the job's parameters using the {@code ClassLoader} of the
37 * job's {@link JobRunner} and return an {@link UnusableJobDetails} if the job runner
38 * is not registered or its class loader cannot deserialize the parameters map.
39 *
40 * @param jobId the job's ID
41 * @param jobData the internal representation of the job
42 * @param runMode the expected run mode of the job
43 * @return the corresponding job details
44 */
45 public JobDetails buildJobDetails(final JobId jobId, final T jobData,
46 final RunMode runMode) {
47 notNull("jobId", jobId);
48 notNull("jobData", jobData);
49 notNull("runMode", runMode);
50
51 final JobRunnerKey jobRunnerKey = notNull("jobRunnerKey", getJobRunnerKey(jobData));
52 final Schedule schedule = notNull("schedule", getSchedule(jobData));
53 final Date nextRunTime = getNextRunTime(jobData);
54 final byte[] parameters = getSerializedParameters(jobData);
55 return new LazyJobDetails(schedulerService, jobId, jobRunnerKey, runMode, schedule, nextRunTime, parameters);
56 }
57
58
59 /**
60 * Provided by the scheduler implementation to extract the job's {@link JobRunnerKey} from
61 * the scheduler's internal representation of the job.
62 *
63 * @param jobData the scheduler's internal representation of the job
64 * @return the key for the job's target job runner
65 */
66 @Nonnull
67 protected abstract JobRunnerKey getJobRunnerKey(T jobData);
68
69 /**
70 * Provided by the scheduler implementation to extract the job's {@link Schedule} from
71 * the scheduler's internal representation of the job.
72 *
73 * @param jobData the scheduler's internal representation of the job
74 * @return the job's corresponding {@link Schedule}
75 */
76 @Nonnull
77 protected abstract Schedule getSchedule(T jobData);
78
79 /**
80 * Provided by the scheduler implementation to extract the job's next scheduled run time from
81 * the scheduler's internal representation of the job.
82 *
83 * @param jobData the scheduler's internal representation of the job
84 * @return the job's next expected run time; may be {@code null} if the job will not be run again or
85 * if the scheduler does not provide information about future run times
86 */
87 @Nullable
88 protected abstract Date getNextRunTime(T jobData);
89
90 /**
91 * Provided by the scheduler implementation to extract the job's parameters map (in
92 * serialized form) from the scheduler's internal representation of the job.
93 *
94 * @param jobData the scheduler's internal representation of the job
95 * @return a byte array containing the parameters map in serialized form; may be {@code null},
96 * in which case an empty map will be substituted
97 */
98 @Nullable
99 protected abstract byte[] getSerializedParameters(T jobData);
100 }