1 package com.opensymphony.webwork.components; 2 3 import com.opensymphony.webwork.ServletActionContext; 4 import com.opensymphony.webwork.dispatcher.DispatcherUtils; 5 import com.opensymphony.webwork.dispatcher.RequestMap; 6 import com.opensymphony.webwork.views.jsp.TagUtils; 7 import com.opensymphony.xwork.ActionContext; 8 import com.opensymphony.xwork.ActionProxy; 9 import com.opensymphony.xwork.ActionProxyFactory; 10 import com.opensymphony.xwork.util.OgnlValueStack; 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import javax.servlet.ServletContext ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 import java.io.Writer ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 28 public class ActionComponent extends Component { 29 private static final Log LOG = LogFactory.getLog(ActionComponent.class); 30 31 protected HttpServletResponse res; 32 protected HttpServletRequest req; 33 34 protected ActionProxy proxy; 35 protected String name; 36 protected String namespace; 37 protected boolean executeResult; 38 protected boolean ignoreContextParams; 39 40 public ActionComponent(OgnlValueStack stack, HttpServletRequest req, HttpServletResponse res) { 41 super(stack); 42 this.req = req; 43 this.res = res; 44 } 45 46 public void end(Writer writer) { 47 executeAction(); 48 } 49 50 private Map createExtraContext() { 51 Map parentParams = null; 52 53 if (!ignoreContextParams) { 54 parentParams = new ActionContext(getStack().getContext()).getParameters(); 55 } 56 57 Map newParams = (parentParams != null) ? new HashMap (parentParams) : new HashMap (); 58 59 if (parameters != null) { 60 newParams.putAll(parameters); 61 } 62 63 ActionContext ctx = new ActionContext(stack.getContext()); 64 ServletContext servletContext = (ServletContext ) ctx.get(ServletActionContext.SERVLET_CONTEXT); 65 Map session = ctx.getSession(); 66 Map application = ctx.getApplication(); 67 68 DispatcherUtils.initialize(servletContext); 69 DispatcherUtils du = DispatcherUtils.getInstance(); 70 Map extraContext = du.createContextMap(new RequestMap(req), 71 newParams, 72 session, 73 application, 74 req, 75 res, 76 servletContext); 77 78 OgnlValueStack newStack = new OgnlValueStack(stack); 79 extraContext.put(ActionContext.VALUE_STACK, newStack); 80 81 return extraContext; 82 } 83 84 public ActionProxy getProxy() { 85 return proxy; 86 } 87 88 96 private void executeAction() { 97 String actualName = findString(name); 98 99 if (actualName == null) { 100 String message = "Unable to find value for name " + name; 101 LOG.error(message); 102 throw new RuntimeException (message); 103 } 104 105 String namespace; 106 107 if (this.namespace == null) { 108 namespace = TagUtils.buildNamespace(getStack(), req); 109 } else { 110 namespace = findString(this.namespace); 111 } 112 113 OgnlValueStack stack = getStack(); 115 try { 117 proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actualName, createExtraContext(), executeResult); 118 req.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack()); 120 proxy.execute(); 121 122 } catch (Exception e) { 123 String message = "Could not execute action: " + namespace + "/" + actualName; 124 LOG.error(message, e); 125 } finally { 126 req.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack); 128 } 129 130 if (getId() != null) { 131 final Map context = stack.getContext(); 132 context.put(getId(), proxy.getAction()); 133 } 134 } 135 136 public void setName(String name) { 137 this.name = name; 138 } 139 140 public void setNamespace(String namespace) { 141 this.namespace = namespace; 142 } 143 144 public void setExecuteResult(boolean executeResult) { 145 this.executeResult = executeResult; 146 } 147 148 public void setIgnoreContextParams(boolean ignoreContextParams) { 149 this.ignoreContextParams = ignoreContextParams; 150 } 151 } 152 | Popular Tags |