KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > AbstractWorkerThread


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot;
10
11 import java.security.AccessController JavaDoc;
12 import java.security.PrivilegedActionException JavaDoc;
13 import java.security.PrivilegedExceptionAction JavaDoc;
14 import java.io.InterruptedIOException JavaDoc;
15
16 /**
17  * Partial implementation of the WorkerThread interface.
18  * @version $Revision: 1.9 $
19  */

20 public abstract class AbstractWorkerThread implements WorkerThread
21 {
22    /**
23     * Creates a new instance of this AbstractWorkerThread, called by subclasses.
24     */

25    protected AbstractWorkerThread()
26    {
27    }
28
29    public void runTask(final Task task)
30    {
31       if (AbstractWorker.debug) System.out.println("[AbstractWorkerThread] Executing task " + task);
32
33       try
34       {
35          Object JavaDoc obj = AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc()
36          {
37             public Object JavaDoc run() throws Exception JavaDoc
38             {
39                return task.run();
40             }
41          }, task.getSecurityContext());
42
43          task.setResult(obj);
44       }
45       catch (PrivilegedActionException JavaDoc x)
46       {
47          Exception JavaDoc xx = x.getException();
48          task.setThrowable(xx);
49          if (xx instanceof InterruptedException JavaDoc || xx instanceof InterruptedIOException JavaDoc) Thread.currentThread().interrupt();
50       }
51       catch (Throwable JavaDoc x)
52       {
53          task.setThrowable(x);
54       }
55       finally
56       {
57          // Mark the task as completed
58
task.setCompleted(true);
59
60          if (AbstractWorker.debug) System.out.println("[AbstractWorkerThread] Completing run for task " + task);
61          task.postRun();
62       }
63    }
64 }
65
Popular Tags