KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.apache.struts.action.Action;
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionForward;
25 import org.apache.struts.action.ActionMapping;
26 import org.apache.struts.action.ActionServlet;
27 import org.apache.struts.config.ModuleConfig;
28
29 import org.springframework.beans.BeansException;
30 import org.springframework.web.context.WebApplicationContext;
31
32 /**
33  * Proxy for a Spring-managed Struts Action that's defined in
34  * ContextLoaderPlugIn's WebApplicationContext.
35  *
36  * <p>The proxy is defined in the Struts config file, specifying this
37  * class as action class. It will delegate to a Struts Action bean
38  * in the ContextLoaderPlugIn context.
39  *
40  * <pre>
41  * &lt;action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"/&gt;</pre>
42  *
43  * The name of the Action bean in the WebApplicationContext will be
44  * determined from the mapping path and module prefix. This can be
45  * customized by overriding the <code>determineActionBeanName</code> method.
46  *
47  * <p>Example:
48  * <ul>
49  * <li>mapping path "/login" -> bean name "/login"<br>
50  * <li>mapping path "/login", module prefix "/mymodule" ->
51  * bean name "/mymodule/login"
52  * </ul>
53  *
54  * <p>A corresponding bean definition in the ContextLoaderPlugin
55  * context looks as follows, being able to fully leverage
56  * Spring's configuration facilities:
57  *
58  * <pre>
59  * &lt;bean name="/login" class="myapp.MyAction"&gt;
60  * &lt;property name="..."&gt;...&lt;/property&gt;
61  * &lt;/bean&gt;</pre>
62  *
63  * Note that you can use a single ContextLoaderPlugIn for all Struts modules.
64  * That context can in turn be loaded from multiple XML files, for example split
65  * according to Struts modules. Alternatively, define one ContextLoaderPlugIn per
66  * Struts module, specifying appropriate "contextConfigLocation" parameters.
67  * In both cases, the Spring bean name has to include the module prefix.
68  *
69  * <p>If you want to avoid having to specify DelegatingActionProxy as Action
70  * type in your struts-config, for example to be able to generate your
71  * Struts config file with XDoclet, consider
72  * {@link DelegatingRequestProcessor DelegatingRequestProcessor}.
73  * The latter's disadvantage is that it might conflict with the need
74  * for a different RequestProcessor subclass.
75  *
76  * <p>The default implementation delegates to the DelegatingActionUtils
77  * class as fas as possible, to reuse as much code as possible with
78  * DelegatingRequestProcessor and DelegatingTilesRequestProcessor.
79  *
80  * <p>Note: The idea of delegating to Spring-managed Struts Actions originated in
81  * Don Brown's <a HREF="http://struts.sourceforge.net/struts-spring">Spring Struts Plugin</a>.
82  * ContextLoaderPlugIn and DelegatingActionProxy constitute a clean-room
83  * implementation of the same idea, essentially superseding the original plugin.
84  * Many thanks to Don Brown and Matt Raible for the original work and for the
85  * agreement to reimplement the idea in Spring proper!
86  *
87  * @author Juergen Hoeller
88  * @since 1.0.1
89  * @see #determineActionBeanName
90  * @see DelegatingRequestProcessor
91  * @see DelegatingTilesRequestProcessor
92  * @see DelegatingActionUtils
93  * @see ContextLoaderPlugIn
94  */

95 public class DelegatingActionProxy extends Action {
96
97     /**
98      * Pass the execute call on to the Spring-managed delegate Action.
99      * @see #getDelegateAction
100      */

101     public ActionForward execute(
102             ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
103             throws Exception JavaDoc {
104
105         Action delegateAction = getDelegateAction(mapping);
106         return delegateAction.execute(mapping, form, request, response);
107     }
108
109     /**
110      * Return the delegate Action for the given mapping.
111      * <p>The default implementation determines a bean name from the
112      * given ActionMapping and looks up the corresponding bean in the
113      * WebApplicationContext.
114      * @param mapping the Struts ActionMapping
115      * @return the delegate Action
116      * @throws BeansException if thrown by WebApplicationContext methods
117      * @see #determineActionBeanName
118      */

119     protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
120         WebApplicationContext wac = getWebApplicationContext(getServlet(), mapping.getModuleConfig());
121         String JavaDoc beanName = determineActionBeanName(mapping);
122         return (Action) wac.getBean(beanName, Action.class);
123     }
124
125     /**
126      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
127      * falling back to the root WebApplicationContext. This context is supposed
128      * to contain the Struts Action beans to delegate to.
129      * @param actionServlet the associated ActionServlet
130      * @param moduleConfig the associated ModuleConfig
131      * @return the WebApplicationContext
132      * @throws IllegalStateException if no WebApplicationContext could be found
133      * @see DelegatingActionUtils#findRequiredWebApplicationContext
134      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
135      */

136     protected WebApplicationContext getWebApplicationContext(
137             ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException JavaDoc {
138
139         return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
140     }
141
142     /**
143      * Determine the name of the Action bean, to be looked up in
144      * the WebApplicationContext.
145      * <p>The default implementation takes the mapping path and
146      * prepends the module prefix, if any.
147      * @param mapping the Struts ActionMapping
148      * @return the name of the Action bean
149      * @see DelegatingActionUtils#determineActionBeanName
150      * @see org.apache.struts.action.ActionMapping#getPath
151      * @see org.apache.struts.config.ModuleConfig#getPrefix
152      */

153     protected String JavaDoc determineActionBeanName(ActionMapping mapping) {
154         return DelegatingActionUtils.determineActionBeanName(mapping);
155     }
156
157 }
158
Popular Tags