001: /*
002: * Copyright 2002-2006 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.config.ModuleConfig;
029: import org.apache.struts.tiles.TilesRequestProcessor;
030:
031: import org.springframework.beans.BeansException;
032: import org.springframework.web.context.WebApplicationContext;
033:
034: /**
035: * Subclass of Struts's TilesRequestProcessor that autowires
036: * Struts Actions defined in ContextLoaderPlugIn's WebApplicationContext
037: * (or, as a fallback, in the root WebApplicationContext).
038: *
039: * <p>Behaves like
040: * {@link DelegatingRequestProcessor DelegatingRequestProcessor},
041: * but also provides the Tiles functionality of the original TilesRequestProcessor.
042: * As there's just a single central class to customize in Struts, we have to provide
043: * another subclass here, covering both the Tiles and the Spring delegation aspect.
044: *
045: * <p>The default implementation delegates to the DelegatingActionUtils
046: * class as fas as possible, to reuse as much code as possible despite
047: * the need to provide two RequestProcessor subclasses. If you need to
048: * subclass yet another RequestProcessor, take this class as a template,
049: * delegating to DelegatingActionUtils just like it.
050: *
051: * @author Juergen Hoeller
052: * @since 1.0.2
053: * @see DelegatingRequestProcessor
054: * @see DelegatingActionProxy
055: * @see DelegatingActionUtils
056: * @see ContextLoaderPlugIn
057: */
058: public class DelegatingTilesRequestProcessor extends
059: TilesRequestProcessor {
060:
061: private WebApplicationContext webApplicationContext;
062:
063: public void init(ActionServlet actionServlet,
064: ModuleConfig moduleConfig) throws ServletException {
065: super .init(actionServlet, moduleConfig);
066: if (actionServlet != null) {
067: this .webApplicationContext = initWebApplicationContext(
068: actionServlet, moduleConfig);
069: }
070: }
071:
072: /**
073: * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
074: * falling back to the root WebApplicationContext. This context is supposed
075: * to contain the Struts Action beans to delegate to.
076: * @param actionServlet the associated ActionServlet
077: * @param moduleConfig the associated ModuleConfig
078: * @return the WebApplicationContext
079: * @throws IllegalStateException if no WebApplicationContext could be found
080: * @see DelegatingActionUtils#findRequiredWebApplicationContext
081: * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
082: */
083: protected WebApplicationContext initWebApplicationContext(
084: ActionServlet actionServlet, ModuleConfig moduleConfig)
085: throws IllegalStateException {
086:
087: return DelegatingActionUtils.findRequiredWebApplicationContext(
088: actionServlet, moduleConfig);
089: }
090:
091: /**
092: * Return the WebApplicationContext that this processor delegates to.
093: */
094: protected final WebApplicationContext getWebApplicationContext() {
095: return webApplicationContext;
096: }
097:
098: /**
099: * Override the base class method to return the delegate action.
100: * @see #getDelegateAction
101: */
102: protected Action processActionCreate(HttpServletRequest request,
103: HttpServletResponse response, ActionMapping mapping)
104: throws IOException {
105:
106: Action action = getDelegateAction(mapping);
107: if (action != null) {
108: return action;
109: }
110: return super .processActionCreate(request, response, mapping);
111: }
112:
113: /**
114: * Return the delegate Action for the given mapping.
115: * <p>The default implementation determines a bean name from the
116: * given ActionMapping and looks up the corresponding bean in the
117: * WebApplicationContext.
118: * @param mapping the Struts ActionMapping
119: * @return the delegate Action, or <code>null</code> if none found
120: * @throws BeansException if thrown by WebApplicationContext methods
121: * @see #determineActionBeanName
122: */
123: protected Action getDelegateAction(ActionMapping mapping)
124: throws BeansException {
125: String beanName = determineActionBeanName(mapping);
126: if (!getWebApplicationContext().containsBean(beanName)) {
127: return null;
128: }
129: return (Action) getWebApplicationContext().getBean(beanName,
130: Action.class);
131: }
132:
133: /**
134: * Determine the name of the Action bean, to be looked up in
135: * the WebApplicationContext.
136: * <p>The default implementation takes the mapping path and
137: * prepends the module prefix, if any.
138: * @param mapping the Struts ActionMapping
139: * @return the name of the Action bean
140: * @see DelegatingActionUtils#determineActionBeanName
141: * @see ActionMapping#getPath
142: * @see ModuleConfig#getPrefix
143: */
144: protected String determineActionBeanName(ActionMapping mapping) {
145: return DelegatingActionUtils.determineActionBeanName(mapping);
146: }
147:
148: }
|