KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.ModuleConfig;
29 import org.apache.struts.tiles.TilesRequestProcessor;
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 TilesRequestProcessor 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>Behaves like
41  * {@link AutowiringRequestProcessor AutowiringRequestProcessor},
42  * but also provides the Tiles functionality of the original TilesRequestProcessor.
43  * As there's just a single central class to customize in Struts, we have to provide
44  * another subclass here, covering both the Tiles and the Spring delegation aspect.
45  *
46  * <p>The default implementation delegates to the DelegatingActionUtils
47  * class as fas as possible, to reuse as much code as possible despite
48  * the need to provide two RequestProcessor subclasses. If you need to
49  * subclass yet another RequestProcessor, take this class as a template,
50  * delegating to DelegatingActionUtils just like it.
51  *
52  * @author Juergen Hoeller
53  * @since 2.0
54  * @see AutowiringRequestProcessor
55  * @see ContextLoaderPlugIn
56  * @see DelegatingActionUtils
57  */

58 public class AutowiringTilesRequestProcessor extends TilesRequestProcessor {
59
60     private WebApplicationContext webApplicationContext;
61
62     private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_NO;
63
64     private boolean dependencyCheck = false;
65
66
67     public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException JavaDoc {
68         super.init(actionServlet, moduleConfig);
69         if (actionServlet != null) {
70             this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig);
71             this.autowireMode = initAutowireMode(actionServlet, moduleConfig);
72             this.dependencyCheck = initDependencyCheck(actionServlet, moduleConfig);
73         }
74     }
75
76     /**
77      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
78      * falling back to the root WebApplicationContext. This context is supposed
79      * to contain the service layer beans to wire the Struts Actions with.
80      * @param actionServlet the associated ActionServlet
81      * @param moduleConfig the associated ModuleConfig
82      * @return the WebApplicationContext
83      * @throws IllegalStateException if no WebApplicationContext could be found
84      * @see DelegatingActionUtils#findRequiredWebApplicationContext
85      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
86      */

87     protected WebApplicationContext initWebApplicationContext(
88             ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException JavaDoc {
89
90         WebApplicationContext wac =
91                 DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
92         if (wac instanceof ConfigurableApplicationContext) {
93             ((ConfigurableApplicationContext) wac).getBeanFactory().ignoreDependencyType(ActionServlet.class);
94         }
95         return wac;
96     }
97
98     /**
99      * Determine the autowire mode to use for wiring Struts Actions.
100      * <p>The default implementation checks the "autowire" init-param of the
101      * Struts ActionServlet, falling back to "AUTOWIRE_BY_TYPE" as default.
102      * @param actionServlet the associated ActionServlet
103      * @param moduleConfig the associated ModuleConfig
104      * @return the autowire mode to use
105      * @see DelegatingActionUtils#getAutowireMode
106      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
107      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
108      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
109      */

110     protected int initAutowireMode(ActionServlet actionServlet, ModuleConfig moduleConfig) {
111         return DelegatingActionUtils.getAutowireMode(actionServlet);
112     }
113
114     /**
115      * Determine whether to apply a dependency check after wiring Struts Actions.
116      * <p>The default implementation checks the "dependencyCheck" init-param of the
117      * Struts ActionServlet, falling back to no dependency check as default.
118      * @param actionServlet the associated ActionServlet
119      * @param moduleConfig the associated ModuleConfig
120      * @return whether to enforce a dependency check or not
121      * @see DelegatingActionUtils#getDependencyCheck
122      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
123      */

124     protected boolean initDependencyCheck(ActionServlet actionServlet, ModuleConfig moduleConfig) {
125         return DelegatingActionUtils.getDependencyCheck(actionServlet);
126     }
127
128
129     /**
130      * Return the current Spring WebApplicationContext.
131      */

132     protected final WebApplicationContext getWebApplicationContext() {
133         return this.webApplicationContext;
134     }
135
136     /**
137      * Return the autowire mode to use for wiring Struts Actions.
138      */

139     protected final int getAutowireMode() {
140         return autowireMode;
141     }
142
143     /**
144      * Return whether to apply a dependency check after wiring Struts Actions.
145      */

146     protected final boolean getDependencyCheck() {
147         return dependencyCheck;
148     }
149
150
151     /**
152      * Extend the base class method to autowire each created Action instance.
153      * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties
154      */

155     protected Action processActionCreate(
156             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping)
157             throws IOException JavaDoc {
158
159         Action action = super.processActionCreate(request, response, mapping);
160         getWebApplicationContext().getAutowireCapableBeanFactory().autowireBeanProperties(
161                 action, getAutowireMode(), getDependencyCheck());
162         return action;
163     }
164
165 }
166
Popular Tags