1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class DBJobDescriptor extends AbstractJobDescriptor
48 implements Persistent
49 {
50
51
52 /*** The table name. */
53 public static final String TABLE_NAME = "ledge_scheduler";
54
55 /*** The key columns. */
56 public static final String[] KEY_COLUMNS = new String[] { "job_id" };
57
58
59
60 /*** The persistence. */
61 private Persistence persistence;
62
63 /*** The scheduler */
64 private AbstractScheduler scheduler;
65
66 /*** The job id. */
67 private long jobId = -1L;
68
69 /***
70 * Constructor.
71 *
72 * @param persistence the persitence component.
73 * @param scheduler the scheduler component.
74 */
75 DBJobDescriptor(Persistence persistence, AbstractScheduler scheduler)
76 {
77 this.persistence = persistence;
78 this.scheduler = scheduler;
79 }
80
81
82
83 /***
84 * Returns the name of the table this type is mapped to.
85 *
86 * @return the name of the table.
87 */
88 public String getTable()
89 {
90 return TABLE_NAME;
91 }
92
93 /***
94 * Returns the names of the key columns.
95 *
96 * @return the names of the key columns.
97 */
98 public String[] getKeyColumns()
99 {
100 return KEY_COLUMNS;
101 }
102
103 /***
104 * {@inheritDoc}
105 */
106 public void getData(OutputRecord record) throws PersistenceException
107 {
108 record.setLong("job_id", jobId);
109 record.setString("job_name", getName());
110 record.setString("schedule_type", getSchedule().getType());
111 record.setString("schedule_config", getSchedule().getConfig());
112 record.setString("job_class_name", getJobClassName());
113 if (getArgument() != null)
114 {
115 record.setString("argument", getArgument());
116 }
117 else
118 {
119 record.setNull("argument");
120 }
121 record.setInteger("run_count", getRunCount());
122 record.setInteger("run_count_limit", getRunCountLimit());
123 if (getLastRunTime() != null)
124 {
125 record.setTimestamp("last_run_time", getLastRunTime());
126 }
127 else
128 {
129 record.setNull("last_run_time");
130 }
131 if (getTimeLimitStart() != null)
132 {
133 record.setTimestamp("run_time_limit_start", getTimeLimitStart());
134 }
135 else
136 {
137 record.setNull("run_time_limit_start");
138 }
139 if (getTimeLimitEnd() != null)
140 {
141 record.setTimestamp("run_time_limit_end", getTimeLimitEnd());
142 }
143 else
144 {
145 record.setNull("run_time_limit_end");
146 }
147 record.setBoolean("reentrant", isReentrant());
148 record.setBoolean("enabled", isEnabled());
149 }
150
151 /***
152 * {@inheritDoc}
153 */
154 public void setData(InputRecord record) throws PersistenceException
155 {
156 jobId = record.getLong("job_id");
157 String name = record.getString("job_name");
158 String scheduleType = record.getString("schedule_type");
159 String scheduleConfig = record.getString("schedule_config");
160 String jobClassName = record.getString("job_class_name");
161 Schedule schedule = null;
162 try
163 {
164 schedule = scheduler.createSchedule(scheduleType, scheduleConfig);
165 }
166 catch (InvalidScheduleException e)
167 {
168 throw new PersistenceException("failed to create schedule", e);
169 }
170 try
171 {
172 super.init(name, schedule, jobClassName);
173 if (!record.isNull("argument"))
174 {
175 argument = record.getString("argument");
176 }
177 if (!record.isNull("run_count"))
178 {
179 runCount = record.getInteger("run_count");
180 }
181 if (!record.isNull("run_count_limit"))
182 {
183 runCountLimit = record.getInteger("run_count_limit");
184 }
185 if (!record.isNull("last_run_time"))
186 {
187 lastRunTime = record.getDate("last_run_time");
188 }
189 if (!record.isNull("run_time_limit_start"))
190 {
191 runTimeLimitStart = record.getDate("run_time_limit_start");
192 }
193 if (!record.isNull("run_time_limit_end"))
194 {
195 runTimeLimitEnd = record.getDate("run_time_limit_end");
196 }
197 if (!record.isNull("auto_clean"))
198 {
199 autoClean = record.getBoolean("auto_clean");
200 }
201 if (!record.isNull("reentrant"))
202 {
203 reentrant = record.getBoolean("reentrant");
204 }
205 if (!record.isNull("enabled"))
206 {
207 enabled = record.getBoolean("enabled");
208 }
209 }
210 catch (Exception e)
211 {
212 throw new PersistenceException("Failed to initialize scheduled job", e);
213 }
214 }
215
216 /***
217 * {@inheritDoc}
218 */
219 public boolean getSaved()
220 {
221 return jobId != -1L;
222 }
223
224 /***
225 * {@inheritDoc}
226 */
227 public void setSaved(long id)
228 {
229 this.jobId = id;
230 }
231
232
233
234 /***
235 * {@inheritDoc}
236 */
237 protected void saveChanges() throws JobModificationException
238 {
239 try
240 {
241 persistence.save(this);
242 }
243 catch (PersistenceException e)
244 {
245 throw new JobModificationException("failed to save job state", e);
246 }
247 }
248
249 }