001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.struts;
018:
019: import java.io.IOException;
020:
021: import javax.servlet.ServletException;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import org.apache.struts.action.Action;
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionServlet;
028: import org.apache.struts.action.RequestProcessor;
029: import org.apache.struts.config.ModuleConfig;
030:
031: import org.springframework.beans.BeansException;
032: import org.springframework.web.context.WebApplicationContext;
033:
034: /**
035: * Subclass of Struts's default {@link RequestProcessor} that looks up
036: * Spring-managed Struts {@link Action Actions} defined in
037: * {@link ContextLoaderPlugIn ContextLoaderPlugIn's} {@link WebApplicationContext}
038: * (or, as a fallback, in the root <code>WebApplicationContext</code>).
039: *
040: * <p>In the Struts config file, you can either specify the original
041: * <code>Action</code> class (as when generated by XDoclet), or no
042: * <code>Action</code> class at all. In any case, Struts will delegate to an
043: * <code>Action</code> bean in the <code>ContextLoaderPlugIn</code> context.
044: *
045: * <pre class="code"><action path="/login" type="myapp.MyAction"/></pre>
046: *
047: * or
048: *
049: * <pre class="code"><action path="/login"/></pre>
050: *
051: * The name of the <code>Action</code> bean in the
052: * <code>WebApplicationContext</code> will be determined from the mapping path
053: * and module prefix. This can be customized by overriding the
054: * {@link #determineActionBeanName} method.
055: *
056: * <p>Example:
057: * <ul>
058: * <li>mapping path "/login" -> bean name "/login"<br>
059: * <li>mapping path "/login", module prefix "/mymodule" ->
060: * bean name "/mymodule/login"
061: * </ul>
062: *
063: * <p>A corresponding bean definition in the <code>ContextLoaderPlugin</code>
064: * context would look as follows; notice that the <code>Action</code> is now
065: * able to leverage fully Spring's configuration facilities:
066: *
067: * <pre class="code">
068: * <bean name="/login" class="myapp.MyAction">
069: * <property name="...">...</property>
070: * </bean></pre>
071: *
072: * Note that you can use a single <code>ContextLoaderPlugIn</code> for all
073: * Struts modules. That context can in turn be loaded from multiple XML files,
074: * for example split according to Struts modules. Alternatively, define one
075: * <code>ContextLoaderPlugIn</code> per Struts module, specifying appropriate
076: * "contextConfigLocation" parameters. In both cases, the Spring bean name has
077: * to include the module prefix.
078: *
079: * <p>If you also need the Tiles setup functionality of the original
080: * <code>TilesRequestProcessor</code>, use
081: * <code>DelegatingTilesRequestProcessor</code>. As there is just a
082: * single central class to customize in Struts, we have to provide another
083: * subclass here, covering both the Tiles and the Spring delegation aspect.
084: *
085: * <p>If this <code>RequestProcessor</code> conflicts with the need for a
086: * different <code>RequestProcessor</code> subclass (other than
087: * <code>TilesRequestProcessor</code>), consider using
088: * {@link DelegatingActionProxy} as the Struts <code>Action</code> type in
089: * your struts-config file.
090: *
091: * <p>The default implementation delegates to the
092: * <code>DelegatingActionUtils</code> class as much as possible, to reuse as
093: * much code as possible despite the need to provide two
094: * <code>RequestProcessor</code> subclasses. If you need to subclass yet
095: * another <code>RequestProcessor</code>, take this class as a template,
096: * delegating to <code>DelegatingActionUtils</code> just like it.
097: *
098: * @author Juergen Hoeller
099: * @since 1.0.2
100: * @see #determineActionBeanName
101: * @see DelegatingTilesRequestProcessor
102: * @see DelegatingActionProxy
103: * @see DelegatingActionUtils
104: * @see ContextLoaderPlugIn
105: */
106: public class DelegatingRequestProcessor extends RequestProcessor {
107:
108: private WebApplicationContext webApplicationContext;
109:
110: public void init(ActionServlet actionServlet,
111: ModuleConfig moduleConfig) throws ServletException {
112: super .init(actionServlet, moduleConfig);
113: if (actionServlet != null) {
114: this .webApplicationContext = initWebApplicationContext(
115: actionServlet, moduleConfig);
116: }
117: }
118:
119: /**
120: * Fetch ContextLoaderPlugIn's {@link WebApplicationContext} from the
121: * <code>ServletContext</code>, falling back to the root
122: * <code>WebApplicationContext</code>.
123: * <p>This context is supposed to contain the Struts <code>Action</code>
124: * beans to delegate to.
125: * @param actionServlet the associated <code>ActionServlet</code>
126: * @param moduleConfig the associated <code>ModuleConfig</code>
127: * @return the <code>WebApplicationContext</code>
128: * @throws IllegalStateException if no <code>WebApplicationContext</code> could be found
129: * @see DelegatingActionUtils#findRequiredWebApplicationContext
130: * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
131: */
132: protected WebApplicationContext initWebApplicationContext(
133: ActionServlet actionServlet, ModuleConfig moduleConfig)
134: throws IllegalStateException {
135:
136: return DelegatingActionUtils.findRequiredWebApplicationContext(
137: actionServlet, moduleConfig);
138: }
139:
140: /**
141: * Return the <code>WebApplicationContext</code> that this processor
142: * delegates to.
143: */
144: protected final WebApplicationContext getWebApplicationContext() {
145: return this .webApplicationContext;
146: }
147:
148: /**
149: * Override the base class method to return the delegate action.
150: * @see #getDelegateAction
151: */
152: protected Action processActionCreate(HttpServletRequest request,
153: HttpServletResponse response, ActionMapping mapping)
154: throws IOException {
155:
156: Action action = getDelegateAction(mapping);
157: if (action != null) {
158: return action;
159: }
160: return super .processActionCreate(request, response, mapping);
161: }
162:
163: /**
164: * Return the delegate <code>Action</code> for the given mapping.
165: * <p>The default implementation determines a bean name from the
166: * given <code>ActionMapping</code> and looks up the corresponding
167: * bean in the <code>WebApplicationContext</code>.
168: * @param mapping the Struts <code>ActionMapping</code>
169: * @return the delegate <code>Action</code>, or <code>null</code> if none found
170: * @throws BeansException if thrown by <code>WebApplicationContext</code> methods
171: * @see #determineActionBeanName
172: */
173: protected Action getDelegateAction(ActionMapping mapping)
174: throws BeansException {
175: String beanName = determineActionBeanName(mapping);
176: if (!getWebApplicationContext().containsBean(beanName)) {
177: return null;
178: }
179: return (Action) getWebApplicationContext().getBean(beanName,
180: Action.class);
181: }
182:
183: /**
184: * Determine the name of the <code>Action</code> bean, to be looked up in
185: * the <code>WebApplicationContext</code>.
186: * <p>The default implementation takes the
187: * {@link org.apache.struts.action.ActionMapping#getPath mapping path} and
188: * prepends the
189: * {@link org.apache.struts.config.ModuleConfig#getPrefix module prefix},
190: * if any.
191: * @param mapping the Struts <code>ActionMapping</code>
192: * @return the name of the Action bean
193: * @see DelegatingActionUtils#determineActionBeanName
194: * @see org.apache.struts.action.ActionMapping#getPath
195: * @see org.apache.struts.config.ModuleConfig#getPrefix
196: */
197: protected String determineActionBeanName(ActionMapping mapping) {
198: return DelegatingActionUtils.determineActionBeanName(mapping);
199: }
200:
201: }
|