KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.struts.action.ActionMapping;
22 import org.apache.struts.action.ActionServlet;
23 import org.apache.struts.config.ModuleConfig;
24
25 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
26 import org.springframework.web.context.WebApplicationContext;
27 import org.springframework.web.context.support.WebApplicationContextUtils;
28
29 /**
30  * Common methods for letting Struts Actions work with a
31  * Spring WebApplicationContext.
32  *
33  * <p>As everything in Struts is based on concrete inheritance,
34  * we have to provide an Action subclass (DelegatingActionProxy) and
35  * two RequestProcessor subclasses (DelegatingRequestProcessor and
36  * DelegatingTilesRequestProcessor). The only way to share common
37  * functionality is a utility class like this one.
38  *
39  * @author Juergen Hoeller
40  * @since 1.0.2
41  * @see DelegatingActionProxy
42  * @see DelegatingRequestProcessor
43  * @see DelegatingTilesRequestProcessor
44  */

45 public abstract class DelegatingActionUtils {
46
47     /**
48      * The name of the autowire init-param specified on the Struts ActionServlet:
49      * "spring.autowire"
50      */

51     public static final String JavaDoc PARAM_AUTOWIRE = "spring.autowire";
52
53     /**
54      * The name of the dependency check init-param specified on the Struts ActionServlet:
55      * "spring.dependencyCheck"
56      */

57     public static final String JavaDoc PARAM_DEPENDENCY_CHECK = "spring.dependencyCheck";
58
59     /**
60      * Value of the autowire init-param that indicates autowiring by name:
61      * "byName"
62      */

63     public static final String JavaDoc AUTOWIRE_BY_NAME = "byName";
64
65     /**
66      * Value of the autowire init-param that indicates autowiring by type:
67      * "byType"
68      */

69     public static final String JavaDoc AUTOWIRE_BY_TYPE = "byType";
70
71
72     private static final Log logger = LogFactory.getLog(DelegatingActionUtils.class);
73
74
75     /**
76      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext.
77      * <p>Checks for a module-specific context first, falling back to the
78      * context for the default module else.
79      * @param actionServlet the associated ActionServlet
80      * @param moduleConfig the associated ModuleConfig (can be <code>null</code>)
81      * @return the WebApplicationContext, or <code>null</code> if none
82      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
83      */

84     public static WebApplicationContext getWebApplicationContext(
85             ActionServlet actionServlet, ModuleConfig moduleConfig) {
86
87         WebApplicationContext wac = null;
88         String JavaDoc modulePrefix = null;
89
90         // Try module-specific attribute.
91
if (moduleConfig != null) {
92             modulePrefix = moduleConfig.getPrefix();
93             wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute(
94                     ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix);
95         }
96
97         // If not found, try attribute for default module.
98
if (wac == null && !"".equals(modulePrefix)) {
99             wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute(
100                     ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
101         }
102
103         return wac;
104     }
105
106     /**
107      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext.
108      * <p>Checks for a module-specific context first, falling back to the
109      * context for the default module else.
110      * @param actionServlet the associated ActionServlet
111      * @param moduleConfig the associated ModuleConfig (can be <code>null</code>)
112      * @return the WebApplicationContext
113      * @throws IllegalStateException if no WebApplicationContext could be found
114      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
115      */

116     public static WebApplicationContext getRequiredWebApplicationContext(
117             ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException JavaDoc {
118
119         WebApplicationContext wac = getWebApplicationContext(actionServlet, moduleConfig);
120         // If no Struts-specific context found, throw an exception.
121
if (wac == null) {
122             throw new IllegalStateException JavaDoc(
123                     "Could not find ContextLoaderPlugIn's WebApplicationContext as ServletContext attribute [" +
124                     ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + "]: Did you register [" +
125                     ContextLoaderPlugIn.class.getName() + "]?");
126         }
127         return wac;
128     }
129
130     /**
131      * Find most specific context available: check ContextLoaderPlugIn's
132      * WebApplicationContext first, fall back to root WebApplicationContext else.
133      * <p>When checking the ContextLoaderPlugIn context: checks for a module-specific
134      * context first, falling back to the context for the default module else.
135      * @param actionServlet the associated ActionServlet
136      * @param moduleConfig the associated ModuleConfig (can be <code>null</code>)
137      * @return the WebApplicationContext
138      * @throws IllegalStateException if no WebApplicationContext could be found
139      * @see #getWebApplicationContext
140      * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
141      */

142     public static WebApplicationContext findRequiredWebApplicationContext(
143             ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException JavaDoc {
144
145         WebApplicationContext wac = getWebApplicationContext(actionServlet, moduleConfig);
146         // If no Struts-specific context found, fall back to root context.
147
if (wac == null) {
148             wac = WebApplicationContextUtils.getRequiredWebApplicationContext(actionServlet.getServletContext());
149         }
150         return wac;
151     }
152
153     /**
154      * Default implementation of Action bean determination, taking
155      * the mapping path and prepending the module prefix, if any.
156      * @param mapping the Struts ActionMapping
157      * @return the name of the Action bean
158      * @see org.apache.struts.action.ActionMapping#getPath
159      * @see org.apache.struts.config.ModuleConfig#getPrefix
160      */

161     public static String JavaDoc determineActionBeanName(ActionMapping mapping) {
162         String JavaDoc prefix = mapping.getModuleConfig().getPrefix();
163         String JavaDoc path = mapping.getPath();
164         String JavaDoc beanName = prefix + path;
165         if (logger.isDebugEnabled()) {
166             logger.debug("DelegatingActionProxy with mapping path '" + path + "' and module prefix '" +
167                     prefix + "' delegating to Spring bean with name [" + beanName + "]");
168         }
169         return beanName;
170     }
171
172     /**
173      * Determine the autowire mode from the "autowire" init-param of the
174      * Struts ActionServlet, falling back to "AUTOWIRE_BY_TYPE" as default.
175      * @param actionServlet the Struts ActionServlet
176      * @return the autowire mode to use
177      * @see #PARAM_AUTOWIRE
178      * @see #AUTOWIRE_BY_NAME
179      * @see #AUTOWIRE_BY_TYPE
180      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
181      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
182      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
183      */

184     public static int getAutowireMode(ActionServlet actionServlet) {
185         String JavaDoc autowire = actionServlet.getInitParameter(PARAM_AUTOWIRE);
186         if (autowire != null) {
187             if (AUTOWIRE_BY_NAME.equals(autowire)) {
188                 return AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
189             }
190             else if (!AUTOWIRE_BY_TYPE.equals(autowire)) {
191                 throw new IllegalArgumentException JavaDoc("ActionServlet 'autowire' parameter must be 'byName' or 'byType'");
192             }
193         }
194         return AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
195     }
196
197     /**
198      * Determine the dependency check to use from the "dependencyCheck" init-param
199      * of the Struts ActionServlet, falling back to no dependency check as default.
200      * @param actionServlet the Struts ActionServlet
201      * @return whether to enforce a dependency check or not
202      * @see #PARAM_DEPENDENCY_CHECK
203      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
204      */

205     public static boolean getDependencyCheck(ActionServlet actionServlet) {
206         String JavaDoc dependencyCheck = actionServlet.getInitParameter(PARAM_DEPENDENCY_CHECK);
207         return Boolean.valueOf(dependencyCheck).booleanValue();
208     }
209
210 }
211
Popular Tags