KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > asynchronous > concurrent > AsynchronousTaskImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.aspects.asynchronous.concurrent;
24
25 import EDU.oswego.cs.dl.util.concurrent.Callable;
26 import EDU.oswego.cs.dl.util.concurrent.FutureResult;
27 import EDU.oswego.cs.dl.util.concurrent.TimeoutException;
28 import org.jboss.aspects.asynchronous.AsynchronousConstants;
29 import org.jboss.aspects.asynchronous.AsynchronousParameters;
30 import org.jboss.aspects.asynchronous.AsynchronousUserTask;
31 import org.jboss.aspects.asynchronous.ProcessingTime;
32 import org.jboss.aspects.asynchronous.ThreadManagerResponse;
33 import org.jboss.aspects.asynchronous.common.ThreadManagerResponseImpl;
34
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36
37 /**
38  * @author <a HREF="mailto:chussenet@yahoo.com">{Claude Hussenet Independent Consultant}</a>.
39  * @version <tt>$Revision: 37406 $</tt>
40  */

41
42 public final class AsynchronousTaskImpl
43
44 implements AsynchronousConstants, org.jboss.aspects.asynchronous.concurrent.AsynchronousTask
45 {
46
47    private long _timeout = 0;
48
49    private AsynchronousParameters _inputParametersImpl = null;
50
51    private AsynchronousUserTask _oneInstance = null;
52
53    private FutureResult _futureResult = null;
54
55    private Callable _callable = null;
56
57    private String JavaDoc _id = null;
58
59    AsynchronousTaskImpl(String JavaDoc id,
60
61                         String JavaDoc taskImpl,
62
63                         AsynchronousParameters inputParametersImpl,
64
65                         long timeout)
66    {
67
68       this._id = id;
69
70       _futureResult = new FutureResult();
71
72       _timeout = timeout;
73
74       _inputParametersImpl = inputParametersImpl;
75
76       try
77       {
78
79          Class JavaDoc aClass = Class.forName(taskImpl);
80
81          _oneInstance = (AsynchronousUserTask) aClass.newInstance();
82
83       }
84       catch (Exception JavaDoc e)
85       {
86          e.printStackTrace();
87
88       }
89
90    }
91
92    AsynchronousTaskImpl(String JavaDoc id,
93
94                         AsynchronousUserTask userTask,
95
96                         AsynchronousParameters inputParametersImpl,
97
98                         long timeout)
99    {
100
101       this._id = id;
102
103       _futureResult = new FutureResult();
104
105       _timeout = timeout;
106
107       _oneInstance = userTask;
108
109       _inputParametersImpl = inputParametersImpl;
110
111    }
112
113    public AsynchronousParameters getInputParameters()
114    {
115
116       return _inputParametersImpl;
117
118    }
119
120    public long getTimeout()
121    {
122
123       return _timeout;
124
125    }
126
127    public AsynchronousUserTask getTask()
128    {
129
130       return _oneInstance;
131
132    }
133
134    public ThreadManagerResponse getResponse()
135    {
136
137       try
138       {
139
140          if (isDone())
141
142             return (ThreadManagerResponse) _futureResult.get();
143
144          else
145          {
146
147             Object JavaDoc value = _futureResult.get();
148
149             if (value != null)
150
151                return (ThreadManagerResponse) value;
152
153             else
154
155                return new ThreadManagerResponseImpl(getId(),
156
157                NOVALUE,
158
159                null,
160
161                null);
162
163          }
164
165       }
166       catch (TimeoutException e)
167       {
168
169          return new ThreadManagerResponseImpl(getId(),
170
171          TIMEOUT,
172
173          e.getMessage(),
174
175          e,
176
177          getStartingTime(),
178
179          getEndingTime());
180
181       }
182       catch (InterruptedException JavaDoc e)
183       {
184
185          return new ThreadManagerResponseImpl(getId(),
186
187          INTERRUPTED,
188
189          e.getMessage(),
190
191          e,
192
193          getStartingTime(),
194
195          getEndingTime());
196
197       }
198       catch (InvocationTargetException JavaDoc e)
199       {
200
201          int errorCode = INVOCATION;
202
203          if (e.getTargetException()
204
205          instanceof EDU.oswego.cs.dl.util.concurrent.TimeoutException)
206
207             errorCode = TIMEOUT;
208
209          return new ThreadManagerResponseImpl(getId(),
210
211          errorCode,
212
213          e.getTargetException().getMessage(),
214
215          e.getTargetException(),
216
217          getStartingTime(),
218
219          getEndingTime());
220
221       }
222
223    }
224
225    public long getStartingTime()
226    {
227
228       return ((ProcessingTime) _callable).getStartingTime();
229
230    }
231
232    public long getEndingTime()
233    {
234
235       return ((ProcessingTime) _callable).getEndingTime();
236
237    }
238
239
240    public boolean isDone()
241    {
242
243       return _futureResult.isReady();
244
245    }
246
247    public Runnable JavaDoc add() throws Exception JavaDoc
248    {
249
250       try
251       {
252
253          if (_timeout == 0)
254
255             _callable =
256
257             new AdapterTask(getId(),
258
259             _inputParametersImpl,
260
261             _oneInstance);
262
263          else
264
265             _callable =
266
267             new TimedCallableImpl(new AdapterTask(getId(),
268
269             _inputParametersImpl,
270
271             _oneInstance),
272
273             _timeout);
274
275          return _futureResult.setter(_callable);
276
277
278       }
279       catch (Exception JavaDoc e)
280       {
281
282
283          throw e;
284
285       }
286
287    }
288
289    public String JavaDoc getId()
290    {
291
292       return _id;
293
294    }
295
296 }
297
298
Popular Tags