KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > embedded > resource > WorkWrapper


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 package org.jboss.ejb3.embedded.resource;
23
24 import javax.resource.spi.work.ExecutionContext JavaDoc;
25 import javax.resource.spi.work.Work JavaDoc;
26 import javax.resource.spi.work.WorkCompletedException JavaDoc;
27 import javax.resource.spi.work.WorkEvent JavaDoc;
28 import javax.resource.spi.work.WorkException JavaDoc;
29 import javax.resource.spi.work.WorkListener JavaDoc;
30 import javax.resource.spi.work.WorkManager JavaDoc;
31 import javax.resource.spi.work.WorkRejectedException JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.util.JBossStringBuilder;
35 import org.jboss.util.NestedRuntimeException;
36 import org.jboss.util.threadpool.BasicTaskWrapper;
37 import org.jboss.util.threadpool.StartTimeoutException;
38 import org.jboss.util.threadpool.Task;
39
40 /**
41  * Wraps the resource adapter's work.
42  *
43  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
44  * @version $Revision: 45712 $
45  */

46 public class WorkWrapper extends BasicTaskWrapper implements Task
47 {
48    /** The log */
49    private static final Logger log = Logger.getLogger(WorkWrapper.class);
50
51    /** Whether we are tracing */
52    private boolean trace = log.isTraceEnabled();
53    
54    /** The work */
55    private Work JavaDoc work;
56
57    /** The execution context */
58    private ExecutionContext JavaDoc executionContext;
59
60    /** the work listener */
61    private WorkListener JavaDoc workListener;
62
63    /** The start timeout */
64    private long startTimeout;
65
66    /** The work manager */
67    private JBossWorkManager workManager;
68
69    /** The wait type */
70    private int waitType;
71
72    /** The blocked time */
73    private long blockedTime;
74
75    /** Any exception */
76    private WorkException JavaDoc exception;
77
78    /**
79     * Create a new WorkWrapper
80     *
81     * @param workManager the work manager
82     * @param work the work
83     * @param waitType the waitType
84     * @param executionContext the execution context
85     * @param workListener the WorkListener
86     * @throws IllegalArgumentException for null work, execution context or a negative start timeout
87     */

88    public WorkWrapper(JBossWorkManager workManager, Work JavaDoc work, int waitType, long startTimeout, ExecutionContext JavaDoc executionContext, WorkListener JavaDoc workListener)
89    {
90       super();
91
92       if (work == null)
93          throw new IllegalArgumentException JavaDoc("Null work");
94       if (executionContext == null)
95          throw new IllegalArgumentException JavaDoc("Null execution context");
96       if (startTimeout < 0)
97          throw new IllegalArgumentException JavaDoc("Illegal start timeout: " + startTimeout);
98
99       this.workManager = workManager;
100       this.work = work;
101       this.waitType = waitType;
102       this.startTimeout = startTimeout;
103       this.executionContext = executionContext;
104       this.workListener = workListener;
105
106       setTask(this);
107    }
108
109    /**
110     * Get the work manager
111     *
112     * @return the work manager
113     */

114    public JBossWorkManager getWorkManager()
115    {
116       return workManager;
117    }
118
119    /**
120     * Retrieve the work
121     *
122     * @return the work
123     */

124    public Work JavaDoc getWork()
125    {
126       return work;
127    }
128
129    /**
130     * Retrieve the work listener
131     *
132     * @return the WorkListener
133     */

134    public WorkListener JavaDoc getWorkListener()
135    {
136       return workListener;
137    }
138
139    /**
140     * Retrieve the exection context
141     *
142     * @return the execution context
143     */

144    public ExecutionContext JavaDoc getExecutionContext()
145    {
146       return executionContext;
147    }
148
149    /**
150     * Retrieve the time blocked
151     *
152     * @return the blocked time
153     */

154    public long getBlockedElapsed()
155    {
156       return blockedTime;
157    }
158    
159    /**
160     * Get any exception
161     *
162     * @return the exception or null if there is none
163     */

164    public WorkException JavaDoc getWorkException()
165    {
166       return exception;
167    }
168
169    public int getWaitType()
170    {
171       return waitType;
172    }
173
174    public int getPriority()
175    {
176       return Thread.NORM_PRIORITY;
177    }
178
179    public long getStartTimeout()
180    {
181       return startTimeout;
182    }
183
184    public long getCompletionTimeout()
185    {
186       return executionContext.getTransactionTimeout();
187    }
188
189    public void execute()
190    {
191       if (trace)
192          log.trace("Executing work " + this);
193       try
194       {
195          workManager.startWork(this);
196       }
197       catch (WorkException JavaDoc e)
198       {
199          e.printStackTrace();
200          taskRejected(new NestedRuntimeException(e));
201          return;
202       }
203       try
204       {
205          work.run();
206       }
207       finally
208       {
209          workManager.endWork(this);
210       }
211       if (trace)
212          log.trace("Executed work " + this);
213    }
214
215    public void stop()
216    {
217       if (trace)
218          log.trace("Stopping work " + this);
219
220       work.release();
221    }
222
223    public void accepted(long time)
224    {
225       blockedTime = time;
226
227       if (trace)
228          log.trace("Accepted work " + this);
229
230       if (workListener != null)
231       {
232          WorkEvent JavaDoc event = new WorkEvent JavaDoc(workManager, WorkEvent.WORK_ACCEPTED, work, null);
233          workListener.workAccepted(event);
234       }
235    }
236
237    public void rejected(long time, Throwable JavaDoc throwable)
238    {
239       blockedTime = time;
240
241       if (trace)
242       {
243          if (throwable != null)
244             log.trace("Rejecting work " + this, throwable);
245          else
246             log.trace("Rejecting work " + this);
247       }
248
249       if (throwable != null)
250       {
251          exception = new WorkRejectedException JavaDoc(throwable);
252          if (throwable instanceof StartTimeoutException)
253             exception.setErrorCode(WorkRejectedException.START_TIMED_OUT);
254       }
255       
256       workManager.cancelWork(this);
257       
258       if (workListener != null)
259       {
260          WorkEvent JavaDoc event = new WorkEvent JavaDoc(workManager, WorkEvent.WORK_ACCEPTED, work, exception);
261          workListener.workRejected(event);
262       }
263    }
264
265    public void started(long time)
266    {
267       if (waitType != WAIT_NONE)
268          blockedTime = time;
269
270       if (workListener != null)
271       {
272          WorkEvent JavaDoc event = new WorkEvent JavaDoc(workManager, WorkEvent.WORK_STARTED, work, null);
273          workListener.workStarted(event);
274       }
275    }
276
277    public void completed(long time, Throwable JavaDoc throwable)
278    {
279       if (waitType == WAIT_FOR_COMPLETE)
280          blockedTime = time;
281
282       if (throwable != null)
283          exception = new WorkCompletedException JavaDoc(throwable);
284
285       if (trace)
286          log.trace("Completed work " + this);
287
288       if (workListener != null)
289       {
290          WorkEvent JavaDoc event = new WorkEvent JavaDoc(workManager, WorkEvent.WORK_COMPLETED, work, exception);
291          workListener.workCompleted(event);
292       }
293    }
294    
295    public String JavaDoc toString()
296    {
297       JBossStringBuilder buffer = new JBossStringBuilder(100);
298       buffer.append("WorkWrapper@").append(Integer.toHexString(System.identityHashCode(this)));
299       buffer.append("[workManger=").append(workManager);
300       buffer.append(" work=").append(work);
301       buffer.append(" state=").append(getStateString());
302       if (executionContext != null && executionContext.getXid() != null)
303       {
304          buffer.append(" xid=").append(executionContext.getXid());
305          buffer.append(" txTimeout=").append(executionContext.getTransactionTimeout());
306       }
307       buffer.append(" waitType=");
308       switch (waitType)
309       {
310          case WAIT_NONE:
311          {
312             buffer.append("WAIT_NONE");
313             break;
314          }
315          case WAIT_FOR_START:
316          {
317             buffer.append("WAIT_FOR_START");
318             break;
319          }
320          case WAIT_FOR_COMPLETE:
321          {
322             buffer.append("WAIT_FOR_COMPLETE");
323             break;
324          }
325          default:
326             buffer.append("???");
327       }
328       if (startTimeout != WorkManager.INDEFINITE)
329          buffer.append(" startTimeout=").append(startTimeout);
330       long completionTimeout = getCompletionTimeout();
331       if (completionTimeout != -1)
332          buffer.append(" completionTimeout=").append(completionTimeout);
333       if (blockedTime != 0)
334          buffer.append(" blockTime=").append(blockedTime);
335       buffer.append(" elapsedTime=").append(getElapsedTime());
336       if (workListener != null)
337          buffer.append(" workListener=").append(workListener);
338       if (exception != null)
339          buffer.append(" exception=").append(exception);
340       buffer.append("]");
341       return buffer.toString();
342    }
343 }
344
Popular Tags