KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > handler > SimpleServletPostProcessor


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.handler;
18
19 import java.util.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import javax.servlet.Servlet JavaDoc;
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26
27 import org.springframework.beans.BeansException;
28 import org.springframework.beans.factory.BeanInitializationException;
29 import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
30 import org.springframework.web.context.ServletConfigAware;
31 import org.springframework.web.context.ServletContextAware;
32
33 /**
34  * Bean post-processor that applies initialization and destruction callbacks
35  * to beans that implement the Servlet interface.
36  *
37  * <p>After initialization of the bean instance, the Servlet <code>init</code>
38  * method will be called with a ServletConfig that contains the bean name
39  * of the Servlet and the ServletContext that it is running in.
40  *
41  * <p>Before destruction of the bean instance, the Servlet <code>destroy</code>
42  * will be called.
43  *
44  * <p><b>Note that this post-processor does not support Servlet initialization
45  * parameters.</b> Bean instances that implement the Servlet interface are
46  * supposed to be configured like any other Spring bean, that is, through
47  * constructor arguments or bean properties.
48  *
49  * <p>For reuse of a Servlet implementation in a plain Servlet container and as
50  * a bean in a Spring context, consider deriving from Spring's HttpServletBean
51  * base class that applies Servlet initialization parameters as bean properties,
52  * supporting both initialization styles.
53  *
54  * <p><b>Alternatively, consider wrapping a Servlet with Spring's
55  * ServletWrappingController.</b> This is particularly appropriate for
56  * existing Servlet classes, allowing to specify Servlet initialization
57  * parameters etc.
58  *
59  * @author Juergen Hoeller
60  * @since 1.1.5
61  * @see javax.servlet.Servlet
62  * @see javax.servlet.ServletConfig
63  * @see SimpleServletHandlerAdapter
64  * @see org.springframework.web.servlet.HttpServletBean
65  * @see org.springframework.web.servlet.mvc.ServletWrappingController
66  */

67 public class SimpleServletPostProcessor implements
68         DestructionAwareBeanPostProcessor, ServletContextAware, ServletConfigAware {
69
70     private boolean useSharedServletConfig = true;
71
72     private ServletContext JavaDoc servletContext;
73
74     private ServletConfig JavaDoc servletConfig;
75
76
77     /**
78      * Set whether to use the shared ServletConfig object passed in
79      * through <code>setServletConfig</code>, if available.
80      * <p>Default is "true". Turn this setting to "false" to pass in
81      * a mock ServletConfig object with the bean name as servlet name,
82      * holding the current ServletContext.
83      * @see #setServletConfig
84      */

85     public void setUseSharedServletConfig(boolean useSharedServletConfig) {
86         this.useSharedServletConfig = useSharedServletConfig;
87     }
88
89     public void setServletContext(ServletContext JavaDoc servletContext) {
90         this.servletContext = servletContext;
91     }
92
93     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
94         this.servletConfig = servletConfig;
95     }
96
97
98     public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
99         return bean;
100     }
101
102     public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
103         if (bean instanceof Servlet JavaDoc) {
104             ServletConfig JavaDoc config = this.servletConfig;
105             if (config == null || !this.useSharedServletConfig) {
106                 config = new DelegatingServletConfig(beanName, this.servletContext);
107             }
108             try {
109                 ((Servlet JavaDoc) bean).init(config);
110             }
111             catch (ServletException JavaDoc ex) {
112                 throw new BeanInitializationException("Servlet.init threw exception", ex);
113             }
114         }
115         return bean;
116     }
117
118     public void postProcessBeforeDestruction(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
119         if (bean instanceof Servlet JavaDoc) {
120             ((Servlet JavaDoc) bean).destroy();
121         }
122     }
123
124
125     /**
126      * Internal implementation of the ServletConfig interface, to be passed
127      * to the wrapped servlet.
128      */

129     private static class DelegatingServletConfig implements ServletConfig JavaDoc {
130
131         private final String JavaDoc servletName;
132
133         private final ServletContext JavaDoc servletContext;
134
135         public DelegatingServletConfig(String JavaDoc servletName, ServletContext JavaDoc servletContext) {
136             this.servletName = servletName;
137             this.servletContext = servletContext;
138         }
139
140         public String JavaDoc getServletName() {
141             return servletName;
142         }
143
144         public ServletContext JavaDoc getServletContext() {
145             return servletContext;
146         }
147
148         public String JavaDoc getInitParameter(String JavaDoc paramName) {
149             return null;
150         }
151
152         public Enumeration JavaDoc getInitParameterNames() {
153             return Collections.enumeration(Collections.EMPTY_SET);
154         }
155     }
156
157 }
158
Popular Tags