KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > ActionComponent


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 JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import java.io.Writer JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * ActionTag enables developers to call Actions directly from a JSP page by specifying the Action name and an optional
23  * namespace. The body content of the tag is used to render the results from the Action. Any Result processor defined
24  * for this Action in xwork.xml will be ignored.
25  *
26  * @author <a HREF="mailto:plightbo@gmail.com">Pat Lightbody</a>
27  */

28 public class ActionComponent extends Component {
29     private static final Log LOG = LogFactory.getLog(ActionComponent.class);
30
31     protected HttpServletResponse JavaDoc res;
32     protected HttpServletRequest JavaDoc req;
33
34     protected ActionProxy proxy;
35     protected String JavaDoc name;
36     protected String JavaDoc namespace;
37     protected boolean executeResult;
38     protected boolean ignoreContextParams;
39
40     public ActionComponent(OgnlValueStack stack, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
41         super(stack);
42         this.req = req;
43         this.res = res;
44     }
45
46     public void end(Writer JavaDoc 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 JavaDoc(parentParams) : new HashMap JavaDoc();
58
59         if (parameters != null) {
60             newParams.putAll(parameters);
61         }
62
63         ActionContext ctx = new ActionContext(stack.getContext());
64         ServletContext JavaDoc servletContext = (ServletContext JavaDoc) 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     /**
89      * Execute the requested action. If no namespace is provided, we'll
90      * attempt to derive a namespace using buildNamespace(). The ActionProxy
91      * and the namespace will be saved into the instance variables proxy and
92      * namespace respectively.
93      *
94      * @see com.opensymphony.webwork.views.jsp.TagUtils#buildNamespace
95      */

96     private void executeAction() {
97         String JavaDoc actualName = findString(name);
98
99         if (actualName == null) {
100             String JavaDoc message = "Unable to find value for name " + name;
101             LOG.error(message);
102             throw new RuntimeException JavaDoc(message);
103         }
104
105         String JavaDoc namespace;
106
107         if (this.namespace == null) {
108             namespace = TagUtils.buildNamespace(getStack(), req);
109         } else {
110             namespace = findString(this.namespace);
111         }
112
113         // get the old value stack from the request
114
OgnlValueStack stack = getStack();
115         // execute at this point, after params have been set
116
try {
117             proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actualName, createExtraContext(), executeResult);
118             // set the new stack into the request for the taglib to use
119
req.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());
120             proxy.execute();
121
122         } catch (Exception JavaDoc e) {
123             String JavaDoc message = "Could not execute action: " + namespace + "/" + actualName;
124             LOG.error(message, e);
125         } finally {
126             // set the old stack back on the request
127
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 JavaDoc name) {
137         this.name = name;
138     }
139
140     public void setNamespace(String JavaDoc 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