1 package org.apache.turbine.services.schedule;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Calendar;
23 import java.util.Date;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.apache.turbine.util.TurbineException;
29
30
31
32
33
34
35 public abstract class AbstractJobEntry implements JobEntry
36 {
37
38 protected static final Logger log = LogManager.getLogger(ScheduleService.LOGGER_NAME);
39
40
41 private boolean jobIsActive = false;
42
43
44 private long runtime = 0;
45
46
47 protected enum ScheduleType {
48 SECOND,
49 MINUTE,
50 WEEK_DAY,
51 DAY_OF_MONTH,
52 DAILY
53 }
54
55
56
57
58 public AbstractJobEntry()
59 {
60 super();
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public AbstractJobEntry(int sec,
95 int min,
96 int hour,
97 int wd,
98 int day_mo,
99 String task)
100 throws TurbineException
101 {
102 this();
103
104 if (StringUtils.isEmpty(task))
105 {
106 throw new TurbineException("Error in JobEntry. " +
107 "Bad Job parameter. Task not set.");
108 }
109
110 setSecond(sec);
111 setMinute(min);
112 setHour(hour);
113 setWeekDay(wd);
114 setDayOfMonth(day_mo);
115 setTask(task);
116
117 calcRunTime();
118 }
119
120
121
122
123
124
125
126
127
128 @Override
129 public int compareTo(JobEntry je)
130 {
131 return getJobId() - je.getJobId();
132 }
133
134
135
136
137
138
139 @Override
140 public void setActive(boolean isActive)
141 {
142 jobIsActive = isActive;
143 }
144
145
146
147
148
149
150
151 @Override
152 public boolean isActive()
153 {
154 return jobIsActive;
155 }
156
157
158
159
160
161
162 @Override
163 public long getNextRuntime()
164 {
165 return runtime;
166 }
167
168
169
170
171
172
173 @Override
174 public Date getNextRunDate()
175 {
176 return new Date(runtime);
177 }
178
179
180
181
182
183
184 @Override
185 public String getNextRunAsString()
186 {
187 return getNextRunDate().toString();
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 @Override
207 public void calcRunTime()
208 throws TurbineException
209 {
210 Calendar schedrun = Calendar.getInstance();
211 Calendar now = Calendar.getInstance();
212
213 switch (evaluateJobType())
214 {
215 case SECOND:
216
217 schedrun.add(Calendar.SECOND, getSecond());
218 runtime = schedrun.getTime().getTime();
219 break;
220
221 case MINUTE:
222
223 schedrun.add(Calendar.SECOND, getSecond());
224 schedrun.add(Calendar.MINUTE, getMinute());
225 runtime = schedrun.getTime().getTime();
226 break;
227
228 case WEEK_DAY:
229
230 schedrun.set(Calendar.SECOND, getSecond());
231 schedrun.set(Calendar.MINUTE, getMinute());
232 schedrun.set(Calendar.HOUR_OF_DAY, getHour());
233 schedrun.set(Calendar.DAY_OF_WEEK, getWeekDay());
234
235 if (now.before(schedrun))
236 {
237
238 runtime = schedrun.getTime().getTime();
239 }
240 else
241 {
242
243 schedrun.add(Calendar.DAY_OF_WEEK, 7);
244 runtime = schedrun.getTime().getTime();
245 }
246 break;
247
248 case DAY_OF_MONTH:
249
250 schedrun.set(Calendar.SECOND, getSecond());
251 schedrun.set(Calendar.MINUTE, getMinute());
252 schedrun.set(Calendar.HOUR_OF_DAY, getHour());
253 schedrun.set(Calendar.DAY_OF_MONTH, getDayOfMonth());
254
255 if (now.before(schedrun))
256 {
257
258 runtime = schedrun.getTime().getTime();
259 }
260 else
261 {
262
263 schedrun.add(Calendar.MONTH, 1);
264 runtime = schedrun.getTime().getTime();
265 }
266 break;
267
268 case DAILY:
269
270 schedrun.set(Calendar.SECOND, getSecond());
271 schedrun.set(Calendar.MINUTE, getMinute());
272 schedrun.set(Calendar.HOUR_OF_DAY, getHour());
273
274
275 if (now.before(schedrun))
276 {
277 runtime = schedrun.getTime().getTime();
278 }
279 else
280 {
281
282 schedrun.add(Calendar.HOUR_OF_DAY, 24);
283 runtime = schedrun.getTime().getTime();
284 }
285 break;
286
287 default:
288
289 }
290
291 log.info("Next runtime for task {} is {}", this::getTask, this::getNextRunDate);
292 }
293
294
295
296
297
298
299
300
301
302
303
304 private ScheduleType evaluateJobType()
305 throws TurbineException
306 {
307
308
309 if (getDayOfMonth() < 0)
310 {
311
312 if (getWeekDay() < 0)
313 {
314
315 if (getHour() < 0)
316 {
317
318 if (getMinute() < 0)
319 {
320
321 if (getSecond() < 0)
322 {
323 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
324 }
325
326 return ScheduleType.SECOND;
327 }
328 else
329 {
330
331
332 if (getMinute() < 0 || getSecond() < 0)
333 {
334 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
335 }
336
337 return ScheduleType.MINUTE;
338 }
339 }
340 else
341 {
342
343
344 if (getMinute() < 0 || getHour() < 0 || getSecond() < 0)
345 {
346 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
347 }
348
349 return ScheduleType.DAILY;
350 }
351 }
352 else
353 {
354
355
356 if (getMinute() < 0 || getHour() < 0 || getSecond() < 0)
357 {
358 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
359 }
360
361 return ScheduleType.WEEK_DAY;
362 }
363 }
364 else
365 {
366
367
368 if (getMinute() < 0 || getHour() < 0)
369 {
370 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
371 }
372
373 return ScheduleType.DAY_OF_MONTH;
374 }
375 }
376
377
378
379
380
381
382 @Override
383 public abstract int getJobId();
384
385
386
387
388
389
390 @Override
391 public abstract void setJobId(int v);
392
393
394
395
396
397
398 public abstract int getSecond();
399
400
401
402
403
404
405 public abstract void setSecond(int v);
406
407
408
409
410
411
412 public abstract int getMinute();
413
414
415
416
417
418
419 public abstract void setMinute(int v);
420
421
422
423
424
425
426 public abstract int getHour();
427
428
429
430
431
432
433 public abstract void setHour(int v);
434
435
436
437
438
439
440 public abstract int getWeekDay();
441
442
443
444
445
446
447 public abstract void setWeekDay(int v);
448
449
450
451
452
453
454 public abstract int getDayOfMonth();
455
456
457
458
459
460
461 public abstract void setDayOfMonth(int v);
462
463
464
465
466
467
468 @Override
469 public abstract String getTask();
470
471
472
473
474
475
476 @Override
477 public abstract void setTask(String v);
478 }