KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.struts.action.Action;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionServlet;
28 import org.apache.struts.action.RequestProcessor;
29 import org.apache.struts.config.ModuleConfig;
30
31 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
32 import org.springframework.context.ConfigurableApplicationContext;
33 import org.springframework.web.context.WebApplicationContext;
34
35 /**
36  * Subclass of Struts's default RequestProcessor that autowires Struts Actions
37  * with Spring beans defined in ContextLoaderPlugIn's WebApplicationContext
38  * or - in case of general service layer beans - in the root WebApplicationContext.
39  *
40  * <p>In the Struts config file, you simply continue to specify the original
41  * Action class. The instance created for that class will automatically get
42  * wired with matching service layer beans, that is, bean property setters
43  * will automatically be called if a service layer bean matches the property.
44  *
45  * <pre>
46  * &lt;action path="/login" type="myapp.MyAction"/&gt;</pre>
47  *
48  * There are two autowire modes available: "byType" and "byName". The default
49  * is "byType", matching service layer beans with the Action's bean property
50  * argument types. This behavior can be changed through specifying an "autowire"
51  * init-param for the Struts ActionServlet with the value "byName", which will
52  * match service layer bean names with the Action's bean property <i>names</i>.
53  *
54  * <p>Dependency checking is turned off by default: If no matching service
55  * layer bean can be found, the setter in question will simply not get invoked.
56  * To enforce matching service layer beans, consider specify the "dependencyCheck"
57  * init-param for the Struts ActionServlet with the value "true".
58  *
59  * <p>If you also need the Tiles setup functionality of the original
60  * TilesRequestProcessor, use AutowiringTilesRequestProcessor. As there's just
61  * a single central class to customize in Struts, we have to provide another
62  * subclass here, covering both the Tiles and the Spring delegation aspect.
63  *
64  * <p>The default implementation delegates to the DelegatingActionUtils
65  * class as fas as possible, to reuse as much code as possible despite
66  * the need to provide two RequestProcessor subclasses. If you need to
67  * subclass yet another RequestProcessor, take this class as a template,
68  * delegating to DelegatingActionUtils just like it.
69  *
70  * @author Juergen Hoeller
71  * @since 2.0
72  * @see AutowiringTilesRequestProcessor
73  * @see ContextLoaderPlugIn
74  * @see DelegatingActionUtils
75  */

76 public class AutowiringRequestProcessor extends RequestProcessor {
77
78     private WebApplicationContext webApplicationContext;
79
80     private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_NO;
81
82     private boolean dependencyCheck = false;
83
84
85     public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException JavaDoc {
86         super.init(actionServlet, moduleConfig);
87         if (actionServlet != null) {
88             this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig);
89             this.autowireMode = initAutowireMode(actionServlet, moduleConfig);
90             this.dependencyCheck = initDependencyCheck(actionServlet, moduleConfig);
91         }
92     }
93
94     /**
95      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
96      * falling back to the root WebApplicationContext. This context is supposed
97      * to contain the service layer beans to wire the Struts Actions with.
98      * @param actionServlet the associated ActionServlet
99      * @param moduleConfig the associated ModuleConfig
100      * @return the WebApplicationContext
101      * @throws IllegalStateException if no WebApplicationContext could be found
102      * @see DelegatingActionUtils#findRequiredWebApplicationContext
103      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
104      */

105     protected WebApplicationContext initWebApplicationContext(
106             ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException JavaDoc {
107
108         WebApplicationContext wac =
109                 DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
110         if (wac instanceof ConfigurableApplicationContext) {
111             ((ConfigurableApplicationContext) wac).getBeanFactory().ignoreDependencyType(ActionServlet.class);
112         }
113         return wac;
114     }
115
116     /**
117      * Determine the autowire mode to use for wiring Struts Actions.
118      * <p>The default implementation checks the "autowire" init-param of the
119      * Struts ActionServlet, falling back to "AUTOWIRE_BY_TYPE" as default.
120      * @param actionServlet the associated ActionServlet
121      * @param moduleConfig the associated ModuleConfig
122      * @return the autowire mode to use
123      * @see DelegatingActionUtils#getAutowireMode
124      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
125      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
126      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
127      */

128     protected int initAutowireMode(ActionServlet actionServlet, ModuleConfig moduleConfig) {
129         return DelegatingActionUtils.getAutowireMode(actionServlet);
130     }
131
132     /**
133      * Determine whether to apply a dependency check after wiring Struts Actions.
134      * <p>The default implementation checks the "dependencyCheck" init-param of the
135      * Struts ActionServlet, falling back to no dependency check as default.
136      * @param actionServlet the associated ActionServlet
137      * @param moduleConfig the associated ModuleConfig
138      * @return whether to enforce a dependency check or not
139      * @see DelegatingActionUtils#getDependencyCheck
140      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
141      */

142     protected boolean initDependencyCheck(ActionServlet actionServlet, ModuleConfig moduleConfig) {
143         return DelegatingActionUtils.getDependencyCheck(actionServlet);
144     }
145
146
147     /**
148      * Return the current Spring WebApplicationContext.
149      */

150     protected final WebApplicationContext getWebApplicationContext() {
151         return this.webApplicationContext;
152     }
153
154     /**
155      * Return the autowire mode to use for wiring Struts Actions.
156      */

157     protected final int getAutowireMode() {
158         return autowireMode;
159     }
160
161     /**
162      * Return whether to apply a dependency check after wiring Struts Actions.
163      */

164     protected final boolean getDependencyCheck() {
165         return dependencyCheck;
166     }
167
168
169     /**
170      * Extend the base class method to autowire each created Action instance.
171      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
172      */

173     protected Action processActionCreate(
174             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping)
175             throws IOException JavaDoc {
176
177         Action action = super.processActionCreate(request, response, mapping);
178         getWebApplicationContext().getAutowireCapableBeanFactory().autowireBeanProperties(
179                 action, getAutowireMode(), getDependencyCheck());
180         return action;
181     }
182
183 }
184
Popular Tags