KickJava   Java API By Example, From Geeks To Geeks.

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


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  * configured Chain (or Command) for performing the actual business
36  * logic related to a request. The name of the chain to be executed
37  * is specified by setting the <code>parameter</code> attribute of
38  * the <code>&lt;action&gt;</code> element configuring this action.
39  * For example:</p>
40  *
41  * <pre>
42  * &lt;action path="/myaction"
43  * type="org.apache.struts.chain.legacy.ChainAction"
44  * name="myform"
45  * scope="request"
46  * input="/mypage.jsp"
47  * parameter="name-of-chain-to-execute"
48  * </pre>
49  *
50  * <p>Prior to calling the specified chain (or command), this action
51  * sets up a <code>Context</code> object containing the relevant
52  * properties, along with the following additional attributes:</p>
53  * <ul>
54  * <li><strong>mapping</strong> - The <code>ActionMapping</code> passed
55  * to our <code>execute()</code> method</li>
56  * <li><strong>form</strong> - The <code>ActionForm</code> passed to
57  * our <code>execute()</code> method</li>
58  * </ul>
59  *
60  * <p>After execution of the specified command or chain is completed,
61  * the following context attributes are examined (in this order) to
62  * determine how to proceed.</p>
63  * <ul>
64  * <li><strong>exception</strong> - If a <code>java.lang.Exception</code>
65  * is found here, it will be rethrown as the outcome of this action.</li>
66  * <li><strong>forward</strong> - If an
67  * <code>org.apache.struts.action.ActionForward</code> is found here,
68  * it will be returned as the outcome of this action. Otherwise,
69  * <code>null</code> will be returned.</li>
70  * </ul>
71  */

72
73 public class ChainAction extends Action {
74
75
76     // ------------------------------------------------------- Instance Varibles
77

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

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

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

97     public ActionForward execute(ActionMapping mapping,
98                                  ActionForm form,
99                                  HttpServletRequest JavaDoc request,
100                                  HttpServletResponse JavaDoc response)
101         throws Exception JavaDoc {
102
103         // Set up a context for this request
104
Context context = new ServletWebContext
105             (getServlet().getServletContext(), request, response);
106         context.put("mapping", mapping);
107         context.put("form", form);
108
109         // Delegate to the specified command
110
Command command = getCatalog().getCommand(mapping.getParameter());
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