KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > ServletWrappingController


1 /*
2  * Copyright 2002-2005 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.servlet.mvc;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.servlet.Servlet JavaDoc;
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.springframework.beans.factory.BeanNameAware;
29 import org.springframework.beans.factory.DisposableBean;
30 import org.springframework.beans.factory.InitializingBean;
31 import org.springframework.web.servlet.ModelAndView;
32
33 /**
34  * Spring Controller implementation that wraps a servlet instance which it manages
35  * internally. Such a wrapped servlet is not known outside of this controller;
36  * its entire lifecycle is covered here (in contrast to ServletForwardingController).
37  *
38  * <p>Useful to invoke an existing servlet via Spring's dispatching infrastructure,
39  * for example to apply Spring HandlerInterceptors to its requests. This will work
40  * even in a Servlet 2.2 container that does not support Servlet filters.
41  *
42  * <p>In particular, the main intent of this controller is to allow for applying
43  * Spring's OpenSessionInViewInterceptor or OpenPersistenceManagerInViewInterceptor
44  * to Struts actions in a Servlet 2.2 container. The Struts ActionServlet will be
45  * wrapped by this controller in such a scenario, rather than defined in web.xml.
46  * You then need to map "/*.do" (or whatever pattern you choose for your Struts actions)
47  * onto this controller, which will in turn forward to the Struts ActionServlet.
48  *
49  * <p>Note that Struts has a special requirement in that it parses web.xml to
50  * find its servlet mapping. Therefore, you need to specify the DispatcherServlet's
51  * servlet name as "servletName" on this controller, so that Struts finds the
52  * DispatcherServlet's mapping (thinking that it refers to the ActionServlet).
53  *
54  * <p>In a Servlet 2.3 container, when not using Spring's own web MVC framework,
55  * it is recommended to use classic servlet mapping in combination with a filter,
56  * for example Spring's OpenSessionInViewFilter or OpenPersistenceManagerInViewFilter.
57  *
58  * <p><b>Example:</b> a DispatcherServlet XML context, forwarding "*.do" to the Struts
59  * ActionServlet wrapped by a ServletWrappingController. All such requests will go
60  * through the configured HandlerInterceptor chain (e.g. an OpenSessionInViewInterceptor).
61  * From the Struts point of view, everything will work as usual.
62  *
63  * <pre>
64  * &lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
65  * &lt;property name="interceptors"&gt;
66  * &lt;list&gt;
67  * &lt;ref bean="openSessionInViewInterceptor"/&gt;
68  * &lt;/list&gt;
69  * &lt;/property&gt;
70  * &lt;property name="mappings"&gt;
71  * &lt;props&gt;
72  * &lt;prop key="*.do"&gt;strutsWrappingController&lt;/prop&gt;
73  * &lt;/props&gt;
74  * &lt;/property&gt;
75  * &lt;/bean&gt;
76  *
77  * &lt;bean id="strutsWrappingController" class="org.springframework.web.servlet.mvc.ServletWrappingController"&gt;
78  * &lt;property name="servletClass"&gt;
79  * &lt;value&gt;org.apache.struts.action.ActionServlet&lt;/value&gt;
80  * &lt;/property&gt;
81  * &lt;property name="servletName"&gt;
82  * &lt;value&gt;action&lt;/value&gt;
83  * &lt;/property&gt;
84  * &lt;property name="initParameters"&gt;
85  * &lt;props&gt;
86  * &lt;prop key="config"&gt;/WEB-INF/struts-config.xml&lt;/prop&gt;
87  * &lt;/props&gt;
88  * &lt;/property&gt;
89  * &lt;/bean&gt;</pre>
90  *
91  * Thanks to Keith Garry Boyce for pointing out the issue with Struts in a
92  * Servlet 2.2 container, and for providing a prototype for accessing Struts
93  * through Spring's web dispatching infrastructure!
94  *
95  * @author Juergen Hoeller
96  * @since 1.1.1
97  * @see ServletForwardingController
98  * @see org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor
99  * @see org.springframework.orm.hibernate.support.OpenSessionInViewFilter
100  * @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor
101  * @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
102  */

103 public class ServletWrappingController extends AbstractController
104     implements BeanNameAware, InitializingBean, DisposableBean {
105
106     private Class JavaDoc servletClass;
107
108     private String JavaDoc servletName;
109
110     private Properties JavaDoc initParameters = new Properties JavaDoc();
111
112     private String JavaDoc beanName;
113
114     private Servlet JavaDoc servletInstance;
115
116
117     /**
118      * Set the class of the servlet to wrap.
119      * Needs to implement <code>javax.servlet.Servlet</code>.
120      * @see javax.servlet.Servlet
121      */

122     public void setServletClass(Class JavaDoc servletClass) {
123         this.servletClass = servletClass;
124     }
125
126     /**
127      * Set the name of the servlet to wrap.
128      * Default is the bean name of this controller.
129      */

130     public void setServletName(String JavaDoc servletName) {
131         this.servletName = servletName;
132     }
133
134     /**
135      * Specify init parameters for the servlet to wrap,
136      * as name-value pairs.
137      */

138     public void setInitParameters(Properties JavaDoc initParameters) {
139         this.initParameters = initParameters;
140     }
141
142     public void setBeanName(String JavaDoc name) {
143         this.beanName = name;
144     }
145
146
147     /**
148      * Initialize the wrapped Servlet instance.
149      * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
150      */

151     public void afterPropertiesSet() throws Exception JavaDoc {
152         if (this.servletClass == null) {
153             throw new IllegalArgumentException JavaDoc("servletClass is required");
154         }
155         if (!Servlet JavaDoc.class.isAssignableFrom(this.servletClass)) {
156             throw new IllegalArgumentException JavaDoc("servletClass [" + this.servletClass.getName() +
157                 "] needs to implement interface [javax.servlet.Servlet]");
158         }
159         if (this.servletName == null) {
160             this.servletName = this.beanName;
161         }
162         this.servletInstance = (Servlet JavaDoc) this.servletClass.newInstance();
163         this.servletInstance.init(new DelegatingServletConfig());
164     }
165
166
167     /**
168      * Invoke the the wrapped Servlet instance.
169      * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
170      */

171     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
172         throws Exception JavaDoc {
173
174         this.servletInstance.service(request, response);
175         return null;
176     }
177
178
179     /**
180      * Destroy the wrapped Servlet instance.
181      * @see javax.servlet.Servlet#destroy()
182      */

183     public void destroy() {
184         this.servletInstance.destroy();
185     }
186
187
188     /**
189      * Internal implementation of the ServletConfig interface, to be passed
190      * to the wrapped servlet. Delegates to ServletWrappingController fields
191      * and methods to provide init parameters and other environment info.
192      */

193     private class DelegatingServletConfig implements ServletConfig JavaDoc {
194
195         public String JavaDoc getServletName() {
196             return servletName;
197         }
198
199         public ServletContext JavaDoc getServletContext() {
200             return ServletWrappingController.this.getServletContext();
201         }
202
203         public String JavaDoc getInitParameter(String JavaDoc paramName) {
204             return initParameters.getProperty(paramName);
205         }
206
207         public Enumeration JavaDoc getInitParameterNames() {
208             return initParameters.keys();
209         }
210     }
211
212 }
213
Popular Tags