View Javadoc

1   package org.opensync.engine.server;
2   
3   import java.io.*;
4   import java.util.*;
5   
6   /***
7    * The task represents a synchronisation to do
8    */
9   
10  public class Task implements Serializable,Runnable, TaskListener {
11  
12    protected boolean finish = false;
13    protected String  status = "";
14  
15    public String getStatus() {
16      return status;
17    }
18  
19    public void setStatus(String status) {
20      this.status = status;
21    }
22  
23    public boolean getFinish() {
24      return finish;
25    }
26  
27    public void setFinish(boolean finish) {
28      this.finish = finish;
29    }
30  
31    /***
32     * The name of the task
33     *
34     */
35    protected String name;
36    /***
37     * The agenda of the task
38     *
39     */
40    protected Agenda agenda;
41    /***
42     * The synchronization to do
43     *
44     */
45    protected Synchronization synchronization;
46    /***
47     * The parent task
48     *
49     */
50    protected Task parentTask;
51    /***
52     * All the subtasks
53     *
54     *
55     */
56    protected ArrayList subTasks = new ArrayList();
57    /***
58     * The tasks not yet done
59     *
60     */
61    protected ArrayList waittingSubTasks = new ArrayList();
62    /***
63     * All tasks listeners
64     *
65     */
66    protected ArrayList tasksListener = new ArrayList();
67    /***
68     * The delay olf the task
69     *
70     */
71    protected long delay;
72    /***
73     * Construct a task object
74     *
75     */
76    public Task() {
77    }
78  
79    /***
80     * Read a task from a binary stream
81     *
82     * @param	ois the stream
83     * @exception	IOException
84     * @exception	ClassNotFoundException
85     */
86    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
87      ois.defaultReadObject();
88    }
89    /***
90     * Write a task in a binary stream
91     *
92     * @param	oos the stream
93     * @exception	IOException
94     */
95    private void writeObject(ObjectOutputStream oos) throws IOException {
96      synchronized(tasksListener){
97        Iterator iterator = tasksListener.iterator();
98        Collection old = new ArrayList();
99        while(iterator.hasNext()){
100         TaskListener listener = (TaskListener)iterator.next();
101         if(!(listener instanceof Task)){
102           tasksListener.remove(listener);
103           old.add(listener);
104           iterator = tasksListener.iterator();
105         }
106       }
107       oos.defaultWriteObject();
108       iterator = old.iterator();
109       while(iterator.hasNext()){
110         tasksListener.add(iterator.next());
111       }
112     }
113   }
114   /***
115    * Set the synchronization to do
116    *
117    * @param	synchronization the synchronization to do
118    */
119   public void setSynchronization(Synchronization synchronization) {
120     this.synchronization = synchronization;
121   }
122   /***
123    * Get the synchronization to do
124    *
125    */
126   public Synchronization getSynchronization() {
127     return synchronization;
128   }
129   /***
130    * Get the sub tasks
131    *
132    */
133   public Collection getSubTasks() {
134     return subTasks;
135   }
136   /***
137    * Add a sub task
138    *
139    * @param	task
140    */
141   public void addSubTask(Task task) {
142     task.setAgenda(agenda);
143     subTasks.add(task);
144     task.setParentTask(this);
145     task.addTaskListener(this);
146   }
147   /***
148    * Regsiter a task listener
149    *
150    * @param	taskListener the task listener to register
151    */
152   public void addTaskListener(TaskListener taskListener) {
153     synchronized(tasksListener){
154       tasksListener.add(taskListener);
155     }
156   }
157   /***
158    * Remove a task listener
159    *
160    * @param	taskListener the task listener to remov
161    */
162   public void removeTaskListener(TaskListener taskListener) {
163     synchronized(tasksListener){
164       tasksListener.remove(taskListener);
165     }
166   }
167   /***
168    * Set the parent taks
169    *
170    * @param	parentTask
171    */
172   public void setParentTask(Task parentTask) {
173     this.parentTask = parentTask;
174   }
175   /***
176    * Get the parent task
177    *
178    */
179   public Task getParentTask() {
180     return parentTask;
181   }
182   /***
183    * Set the name of the task
184    *
185    * @param	name
186    */
187   public void setName(String name) {
188     this.name = name;
189   }
190   /***
191    * Get the name of the task
192    *
193    */
194   public String getName() {
195     return name;
196   }
197   /***
198    * Set the agenda
199    *
200    * @param	agenda
201    */
202   public void setAgenda(Agenda agenda) {
203     this.agenda = agenda;
204   }
205   /***
206    * Get the agenda
207    *
208    */
209   public Agenda getAgenda() {
210     return agenda;
211   }
212 
213   /***
214    * initialize the tak dependance
215    *
216    */
217 
218   public void initDepends(){
219     synchronized(waittingSubTasks){
220       synchronized(subTasks){
221         waittingSubTasks = new ArrayList(subTasks);
222       }
223     }
224   }
225   /***
226    * Run the task
227    *
228    */
229   public void run() {
230     try{
231       start();
232       Synchronizer.executeTask(this);
233       stop();
234     }
235     catch(Exception e){
236       fail(e);
237     }
238   }
239   /***
240    * Start the taks
241    *
242    */
243   protected void start(){
244     TaskEvent taskEvent = new TaskEvent(this,GregorianCalendar.getInstance().getTime());
245     ArrayList listeners;
246     synchronized(tasksListener){
247       listeners = (ArrayList)tasksListener.clone();
248     }
249     Iterator iterator = listeners.iterator();
250     while(iterator.hasNext()){
251       TaskListener listener = (TaskListener)iterator.next();
252       listener.taskStart(taskEvent);
253     }
254     delay = System.currentTimeMillis();
255     synchronization.setLastStatus(Synchronization.STATUS_RUNNING);
256   }
257   /***
258    * Stop the task
259    *
260    */
261   protected void stop(){
262     TaskEvent taskEvent = new TaskEvent(
263       this,GregorianCalendar.getInstance().getTime(),
264       (System.currentTimeMillis() - delay) / 1000
265     );
266     ArrayList listeners;
267     synchronized(tasksListener){
268       listeners = (ArrayList)tasksListener.clone();
269     }
270     Iterator iterator = listeners.iterator();
271     while(iterator.hasNext()){
272       TaskListener listener = (TaskListener)iterator.next();
273       listener.taskStop(taskEvent);
274     }
275     //re-initialization of the dependance
276     initDepends();
277     synchronization.setLastDone(GregorianCalendar.getInstance().getTime());
278     synchronization.setLastStatus(Synchronization.STATUS_SUCCESS);
279     OpenSync.getInstance().saveState();
280   }
281   /***
282    * The task failed
283    *
284    * @param	exception
285    */
286   public void fail(Exception exception){
287     TaskEvent taskEvent = new TaskEvent(
288       this,GregorianCalendar.getInstance().getTime(),exception
289     );
290     ArrayList listeners;
291     synchronized(tasksListener){
292       listeners = (ArrayList)tasksListener.clone();
293     }
294     Iterator iterator = listeners.iterator();
295     while(iterator.hasNext()){
296       TaskListener listener = (TaskListener)iterator.next();
297       listener.taskFail(taskEvent);
298     }
299     //re-initialization of dependance
300     initDepends();
301     OpenSync.getInstance().saveState();
302     synchronization.setLastDone(GregorianCalendar.getInstance().getTime());
303     synchronization.setLastStatus(Synchronization.STATUS_FAIL);
304   }
305   //TaskListern implements
306   /***
307    * Implements the TaskListenr interface
308    *
309    * @param	event the task event
310    */
311   public void taskStart(TaskEvent event) {
312   }
313   /***
314    * Implements the TaskListenr interface
315    *
316    * @param	event the task event
317    */
318   public void taskStop(TaskEvent event) {
319     synchronized(waittingSubTasks){
320       waittingSubTasks.remove(event.getTask());
321       if(waittingSubTasks.isEmpty()){
322         new Thread(this).start();
323       }
324     }
325   }
326   /***
327    * Implements the TaskListenr interface
328    *
329    * @param	event the task event
330    */
331   public void taskFail(TaskEvent event){
332   }
333   /***
334    * Use for debug only
335    *
336    */
337   public String toString(){
338     StringBuffer buffer = new StringBuffer();
339     buffer.append("Task{\n");
340     buffer.append("\tname : ").append(name).append("\n");
341     buffer.append("\tsynchronization : " + synchronization.getName()).append("\n");
342     if(parentTask != null){
343       buffer.append("\tparentTask : ").append(parentTask.getName()).append("\n");
344     }
345     if(tasksListener != null && tasksListener.size() > 0){
346       buffer.append("\tTask listeners : ");
347       Iterator iterator = tasksListener.iterator();
348       while(iterator.hasNext()){
349         Object obj = iterator.next();
350         if(obj instanceof Task){
351           buffer.append(((Task)obj).getName()).append(";");
352         }
353       }
354       buffer.append("\n");
355     }
356     buffer.append("\tWaitting subTask : ");
357     Iterator iterator;
358     iterator = waittingSubTasks.iterator();
359     while(iterator.hasNext()){
360       buffer.append(((Task)iterator.next()).getName());
361     }
362     buffer.append("\n");
363     if(subTasks != null && subTasks.size() > 0){
364       buffer.append("\tsubTasks :\n");
365       iterator = subTasks.iterator();
366       while(iterator.hasNext()){
367         buffer.append(iterator.next().toString());
368       }
369     }
370     buffer.append("}\n");
371     return buffer.toString();
372   }
373   /***
374    * Get all the not yet done
375    *
376    */
377   public ArrayList getWaittingSubTasks() {
378     return waittingSubTasks;
379   }
380 }