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.factory.config.AutowireCapableBeanFactory;
032: import org.springframework.context.ConfigurableApplicationContext;
033: import org.springframework.web.context.WebApplicationContext;
034:
035: /**
036: * Subclass of Struts's TilesRequestProcessor that autowires Struts Actions
037: * with Spring beans defined in ContextLoaderPlugIn's WebApplicationContext
038: * or - in case of general service layer beans - in the root WebApplicationContext.
039: *
040: * <p>Behaves like
041: * {@link AutowiringRequestProcessor AutowiringRequestProcessor},
042: * but also provides the Tiles functionality of the original TilesRequestProcessor.
043: * As there's just a single central class to customize in Struts, we have to provide
044: * another subclass here, covering both the Tiles and the Spring delegation aspect.
045: *
046: * <p>The default implementation delegates to the DelegatingActionUtils
047: * class as fas as possible, to reuse as much code as possible despite
048: * the need to provide two RequestProcessor subclasses. If you need to
049: * subclass yet another RequestProcessor, take this class as a template,
050: * delegating to DelegatingActionUtils just like it.
051: *
052: * @author Juergen Hoeller
053: * @since 2.0
054: * @see AutowiringRequestProcessor
055: * @see ContextLoaderPlugIn
056: * @see DelegatingActionUtils
057: */
058: public class AutowiringTilesRequestProcessor extends
059: TilesRequestProcessor {
060:
061: private WebApplicationContext webApplicationContext;
062:
063: private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_NO;
064:
065: private boolean dependencyCheck = false;
066:
067: public void init(ActionServlet actionServlet,
068: ModuleConfig moduleConfig) throws ServletException {
069: super .init(actionServlet, moduleConfig);
070: if (actionServlet != null) {
071: this .webApplicationContext = initWebApplicationContext(
072: actionServlet, moduleConfig);
073: this .autowireMode = initAutowireMode(actionServlet,
074: moduleConfig);
075: this .dependencyCheck = initDependencyCheck(actionServlet,
076: moduleConfig);
077: }
078: }
079:
080: /**
081: * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
082: * falling back to the root WebApplicationContext. This context is supposed
083: * to contain the service layer beans to wire the Struts Actions with.
084: * @param actionServlet the associated ActionServlet
085: * @param moduleConfig the associated ModuleConfig
086: * @return the WebApplicationContext
087: * @throws IllegalStateException if no WebApplicationContext could be found
088: * @see DelegatingActionUtils#findRequiredWebApplicationContext
089: * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
090: */
091: protected WebApplicationContext initWebApplicationContext(
092: ActionServlet actionServlet, ModuleConfig moduleConfig)
093: throws IllegalStateException {
094:
095: WebApplicationContext wac = DelegatingActionUtils
096: .findRequiredWebApplicationContext(actionServlet,
097: moduleConfig);
098: if (wac instanceof ConfigurableApplicationContext) {
099: ((ConfigurableApplicationContext) wac).getBeanFactory()
100: .ignoreDependencyType(ActionServlet.class);
101: }
102: return wac;
103: }
104:
105: /**
106: * Determine the autowire mode to use for wiring Struts Actions.
107: * <p>The default implementation checks the "autowire" init-param of the
108: * Struts ActionServlet, falling back to "AUTOWIRE_BY_TYPE" as default.
109: * @param actionServlet the associated ActionServlet
110: * @param moduleConfig the associated ModuleConfig
111: * @return the autowire mode to use
112: * @see DelegatingActionUtils#getAutowireMode
113: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
114: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
115: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
116: */
117: protected int initAutowireMode(ActionServlet actionServlet,
118: ModuleConfig moduleConfig) {
119: return DelegatingActionUtils.getAutowireMode(actionServlet);
120: }
121:
122: /**
123: * Determine whether to apply a dependency check after wiring Struts Actions.
124: * <p>The default implementation checks the "dependencyCheck" init-param of the
125: * Struts ActionServlet, falling back to no dependency check as default.
126: * @param actionServlet the associated ActionServlet
127: * @param moduleConfig the associated ModuleConfig
128: * @return whether to enforce a dependency check or not
129: * @see DelegatingActionUtils#getDependencyCheck
130: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
131: */
132: protected boolean initDependencyCheck(ActionServlet actionServlet,
133: ModuleConfig moduleConfig) {
134: return DelegatingActionUtils.getDependencyCheck(actionServlet);
135: }
136:
137: /**
138: * Return the current Spring WebApplicationContext.
139: */
140: protected final WebApplicationContext getWebApplicationContext() {
141: return this .webApplicationContext;
142: }
143:
144: /**
145: * Return the autowire mode to use for wiring Struts Actions.
146: */
147: protected final int getAutowireMode() {
148: return autowireMode;
149: }
150:
151: /**
152: * Return whether to apply a dependency check after wiring Struts Actions.
153: */
154: protected final boolean getDependencyCheck() {
155: return dependencyCheck;
156: }
157:
158: /**
159: * Extend the base class method to autowire each created Action instance.
160: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
161: */
162: protected Action processActionCreate(HttpServletRequest request,
163: HttpServletResponse response, ActionMapping mapping)
164: throws IOException {
165:
166: Action action = super.processActionCreate(request, response,
167: mapping);
168: getWebApplicationContext().getAutowireCapableBeanFactory()
169: .autowireBeanProperties(action, getAutowireMode(),
170: getDependencyCheck());
171: return action;
172: }
173:
174: }
|