KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > legacy > DispatchAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.chain.legacy;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.apache.commons.chain.Catalog;
23 import org.apache.commons.chain.CatalogFactory;
24 import org.apache.commons.chain.Command;
25 import org.apache.commons.chain.Context;
26 import org.apache.commons.chain.web.servlet.ServletWebContext;
27
28 import org.apache.struts.action.Action;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 /**
34  * <p>An <code>Action</code> implementation that delegates to a
35  * Chain (or Command) that is specified by a request parameter
36  * whose name is configured in the <code>parameter</code> attribute
37  * of the <code>&lt;action&gt;</code> element configuring this action.
38  * For example:</p>
39  *
40  * <pre>
41  * &lt;action path="/myaction"
42  * type="org.apache.struts.chain.legacy.DispatchAction"
43  * name="myform"
44  * scope="request"
45  * input="/mypage.jsp"
46  * parameter="name-of-request-parameter"
47  * </pre>
48  *
49  * <p>Prior to calling the specified chain (or command), this action
50  * sets up a <code>Context</code> object containing the relevant
51  * properties, along with the following additional attributes:</p>
52  * <ul>
53  * <li><strong>mapping</strong> - The <code>ActionMapping</code> passed
54  * to our <code>execute()</code> method</li>
55  * <li><strong>form</strong> - The <code>ActionForm</code> passed to
56  * our <code>execute()</code> method</li>
57  * </ul>
58  *
59  * <p>After execution of the specified command or chain is completed,
60  * the following context attributes are examined (in this order) to
61  * determine how to proceed.</p>
62  * <ul>
63  * <li><strong>exception</strong> - If a <code>java.lang.Exception</code>
64  * is found here, it will be rethrown as the outcome of this action.</li>
65  * <li><strong>forward</strong> - If an
66  * <code>org.apache.struts.action.ActionForward</code> is found here,
67  * it will be returned as the outcome of this action. Otherwise,
68  * <code>null</code> will be returned.</li>
69  * </ul>
70  */

71
72 public class DispatchAction extends Action {
73
74
75     // ------------------------------------------------------- Instance Varibles
76

77
78     /**
79      * <p>The <code>Catalog</code> that will be consulted to look up
80      * the <code>Command</code> to be executed.</p>
81      */

82     private Catalog catalog = null;
83
84
85     // ---------------------------------------------------------- Public Methods
86

87
88     /**
89      * <p>Delegate to the command chain specified in our configuration.</p>
90      *
91      * @param mapping <code>ActionMapping</code> configuring this action
92      * @param form <code>ActionForm</code> for this request (if any)
93      * @param request <code>HttpServletRequest</code> we are processing
94      * @param response <code>HttpServletResponse</code> we are creating
95      */

96     public ActionForward execute(ActionMapping mapping,
97                                  ActionForm form,
98                                  HttpServletRequest JavaDoc request,
99                                  HttpServletResponse JavaDoc response)
100         throws Exception JavaDoc {
101
102         // Set up a context for this request
103
Context context = new ServletWebContext
104             (getServlet().getServletContext(), request, response);
105         context.put("mapping", mapping);
106         context.put("form", form);
107
108         // Delegate to the specified command
109
String JavaDoc name = mapping.getParameter();
110         Command command = getCatalog().getCommand(request.getParameter(name));
111         command.execute(context); // Ignore return state
112

113         // Return results as appropriate
114
Exception JavaDoc exception = null;
115         try {
116             exception = (Exception JavaDoc) context.get("exception");
117             if (exception != null) {
118                 throw exception;
119             }
120         } catch (ClassCastException JavaDoc e) {
121             ; // It is not an Exception
122
}
123         ActionForward forward = null;
124         try {
125             forward = (ActionForward) context.get("forward");
126         } catch (ClassCastException JavaDoc e) {
127             forward = null; // It is not an ActionForward
128
}
129         return forward;
130
131     }
132
133
134     // ------------------------------------------------------- Protected Methods
135

136
137     /**
138      * <p>Return the <code>Catalog</code> we will use to acquire the
139      * <code>Command</code> to be executed. NOTE: Any race condition
140      * calling this method is harmless, so do not bother to synchronize.</p>
141      */

142     protected Catalog getCatalog() {
143
144         if (catalog == null) {
145             catalog = CatalogFactory.getInstance().getCatalog();
146         }
147         return catalog;
148
149     }
150
151
152 }
153
Popular Tags