KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > taskman > TaskDescriptor


1 package org.sapia.taskman;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Map JavaDoc;
5
6 /**
7  * The base task descriptor class. An instance of this class wraps a
8  * <code>Task</code>, as well as metadata pertaining to the latter.
9  *
10  * @author Yanick Duchesne 15-Apr-2003
11  * <dl>
12  * <dt><b>Copyright: </b>
13  * <dd>Copyright &#169; 2002-2004 <a
14  * HREF="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
15  * Rights Reserved.</dd>
16  * </dt>
17  * <dt><b>License: </b>
18  * <dd>Read the license.txt file of the jar or visit the <a
19  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the
20  * Sapia OSS web site</dd>
21  * </dt>
22  * </dl>
23  */

24 public abstract class TaskDescriptor implements Serializable JavaDoc {
25
26   static final long serialVersionUID = 1L;
27
28   public static final int PERIODIC = 0;
29   public static final int TRANSIENT = 1;
30   private int _occurrence;
31   private long _intervalMillis;
32   private long _nextExecTime;
33   private boolean _executed;
34   private String JavaDoc _name;
35   private Task _task;
36   private transient TaskOutput _out;
37   private Map JavaDoc _contextVals;
38   private boolean _root;
39
40   /**
41    * Creates an instance of this class with the given parameters.
42    *
43    * @param name
44    * the name of the task to which this instance corresponds.
45    * @param occurrence
46    * the "occurrence" of the task's execution - corresponds to one of
47    * the public constants of this class.
48    * @param intervalMillis
49    * the interval at which the task will be executed.
50    * @param task
51    * the <code>Task</code> to associate to this descriptor.
52    */

53   public TaskDescriptor(String JavaDoc name, int occurrence, long intervalMillis,
54       Task task) {
55     _occurrence = occurrence;
56     _intervalMillis = intervalMillis;
57     _task = task;
58     _name = name;
59   }
60
61   String JavaDoc getName() {
62     return _name;
63   }
64
65   TaskDescriptor setRoot(boolean root) {
66     _root = root;
67
68     return this;
69   }
70
71   void setTaskOutput(TaskOutput out) {
72     _out = out;
73   }
74
75   TaskOutput getTaskOutput() {
76     return _out;
77   }
78
79   void setContextVals(Map JavaDoc vals) {
80     _contextVals = vals;
81   }
82
83   Map JavaDoc getContextVals() {
84     return _contextVals;
85   }
86
87   boolean isFinished() {
88     return (_executed && (_occurrence == TRANSIENT))
89         || (_task instanceof Abortable && ((Abortable) _task).isAborted());
90   }
91
92   long nextExecTime() {
93     return _nextExecTime;
94   }
95
96   boolean isPeriodic() {
97     return _occurrence == PERIODIC;
98   }
99
100   void calcNextExecTime() {
101     _nextExecTime = System.currentTimeMillis() + _intervalMillis;
102   }
103
104   final void exec(TaskContext ctx) {
105     if(_out != null) {
106       _out.setTaskName(_name);
107     }
108
109     if(_contextVals != null) {
110       ctx.copyVals(_contextVals);
111     }
112
113     _task.exec(ctx);
114     _executed = true;
115     calcNextExecTime();
116
117     if(_root) {
118       ctx.getTaskOutput().close();
119     }
120   }
121 }
122
Popular Tags