KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.interceptor;
6
7 import com.opensymphony.xwork.ActionContext;
8 import com.opensymphony.xwork.ActionInvocation;
9 import com.opensymphony.xwork.ActionProxy;
10 import com.opensymphony.xwork.config.entities.ResultConfig;
11 import com.opensymphony.xwork.interceptor.Interceptor;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import java.util.Collections JavaDoc;
16 import java.util.Map JavaDoc;
17
18
19 /**
20  * Executes an action in a background thread and then returns
21  * to the "wait" result. On the next request, if the action
22  * is done executing, the original result is sent back. This allows
23  * long running actions to execute while you present the user with a
24  * "Please, wait" message. The wait JSP should reload itself by using
25  * the following HTML in the HEAD area:
26  * <p/>
27  * <p/>
28  * <pre>
29  * &lt;meta http-equiv="refresh" content="5;url=&lt;ww:url includeParams="all" /&gt;"/&gt;
30  * </pre>
31  * <p/>
32  * <p/>
33  * This will cause the request to be reloaded every 5 seconds. <b>You will need
34  * to specify a result of "wait" in your action for this to work.</b>
35  *
36  * @author <a HREF="plightbo@gmail.com">Pat Lightbody</a>
37  */

38 public class ExecuteAndWaitInterceptor implements Interceptor {
39     private static final Log LOG = LogFactory.getLog(ExecuteAndWaitInterceptor.class);
40
41     public static final String JavaDoc KEY = "__execWait";
42
43     private int threadPriority = Thread.NORM_PRIORITY;
44
45     public void init() {
46     }
47
48     protected BackgroundProcess getNewBackgroundProcess(String JavaDoc name, ActionInvocation actionInvocation, int threadPriority) {
49         return new BackgroundProcess(name + "BackgroundThread", actionInvocation, threadPriority);
50     }
51
52     public String JavaDoc intercept(ActionInvocation actionInvocation) throws Exception JavaDoc {
53         ActionProxy proxy = actionInvocation.getProxy();
54         String JavaDoc name = proxy.getActionName();
55         ActionContext context = actionInvocation.getInvocationContext();
56         Map JavaDoc session = context.getSession();
57
58         synchronized (session) {
59             BackgroundProcess bp = (BackgroundProcess) session.get(KEY + name);
60
61             if (bp == null) {
62                 bp = getNewBackgroundProcess(name, actionInvocation, threadPriority);
63                 session.put(KEY + name, bp);
64             }
65
66             if (!bp.isDone()) {
67                 actionInvocation.getStack().push(bp.getAction());
68                 Map JavaDoc results = proxy.getConfig().getResults();
69                 if (!results.containsKey("wait")) {
70                     LOG.warn("ExecuteAndWait interceptor detected that no result named 'wait' is available. " +
71                             "Defaulting to a plain built-in wait page. It is highly recommend you provided " +
72                             "provide an action-specific or global result named 'wait'! This requires FreeMarker " +
73                             "support and won't work if you don't have it installed");
74                     // no wait result? hmm -- let's try to do dynamically put it in for you!
75
ResultConfig rc = new ResultConfig("wait", "com.opensymphony.webwork.views.freemarker.FreemarkerResult",
76                             Collections.singletonMap("location", "com/opensymphony/webwork/interceptor/wait.ftl"));
77                     results.put("wait", rc);
78                 }
79
80                 return "wait";
81             } else {
82                 session.remove(KEY + name);
83                 actionInvocation.getStack().push(bp.getAction());
84
85                 // if an exception occured during action execution, throw it here
86
if (bp.getException() != null) {
87                     throw bp.getException();
88                 }
89
90                 return bp.getResult();
91             }
92         }
93     }
94
95
96     public void destroy() {
97     }
98
99     public void setThreadPriority(int threadPriority) {
100         this.threadPriority = threadPriority;
101     }
102
103 }
104
Popular Tags