KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.taskman;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 /**
7  * Models the execution context of a <code>Task</code>.
8  *
9  * @author Yanick Duchesne 15-Apr-2003
10  * <dl>
11  * <dt><b>Copyright: </b>
12  * <dd>Copyright &#169; 2002-2004 <a
13  * HREF="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
14  * Rights Reserved.</dd>
15  * </dt>
16  * <dt><b>License: </b>
17  * <dd>Read the license.txt file of the jar or visit the <a
18  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the
19  * Sapia OSS web site</dd>
20  * </dt>
21  * </dl>
22  */

23 public class TaskContext {
24   private TaskOutput _out;
25   private TaskManager _mgr;
26   private Map JavaDoc _context = new HashMap JavaDoc();
27   private TaskmanTransaction _tx;
28
29   /**
30    * Constructor for TaskContext.
31    *
32    * @param out
33    * a <code>TaskOutput</code>
34    * @param mgr
35    * the <code>TaskManager</code> that created this instance.
36    */

37   protected TaskContext(TaskOutput out, TaskManager mgr) {
38     _out = out;
39     _mgr = mgr;
40   }
41
42   /**
43    * Returns a <code>TaskOutput</code>.
44    *
45    * @return a <code>TaskOutput</code>.
46    */

47   public TaskOutput getTaskOutput() {
48     return _out;
49   }
50
51   /**
52    * "Exports" the given value to this context - binding it to the given key.
53    *
54    * @param key
55    * the key under which to import the given value.
56    * @param value
57    * the <code>Object</code> to export - if an object is already
58    * bound to the given key, it is overwritten.
59    * @return this instance, to allow for chained invocations.
60    */

61   public TaskContext exportVal(Object JavaDoc key, Object JavaDoc value) {
62     _context.put(key, value);
63
64     return this;
65   }
66
67   /**
68    * "Imports" the value corresponding to the given key.
69    *
70    * @param key
71    * the key for which to retrieve the value.
72    * @return the <code>Object</code> that was bound to the given key, or
73    * <code>null</code> if no object could be found.
74    * @see #exportVal(Object, Object)
75    */

76   public Object JavaDoc importVal(Object JavaDoc key) {
77     return _context.get(key);
78   }
79
80   /**
81    * Triggers the synchronous execution of the passed-in task.
82    *
83    * @param name
84    * the name of the task to execute.
85    * @param t
86    * the <code>Task</code> to execute.
87    */

88   public void execSyncNestedTask(String JavaDoc name, Task t) {
89     _mgr.execSyncTask(name, t, this);
90     _out.setTaskName(name);
91   }
92
93   /**
94    * Triggers the asynchronous execution of the passed-in task.
95    *
96    * @param name
97    * the name of the task to execute.
98    * @param t
99    * the <code>Task</code> to execute.
100    * @param keepOutput
101    * if <code>true</code>, the given task will use the same
102    * <code>TaskOutput</code> as the task that "owns" this context -
103    * allowing applications to recieve task output from multiple nested
104    * tasks, asynchronously.
105    */

106   public void execAsyncNestedTask(String JavaDoc name, Task t) {
107     _mgr.execAsyncTask(name, t, this);
108   }
109
110   /**
111    * Triggers the synchronous execution of the task whose class is given.
112    *
113    * @param name
114    * the name of the task to execute.
115    * @param t
116    * the <code>Task</code> to execute.
117    * @param ctx
118    * the <code>TaskContext</code> that will be passed to the task in
119    * its execution.
120    * @throws IllegalAccessException
121    * if the current thread does not have access to the class' no-arg
122    * constructor, or if the class is not public.
123    * @throws InstantiationException
124    * if the task instance corresponding to the class could not be
125    * created.
126    */

127   public void execSyncNestedTask(String JavaDoc name, Class JavaDoc taskClass, TaskContext ctx)
128       throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
129     execSyncNestedTask(name, (Task) taskClass.newInstance());
130   }
131
132   /**
133    * Adds a task descriptor to this instance; the task encapsulated by the
134    * descriptor will be executed asynchronously.
135    *
136    * @param desc
137    * a <code>TaskDescripor</code>.
138    */

139   public void execTaskFor(TaskDescriptor desc) {
140     _mgr.execTaskFor(desc);
141   }
142
143   /**
144    * @return <code>true</code> if the task associated to this context takes
145    * part in a transaction.
146    */

147   public boolean isInTransaction() {
148     return _tx != null;
149   }
150
151   /**
152    * Sets this context's <code>TaskOutput</code>.
153    *
154    * @param a
155    * <code>TaskOutput</code>.
156    */

157   void setTaskOutput(TaskOutput out) {
158     _out = out;
159   }
160
161   /**
162    * Copies the values in the given map to this instance.
163    *
164    * @param vals
165    * a <code>Map</code>
166    */

167   void copyVals(Map JavaDoc vals) {
168     if(vals != null) {
169       _context.putAll(vals);
170     }
171   }
172
173   /**
174    * Returns the values of this context in a map.
175    *
176    * @return a <code>Map</code> containing this instance's values.
177    */

178   Map JavaDoc getVals() {
179     return _context;
180   }
181
182   void setTransaction(TaskmanTransaction tx) {
183     _tx = tx;
184   }
185
186   TaskmanTransaction getTransaction() {
187     return _tx;
188   }
189 }
190
Popular Tags