1 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 ; 16 import java.util.Map ; 17 18 19 38 public class ExecuteAndWaitInterceptor implements Interceptor { 39 private static final Log LOG = LogFactory.getLog(ExecuteAndWaitInterceptor.class); 40 41 public static final String KEY = "__execWait"; 42 43 private int threadPriority = Thread.NORM_PRIORITY; 44 45 public void init() { 46 } 47 48 protected BackgroundProcess getNewBackgroundProcess(String name, ActionInvocation actionInvocation, int threadPriority) { 49 return new BackgroundProcess(name + "BackgroundThread", actionInvocation, threadPriority); 50 } 51 52 public String intercept(ActionInvocation actionInvocation) throws Exception { 53 ActionProxy proxy = actionInvocation.getProxy(); 54 String name = proxy.getActionName(); 55 ActionContext context = actionInvocation.getInvocationContext(); 56 Map 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 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 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 (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 |