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 org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.apache.struts.action.ActionMapping;
022: import org.apache.struts.action.ActionServlet;
023: import org.apache.struts.config.ModuleConfig;
024:
025: import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
026: import org.springframework.web.context.WebApplicationContext;
027: import org.springframework.web.context.support.WebApplicationContextUtils;
028:
029: /**
030: * Common methods for letting Struts Actions work with a
031: * Spring WebApplicationContext.
032: *
033: * <p>As everything in Struts is based on concrete inheritance,
034: * we have to provide an Action subclass (DelegatingActionProxy) and
035: * two RequestProcessor subclasses (DelegatingRequestProcessor and
036: * DelegatingTilesRequestProcessor). The only way to share common
037: * functionality is a utility class like this one.
038: *
039: * @author Juergen Hoeller
040: * @since 1.0.2
041: * @see DelegatingActionProxy
042: * @see DelegatingRequestProcessor
043: * @see DelegatingTilesRequestProcessor
044: */
045: public abstract class DelegatingActionUtils {
046:
047: /**
048: * The name of the autowire init-param specified on the Struts ActionServlet:
049: * "spring.autowire"
050: */
051: public static final String PARAM_AUTOWIRE = "spring.autowire";
052:
053: /**
054: * The name of the dependency check init-param specified on the Struts ActionServlet:
055: * "spring.dependencyCheck"
056: */
057: public static final String PARAM_DEPENDENCY_CHECK = "spring.dependencyCheck";
058:
059: /**
060: * Value of the autowire init-param that indicates autowiring by name:
061: * "byName"
062: */
063: public static final String AUTOWIRE_BY_NAME = "byName";
064:
065: /**
066: * Value of the autowire init-param that indicates autowiring by type:
067: * "byType"
068: */
069: public static final String AUTOWIRE_BY_TYPE = "byType";
070:
071: private static final Log logger = LogFactory
072: .getLog(DelegatingActionUtils.class);
073:
074: /**
075: * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext.
076: * <p>Checks for a module-specific context first, falling back to the
077: * context for the default module else.
078: * @param actionServlet the associated ActionServlet
079: * @param moduleConfig the associated ModuleConfig (can be <code>null</code>)
080: * @return the WebApplicationContext, or <code>null</code> if none
081: * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
082: */
083: public static WebApplicationContext getWebApplicationContext(
084: ActionServlet actionServlet, ModuleConfig moduleConfig) {
085:
086: WebApplicationContext wac = null;
087: String modulePrefix = null;
088:
089: // Try module-specific attribute.
090: if (moduleConfig != null) {
091: modulePrefix = moduleConfig.getPrefix();
092: wac = (WebApplicationContext) actionServlet
093: .getServletContext().getAttribute(
094: ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX
095: + modulePrefix);
096: }
097:
098: // If not found, try attribute for default module.
099: if (wac == null && !"".equals(modulePrefix)) {
100: wac = (WebApplicationContext) actionServlet
101: .getServletContext().getAttribute(
102: ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
103: }
104:
105: return wac;
106: }
107:
108: /**
109: * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext.
110: * <p>Checks for a module-specific context first, falling back to the
111: * context for the default module else.
112: * @param actionServlet the associated ActionServlet
113: * @param moduleConfig the associated ModuleConfig (can be <code>null</code>)
114: * @return the WebApplicationContext
115: * @throws IllegalStateException if no WebApplicationContext could be found
116: * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
117: */
118: public static WebApplicationContext getRequiredWebApplicationContext(
119: ActionServlet actionServlet, ModuleConfig moduleConfig)
120: throws IllegalStateException {
121:
122: WebApplicationContext wac = getWebApplicationContext(
123: actionServlet, moduleConfig);
124: // If no Struts-specific context found, throw an exception.
125: if (wac == null) {
126: throw new IllegalStateException(
127: "Could not find ContextLoaderPlugIn's WebApplicationContext as ServletContext attribute ["
128: + ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX
129: + "]: Did you register ["
130: + ContextLoaderPlugIn.class.getName()
131: + "]?");
132: }
133: return wac;
134: }
135:
136: /**
137: * Find most specific context available: check ContextLoaderPlugIn's
138: * WebApplicationContext first, fall back to root WebApplicationContext else.
139: * <p>When checking the ContextLoaderPlugIn context: checks for a module-specific
140: * context first, falling back to the context for the default module else.
141: * @param actionServlet the associated ActionServlet
142: * @param moduleConfig the associated ModuleConfig (can be <code>null</code>)
143: * @return the WebApplicationContext
144: * @throws IllegalStateException if no WebApplicationContext could be found
145: * @see #getWebApplicationContext
146: * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
147: */
148: public static WebApplicationContext findRequiredWebApplicationContext(
149: ActionServlet actionServlet, ModuleConfig moduleConfig)
150: throws IllegalStateException {
151:
152: WebApplicationContext wac = getWebApplicationContext(
153: actionServlet, moduleConfig);
154: // If no Struts-specific context found, fall back to root context.
155: if (wac == null) {
156: wac = WebApplicationContextUtils
157: .getRequiredWebApplicationContext(actionServlet
158: .getServletContext());
159: }
160: return wac;
161: }
162:
163: /**
164: * Default implementation of Action bean determination, taking
165: * the mapping path and prepending the module prefix, if any.
166: * @param mapping the Struts ActionMapping
167: * @return the name of the Action bean
168: * @see org.apache.struts.action.ActionMapping#getPath
169: * @see org.apache.struts.config.ModuleConfig#getPrefix
170: */
171: public static String determineActionBeanName(ActionMapping mapping) {
172: String prefix = mapping.getModuleConfig().getPrefix();
173: String path = mapping.getPath();
174: String beanName = prefix + path;
175: if (logger.isDebugEnabled()) {
176: logger.debug("DelegatingActionProxy with mapping path '"
177: + path + "' and module prefix '" + prefix
178: + "' delegating to Spring bean with name ["
179: + beanName + "]");
180: }
181: return beanName;
182: }
183:
184: /**
185: * Determine the autowire mode from the "autowire" init-param of the
186: * Struts ActionServlet, falling back to "AUTOWIRE_BY_TYPE" as default.
187: * @param actionServlet the Struts ActionServlet
188: * @return the autowire mode to use
189: * @see #PARAM_AUTOWIRE
190: * @see #AUTOWIRE_BY_NAME
191: * @see #AUTOWIRE_BY_TYPE
192: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
193: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
194: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
195: */
196: public static int getAutowireMode(ActionServlet actionServlet) {
197: String autowire = actionServlet
198: .getInitParameter(PARAM_AUTOWIRE);
199: if (autowire != null) {
200: if (AUTOWIRE_BY_NAME.equals(autowire)) {
201: return AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
202: } else if (!AUTOWIRE_BY_TYPE.equals(autowire)) {
203: throw new IllegalArgumentException(
204: "ActionServlet 'autowire' parameter must be 'byName' or 'byType'");
205: }
206: }
207: return AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
208: }
209:
210: /**
211: * Determine the dependency check to use from the "dependencyCheck" init-param
212: * of the Struts ActionServlet, falling back to no dependency check as default.
213: * @param actionServlet the Struts ActionServlet
214: * @return whether to enforce a dependency check or not
215: * @see #PARAM_DEPENDENCY_CHECK
216: * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
217: */
218: public static boolean getDependencyCheck(ActionServlet actionServlet) {
219: String dependencyCheck = actionServlet
220: .getInitParameter(PARAM_DEPENDENCY_CHECK);
221: return Boolean.valueOf(dependencyCheck).booleanValue();
222: }
223:
224: }
|