KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > interceptor > BackgroundProcess


1 package com.opensymphony.webwork.interceptor;
2
3 import com.opensymphony.xwork.ActionInvocation;
4
5 import java.io.Serializable JavaDoc;
6
7 /**
8  * Background thread to be executed by the ExecuteAndWaitInterceptor.
9  *
10  * @author <a HREF="plightbo@gmail.com">Pat Lightbody</a>
11  * @author <a HREF="jim@jimvanfleet.com">Jim Van Fleet</a>
12  */

13 public class BackgroundProcess implements Serializable JavaDoc {
14     protected Object JavaDoc action;
15     protected ActionInvocation invocation;
16     protected String JavaDoc result;
17     protected Exception JavaDoc exception;
18     protected boolean done;
19
20     public BackgroundProcess(String JavaDoc threadName, final ActionInvocation invocation, int threadPriority) {
21         this.invocation = invocation;
22         this.action = invocation.getAction();
23         try {
24             final Thread JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc() {
25                 public void run() {
26                     try {
27                         beforeInvocation();
28                         result = invocation.invokeActionOnly();
29                         afterInvocation();
30                     } catch (Exception JavaDoc e) {
31                         exception = e;
32                     }
33
34                     done = true;
35                 }
36             });
37             t.setName(threadName);
38             t.setPriority(threadPriority);
39             t.start();
40         } catch (Exception JavaDoc e) {
41             exception = e;
42         }
43     }
44
45     /**
46      * called before the background thread determines the result code
47      * from the ActionInvocation.
48      *
49      * @throws Exception any exception thrown will be thrown, in turn, by the ExecuteAndWaitInterceptor
50      */

51     protected void beforeInvocation() throws Exception JavaDoc {
52     }
53
54     /**
55      * called after the background thread determines the result code
56      * from the ActionInvocation, but before the background thread is
57      * marked as done.
58      *
59      * @throws Exception any exception thrown will be thrown, in turn, by the ExecuteAndWaitInterceptor
60      */

61     protected void afterInvocation() throws Exception JavaDoc {
62     }
63
64     public Object JavaDoc getAction() {
65         return action;
66     }
67
68     public ActionInvocation getInvocation() {
69         return invocation;
70     }
71
72     public String JavaDoc getResult() {
73         return result;
74     }
75
76     public Exception JavaDoc getException() {
77         return exception;
78     }
79
80     public boolean isDone() {
81         return done;
82     }
83 }
84
Popular Tags