1 package org.opensync.engine.server;
2
3 import org.opensync.schedule.SchedulerEntry;
4 import org.opensync.schedule.Scheduler;
5
6 import java.util.*;
7
8 /***
9 * This object represents the task scheduler of the OpenSync application
10 */
11
12 public class TasksScheduler extends Scheduler{
13 /***
14 * Construct the <code>TaskScheduler</code> object
15 *
16 * @param httpPort the http port used to execute task by http request
17 */
18 public TasksScheduler(int httpPort) {
19 super(httpPort);
20 }
21 /***
22 * Schedule all tasks of all agenda
23 *
24 * @param agendas all agendas of the config file
25 * @param hot says if it is a hot start
26 */
27 public void schedule(Agendas agendas,boolean hot){
28 Enumeration elems = agendas.elements();
29 while(elems.hasMoreElements()){
30 Agenda agenda = (Agenda)elems.nextElement();
31 schedule(agenda,hot);
32 }
33 }
34 /***
35 * Schedule all tasks of the agenda
36 *
37 * @param agenda the agenda to schedules
38 * @param hot says if it is a hot start
39 */
40 public void schedule(Agenda agenda,boolean hot){
41 Iterator tasks = agenda.getTasks().iterator();
42 while(tasks.hasNext()){
43 Task task = (Task)tasks.next();
44 Iterator schedules = agenda.getSchedules().iterator();
45 while(schedules.hasNext()){
46 Schedule schedule = (Schedule)schedules.next();
47 schedule(task,schedule,hot);
48 }
49 }
50 }
51 /***
52 * Schedule a task
53 *
54 * @param task the task to schedule
55 * @param schedule the information for scheduling the task
56 * @param hot says if it is a hot start
57 */
58 public void schedule(Task task,Schedule schedule,boolean hot){
59 if(hot){
60 if(task.getWaittingSubTasks().isEmpty() && !task.getSubTasks().isEmpty()){
61 execute(task);
62 return;
63 }
64 }
65 else{
66 task.initDepends();
67 }
68 if(!task.getWaittingSubTasks().isEmpty()){
69 Iterator iterator = task.getSubTasks().iterator();
70 while(iterator.hasNext()){
71 Task subTask = (Task)iterator.next();
72 schedule(subTask,schedule,hot);
73 }
74 }
75 else{
76
77 int[]daysOfMonth = schedule.getDaysOfMonth();
78 int[]daysOfWeek = schedule.getDaysOfWeek();
79 if(schedule.isHttpRequest()){
80
81 SchedulerEntry schedulerEntry = new SchedulerEntry(
82 task.getName(),task
83 );
84 addSchedulerEntry(schedulerEntry);
85 }
86 else if(schedule.getDate() != null){
87
88 SchedulerEntry schedulerEntry = new SchedulerEntry(
89 task.getName(),schedule.getDateTime(),task
90 );
91 addSchedulerEntry(schedulerEntry);
92 }
93 else if(daysOfMonth != null && daysOfMonth.length > 0){
94
95 for(int d=0;d<daysOfMonth.length;d++){
96 int day = daysOfMonth[d];
97 SchedulerEntry schedulerEntry = new SchedulerEntry(
98 task.getName(),day,schedule.getHour(),schedule.getMinute(),task,false
99 );
100 addSchedulerEntry(schedulerEntry);
101 }
102 }
103 else if(daysOfWeek != null && daysOfWeek.length > 0){
104
105 for(int d=0;d<daysOfWeek.length;d++){
106 int minute = schedule.getMinute();
107 int hour = schedule.getHour();
108 int day = daysOfWeek[d];
109 for(int r=0;r<schedule.getRepeat() + 1;r++){
110 SchedulerEntry schedulerEntry = new SchedulerEntry(
111 task.getName(),day,hour,minute,task,true
112 );
113 addSchedulerEntry(schedulerEntry);
114 minute += schedule.getPeriod();
115 hour += (int)(minute / 60);
116 if(hour > 23){
117 OpenSync.getInstance().getLog().warn(
118 Log.SYNCHRONIZER,"warning.schedule.exceed",new Object[]{task.getName()}
119 );
120 break;
121 }
122 minute = minute % 60;
123 }
124 }
125 }
126 }
127 }
128 }