KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > threadpool > TaskInfoImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.threadpool;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import org.sape.carbon.core.exception.ExceptionUtility;
24
25 /**
26  * Besides supplying the functionality described by the TaskInfo interface,
27  * this implementation also is responsible for making callbacks using the
28  * TaskCallback interface if a callback has been specified.
29  *
30  * Copyright 2003 Sapient
31  * @since carbon 2.1
32  * @author Douglas Voet, Nov 20, 2003
33  * @version $Revision: 1.6 $($Author: dvoet $ / $Date: 2003/11/20 18:49:59 $)
34  */

35 public class TaskInfoImpl implements TaskInfo {
36     private Log log = LogFactory.getLog(this.getClass());
37     
38     private Runnable JavaDoc task;
39     private TaskStatusEnum status = TaskStatusEnum.PENDING;
40     private Throwable JavaDoc taskFailureCause = null;
41     private String JavaDoc taskName;
42     private String JavaDoc threadPoolName;
43     private TaskCallback callback;
44
45     protected TaskInfoImpl(
46         Runnable JavaDoc task,
47         String JavaDoc taskName,
48         TaskCallback callback,
49         String JavaDoc threadPoolName) {
50             
51         this.task = task;
52         this.taskName = taskName;
53         this.callback = callback;
54         this.threadPoolName = threadPoolName;
55         
56         if (log.isTraceEnabled()) {
57             log.trace(
58                 "Queuing task, thread pool [" +
59                     this.threadPoolName +
60                     "], task [" +
61                     this.taskName +
62                     "]");
63         }
64     }
65
66     public synchronized Throwable JavaDoc getFailureCause() {
67         return this.taskFailureCause;
68     }
69
70     public synchronized TaskStatusEnum getStatus() {
71         return this.status;
72     }
73
74     public String JavaDoc getTaskName() {
75         return this.taskName;
76     }
77     
78     public synchronized void waitUntilExecuted()
79         throws InterruptedException JavaDoc {
80
81         waitUntilExecuted(0);
82     }
83
84     public synchronized void waitUntilExecuted(long timeout)
85         throws InterruptedException JavaDoc {
86
87         if (this.status == TaskStatusEnum.PENDING ||
88             this.status == TaskStatusEnum.EXECUTING) {
89                 
90             this.wait(timeout);
91         }
92     }
93
94     /**
95      * This method is called when a task fails. The status is set to
96      * TaskStatusEnum.FAILED, all listeners are notified and the callback
97      * is called.
98      *
99      * @param cause
100      */

101     public void setFailure(Throwable JavaDoc cause) {
102         if (log.isInfoEnabled()) {
103             log.info(
104                 "Task failed, thread pool [" +
105                     this.threadPoolName +
106                     "], task [" +
107                     this.taskName +
108                     "], failure cause [" +
109                     ExceptionUtility.printStackTracesToString(cause) +
110                     "]");
111         }
112
113         synchronized (this) {
114             this.status = TaskStatusEnum.FAILED;
115             this.taskFailureCause = cause;
116             this.notifyAll();
117         }
118         
119         callCallback(false);
120     }
121
122     /**
123      * This method is called when a task succeeds. The status is set to
124      * TaskStatusEnum.SUCCESS, all listeners are notified and the callback
125      * is called.
126      */

127     public void setSuccess() {
128         if (log.isTraceEnabled()) {
129             log.trace(
130                 "Task executed successfully, thread pool [" +
131                     this.threadPoolName +
132                     "], task [" +
133                     this.taskName +
134                     "]");
135         }
136
137         synchronized (this) {
138             this.status = TaskStatusEnum.SUCCESS;
139             this.notifyAll();
140         }
141         
142         callCallback(true);
143     }
144
145     /**
146      * This method is called when a task begins execution. The status is set to
147      * TaskStatusEnum.EXECUTING.
148      */

149     public synchronized void setExecuting() {
150         if (log.isTraceEnabled()) {
151             log.trace(
152                 "Executing task, thread pool [" +
153                     this.threadPoolName +
154                     "], task [" +
155                     this.taskName +
156                     "]");
157         }
158
159         this.status = TaskStatusEnum.EXECUTING;
160     }
161
162     /** called by the DefaultThreadPoolImpl to get the task to execute */
163     public Runnable JavaDoc getTask() {
164         return this.task;
165     }
166     
167     public synchronized String JavaDoc toString() {
168         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
169         
170         buf.append("Task [");
171         buf.append(this.taskName == null ? "Unnamed Task" : this.taskName);
172         buf.append("], current state [");
173         buf.append(this.status);
174         buf.append("]");
175         
176         if (this.taskFailureCause != null) {
177             buf.append(", failure cause: ");
178             buf.append(ExceptionUtility.printStackTracesToString(
179                 this.taskFailureCause));
180         }
181         return buf.toString();
182     }
183     
184     /** helper method that calls the callback */
185     private void callCallback(boolean success) {
186         if (this.callback != null) {
187             
188             if (log.isTraceEnabled()) {
189                 log.trace(
190                     "Notifying callback, thread pool [" +
191                         this.threadPoolName +
192                         "], task [" +
193                         this.taskName +
194                         "]");
195             }
196             
197             try {
198                 if (success) {
199                     this.callback.taskSucceeded(this);
200                 } else {
201                     this.callback.taskFailed(this);
202                 }
203             } catch (RuntimeException JavaDoc re) {
204                 if (log.isErrorEnabled()) {
205                     log.error(
206                         "Caught RuntimeException in callback, thread pool [" +
207                             this.threadPoolName +
208                             "], task [" +
209                             this.taskName +
210                             "]",
211                         re);
212                 }
213             }
214         }
215     }
216
217 }
218
Popular Tags