1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.servlet;
18
19 import java.util.Enumeration;
20 import java.util.HashSet;
21 import java.util.Set;
22 import javax.servlet.ServletConfig;
23 import javax.servlet.ServletContext;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.springframework.beans.BeanWrapper;
31 import org.springframework.beans.BeansException;
32 import org.springframework.beans.MutablePropertyValues;
33 import org.springframework.beans.PropertyAccessorFactory;
34 import org.springframework.beans.PropertyValue;
35 import org.springframework.beans.PropertyValues;
36 import org.springframework.context.EnvironmentAware;
37 import org.springframework.core.env.ConfigurableEnvironment;
38 import org.springframework.core.env.Environment;
39 import org.springframework.core.env.EnvironmentCapable;
40 import org.springframework.core.io.Resource;
41 import org.springframework.core.io.ResourceEditor;
42 import org.springframework.core.io.ResourceLoader;
43 import org.springframework.util.Assert;
44 import org.springframework.util.StringUtils;
45 import org.springframework.web.context.support.ServletContextResourceLoader;
46 import org.springframework.web.context.support.StandardServletEnvironment;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 @SuppressWarnings("serial")
81 public abstract class HttpServletBean extends HttpServlet
82 implements EnvironmentCapable, EnvironmentAware {
83
84
85 protected final Log logger = LogFactory.getLog(getClass());
86
87
88
89
90
91 private final Set<String> requiredProperties = new HashSet<String>();
92
93 private ConfigurableEnvironment environment;
94
95
96
97
98
99
100
101
102
103
104
105 protected final void addRequiredProperty(String property) {
106 this.requiredProperties.add(property);
107 }
108
109
110
111
112
113
114
115 @Override
116 public final void init() throws ServletException {
117 if (logger.isDebugEnabled()) {
118 logger.debug("Initializing servlet '" + getServletName() + "'");
119 }
120
121
122 try {
123 PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
124 BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
125 ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
126 bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
127 initBeanWrapper(bw);
128 bw.setPropertyValues(pvs, true);
129 }
130 catch (BeansException ex) {
131 logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
132 throw ex;
133 }
134
135
136 initServletBean();
137
138 if (logger.isDebugEnabled()) {
139 logger.debug("Servlet '" + getServletName() + "' configured successfully");
140 }
141 }
142
143
144
145
146
147
148
149
150
151 protected void initBeanWrapper(BeanWrapper bw) throws BeansException {
152 }
153
154
155
156
157
158
159
160 @Override
161 public final String getServletName() {
162 return (getServletConfig() != null ? getServletConfig().getServletName() : null);
163 }
164
165
166
167
168
169
170 @Override
171 public final ServletContext getServletContext() {
172 return (getServletConfig() != null ? getServletConfig().getServletContext() : null);
173 }
174
175
176
177
178
179
180
181
182
183 protected void initServletBean() throws ServletException {
184 }
185
186
187
188
189
190
191 @Override
192 public void setEnvironment(Environment environment) {
193 Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
194 this.environment = (ConfigurableEnvironment) environment;
195 }
196
197
198
199
200
201
202 @Override
203 public ConfigurableEnvironment getEnvironment() {
204 if (this.environment == null) {
205 this.environment = this.createEnvironment();
206 }
207 return this.environment;
208 }
209
210
211
212
213
214 protected ConfigurableEnvironment createEnvironment() {
215 return new StandardServletEnvironment();
216 }
217
218
219
220
221
222 private static class ServletConfigPropertyValues extends MutablePropertyValues {
223
224
225
226
227
228
229
230
231 public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)
232 throws ServletException {
233
234 Set<String> missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ?
235 new HashSet<String>(requiredProperties) : null;
236
237 Enumeration<String> en = config.getInitParameterNames();
238 while (en.hasMoreElements()) {
239 String property = en.nextElement();
240 Object value = config.getInitParameter(property);
241 addPropertyValue(new PropertyValue(property, value));
242 if (missingProps != null) {
243 missingProps.remove(property);
244 }
245 }
246
247
248 if (missingProps != null && missingProps.size() > 0) {
249 throw new ServletException(
250 "Initialization from ServletConfig for servlet '" + config.getServletName() +
251 "' failed; the following required properties were missing: " +
252 StringUtils.collectionToDelimitedString(missingProps, ", "));
253 }
254 }
255 }
256
257 }