KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ModuleConfig;
29 import org.apache.struts.tiles.TilesRequestProcessor;
30
31 import org.springframework.beans.BeansException;
32 import org.springframework.web.context.WebApplicationContext;
33
34 /**
35  * Subclass of Struts's TilesRequestProcessor that autowires
36  * Struts Actions defined in ContextLoaderPlugIn's WebApplicationContext
37  * (or, as a fallback, in the root WebApplicationContext).
38  *
39  * <p>Behaves like
40  * {@link DelegatingRequestProcessor DelegatingRequestProcessor},
41  * but also provides the Tiles functionality of the original TilesRequestProcessor.
42  * As there's just a single central class to customize in Struts, we have to provide
43  * another subclass here, covering both the Tiles and the Spring delegation aspect.
44  *
45  * <p>The default implementation delegates to the DelegatingActionUtils
46  * class as fas as possible, to reuse as much code as possible despite
47  * the need to provide two RequestProcessor subclasses. If you need to
48  * subclass yet another RequestProcessor, take this class as a template,
49  * delegating to DelegatingActionUtils just like it.
50  *
51  * @author Juergen Hoeller
52  * @since 1.0.2
53  * @see DelegatingRequestProcessor
54  * @see DelegatingActionProxy
55  * @see DelegatingActionUtils
56  * @see ContextLoaderPlugIn
57  */

58 public class DelegatingTilesRequestProcessor extends TilesRequestProcessor {
59
60     private WebApplicationContext webApplicationContext;
61     
62
63     public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException JavaDoc {
64         super.init(actionServlet, moduleConfig);
65         if (actionServlet != null) {
66             this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig);
67         }
68     }
69
70     /**
71      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
72      * falling back to the root WebApplicationContext. This context is supposed
73      * to contain the Struts Action beans to delegate to.
74      * @param actionServlet the associated ActionServlet
75      * @param moduleConfig the associated ModuleConfig
76      * @return the WebApplicationContext
77      * @throws IllegalStateException if no WebApplicationContext could be found
78      * @see DelegatingActionUtils#findRequiredWebApplicationContext
79      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
80      */

81     protected WebApplicationContext initWebApplicationContext(
82             ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException JavaDoc {
83
84         return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
85     }
86
87     /**
88      * Return the WebApplicationContext that this processor delegates to.
89      */

90     protected final WebApplicationContext getWebApplicationContext() {
91         return webApplicationContext;
92     }
93
94
95     /**
96      * Override the base class method to return the delegate action.
97      * @see #getDelegateAction
98      */

99     protected Action processActionCreate(
100             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping)
101             throws IOException JavaDoc {
102
103         Action action = getDelegateAction(mapping);
104         if (action != null) {
105             return action;
106         }
107         return super.processActionCreate(request, response, mapping);
108     }
109
110     /**
111      * Return the delegate Action for the given mapping.
112      * <p>The default implementation determines a bean name from the
113      * given ActionMapping and looks up the corresponding bean in the
114      * WebApplicationContext.
115      * @param mapping the Struts ActionMapping
116      * @return the delegate Action, or <code>null</code> if none found
117      * @throws BeansException if thrown by WebApplicationContext methods
118      * @see #determineActionBeanName
119      */

120     protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
121         String JavaDoc beanName = determineActionBeanName(mapping);
122         if (!getWebApplicationContext().containsBean(beanName)) {
123             return null;
124         }
125         return (Action) getWebApplicationContext().getBean(beanName, Action.class);
126     }
127
128     /**
129      * Determine the name of the Action bean, to be looked up in
130      * the WebApplicationContext.
131      * <p>The default implementation takes the mapping path and
132      * prepends the module prefix, if any.
133      * @param mapping the Struts ActionMapping
134      * @return the name of the Action bean
135      * @see DelegatingActionUtils#determineActionBeanName
136      * @see ActionMapping#getPath
137      * @see ModuleConfig#getPrefix
138      */

139     protected String JavaDoc determineActionBeanName(ActionMapping mapping) {
140         return DelegatingActionUtils.determineActionBeanName(mapping);
141     }
142
143 }
144
Popular Tags