KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > struts > DelegatingRequestProcessor


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.web.struts;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.struts.action.Action;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionServlet;
28 import org.apache.struts.action.RequestProcessor;
29 import org.apache.struts.config.ModuleConfig;
30
31 import org.springframework.beans.BeansException;
32 import org.springframework.web.context.WebApplicationContext;
33
34 /**
35  * Subclass of Struts's default RequestProcessor that looks up Spring-managed
36  * Struts Actions defined in ContextLoaderPlugIn's WebApplicationContext
37  * (or, as a fallback, in the root WebApplicationContext).
38  *
39  * <p>In the Struts config file, you can either specify the original Action class
40  * (as when generated by XDoclet), or no Action class at all. In any case, Struts
41  * will delegate to an Action bean in the ContextLoaderPlugIn context.
42  *
43  * <pre>
44  * &lt;action path="/login" type="myapp.MyAction"/&gt;</pre>
45  *
46  * or
47  *
48  * <pre>
49  * &lt;action path="/login"/&gt;</pre>
50  *
51  * The name of the Action bean in the WebApplicationContext will be
52  * determined from the mapping path and module prefix. This can be
53  * customized by overriding the <code>determineActionBeanName</code> method.
54  *
55  * <p>Example:
56  * <ul>
57  * <li>mapping path "/login" -> bean name "/login"<br>
58  * <li>mapping path "/login", module prefix "/mymodule" ->
59  * bean name "/mymodule/login"
60  * </ul>
61  *
62  * <p>A corresponding bean definition in the ContextLoaderPlugin
63  * context looks as follows, being able to fully leverage
64  * Spring's configuration facilities:
65  *
66  * <pre>
67  * &lt;bean name="/login" class="myapp.MyAction"&gt;
68  * &lt;property name="..."&gt;...&lt;/property&gt;
69  * &lt;/bean&gt;</pre>
70  *
71  * Note that you can use a single ContextLoaderPlugIn for all Struts modules.
72  * That context can in turn be loaded from multiple XML files, for example split
73  * according to Struts modules. Alternatively, define one ContextLoaderPlugIn per
74  * Struts module, specifying appropriate "contextConfigLocation" parameters.
75  * In both cases, the Spring bean name has to include the module prefix.
76  *
77  * <p>If you also need the Tiles setup functionality of the original
78  * TilesRequestProcessor, use DelegatingTilesRequestProcessor. As there's just
79  * a single central class to customize in Struts, we have to provide another
80  * subclass here, covering both the Tiles and the Spring delegation aspect.
81  *
82  * <p>If this RequestProcessor conflicts with the need for a different
83  * RequestProcessor subclass (other than TilesRequestProcessor), consider
84  * using {@link DelegatingActionProxy DelegatingActionProxy} as Struts
85  * Action type in your struts-config.
86  *
87  * <p>The default implementation delegates to the DelegatingActionUtils
88  * class as fas as possible, to reuse as much code as possible despite
89  * the need to provide two RequestProcessor subclasses. If you need to
90  * subclass yet another RequestProcessor, take this class as a template,
91  * delegating to DelegatingActionUtils just like it.
92  *
93  * @author Juergen Hoeller
94  * @since 1.0.2
95  * @see #determineActionBeanName
96  * @see DelegatingTilesRequestProcessor
97  * @see DelegatingActionProxy
98  * @see DelegatingActionUtils
99  * @see ContextLoaderPlugIn
100  */

101 public class DelegatingRequestProcessor extends RequestProcessor {
102
103     private WebApplicationContext webApplicationContext;
104     
105
106     public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException JavaDoc {
107         super.init(actionServlet, moduleConfig);
108         if (actionServlet != null) {
109             this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig);
110         }
111     }
112
113     /**
114      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
115      * falling back to the root WebApplicationContext. This context is supposed
116      * to contain the Struts Action beans to delegate to.
117      * @param actionServlet the associated ActionServlet
118      * @param moduleConfig the associated ModuleConfig
119      * @return the WebApplicationContext
120      * @throws IllegalStateException if no WebApplicationContext could be found
121      * @see DelegatingActionUtils#findRequiredWebApplicationContext
122      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
123      */

124     protected WebApplicationContext initWebApplicationContext(
125             ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException JavaDoc {
126
127         return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
128     }
129
130     /**
131      * Return the WebApplicationContext that this processor delegates to.
132      */

133     protected final WebApplicationContext getWebApplicationContext() {
134         return webApplicationContext;
135     }
136
137
138     /**
139      * Override the base class method to return the delegate action.
140      * @see #getDelegateAction
141      */

142     protected Action processActionCreate(
143             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping)
144             throws IOException JavaDoc {
145
146         Action action = getDelegateAction(mapping);
147         if (action != null) {
148             return action;
149         }
150         return super.processActionCreate(request, response, mapping);
151     }
152
153     /**
154      * Return the delegate Action for the given mapping.
155      * <p>The default implementation determines a bean name from the
156      * given ActionMapping and looks up the corresponding bean in the
157      * WebApplicationContext.
158      * @param mapping the Struts ActionMapping
159      * @return the delegate Action, or <code>null</code> if none found
160      * @throws BeansException if thrown by WebApplicationContext methods
161      * @see #determineActionBeanName
162      */

163     protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
164         String JavaDoc beanName = determineActionBeanName(mapping);
165         if (!getWebApplicationContext().containsBean(beanName)) {
166             return null;
167         }
168         return (Action) getWebApplicationContext().getBean(beanName, Action.class);
169     }
170
171     /**
172      * Determine the name of the Action bean, to be looked up in
173      * the WebApplicationContext.
174      * <p>The default implementation takes the mapping path and
175      * prepends the module prefix, if any.
176      * @param mapping the Struts ActionMapping
177      * @return the name of the Action bean
178      * @see DelegatingActionUtils#determineActionBeanName
179      * @see org.apache.struts.action.ActionMapping#getPath
180      * @see org.apache.struts.config.ModuleConfig#getPrefix
181      */

182     protected String JavaDoc determineActionBeanName(ActionMapping mapping) {
183         return DelegatingActionUtils.determineActionBeanName(mapping);
184     }
185
186 }
187
Popular Tags