1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry; |
16 | |
|
17 | |
import org.apache.commons.logging.Log; |
18 | |
import org.apache.commons.logging.LogFactory; |
19 | |
import org.apache.hivemind.ClassResolver; |
20 | |
import org.apache.hivemind.ErrorHandler; |
21 | |
import org.apache.hivemind.Registry; |
22 | |
import org.apache.hivemind.Resource; |
23 | |
import org.apache.hivemind.impl.DefaultClassResolver; |
24 | |
import org.apache.hivemind.impl.RegistryBuilder; |
25 | |
import org.apache.hivemind.impl.StrictErrorHandler; |
26 | |
import org.apache.hivemind.impl.XmlModuleDescriptorProvider; |
27 | |
import org.apache.hivemind.util.ContextResource; |
28 | |
import org.apache.tapestry.services.ApplicationInitializer; |
29 | |
import org.apache.tapestry.services.ServletRequestServicer; |
30 | |
import org.apache.tapestry.util.exception.ExceptionAnalyzer; |
31 | |
|
32 | |
import javax.servlet.ServletConfig; |
33 | |
import javax.servlet.ServletContext; |
34 | |
import javax.servlet.ServletException; |
35 | |
import javax.servlet.http.HttpServlet; |
36 | |
import javax.servlet.http.HttpServletRequest; |
37 | |
import javax.servlet.http.HttpServletResponse; |
38 | |
import java.io.IOException; |
39 | |
import java.util.Locale; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | public class ApplicationServlet extends HttpServlet |
54 | |
{ |
55 | |
private static final long serialVersionUID = -8046042689991538059L; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private static final String REGISTRY_KEY_PREFIX = "org.apache.tapestry.Registry:"; |
66 | |
|
67 | 0 | private static final Log LOG = LogFactory.getLog(ApplicationServlet.class); |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private ClassResolver _resolver; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
private String _registryKey; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
private Registry _registry; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
private ServletRequestServicer _requestServicer; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, |
101 | |
ServletException |
102 | |
{ |
103 | 0 | doService(request, response); |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
protected void doService(HttpServletRequest request, HttpServletResponse response) |
120 | |
throws IOException, ServletException |
121 | |
{ |
122 | |
try |
123 | |
{ |
124 | 0 | _registry.setupThread(); |
125 | |
|
126 | 0 | _requestServicer.service(request, response); |
127 | |
} |
128 | 0 | catch (ServletException ex) |
129 | |
{ |
130 | 0 | log("ServletException", ex); |
131 | |
|
132 | 0 | show(ex); |
133 | |
|
134 | |
|
135 | |
|
136 | 0 | throw ex; |
137 | |
} |
138 | 0 | catch (IOException ex) |
139 | |
{ |
140 | 0 | log("IOException", ex); |
141 | |
|
142 | 0 | show(ex); |
143 | |
|
144 | |
|
145 | |
|
146 | 0 | throw ex; |
147 | |
} |
148 | |
finally |
149 | |
{ |
150 | 0 | _registry.cleanupThread(); |
151 | 0 | } |
152 | 0 | } |
153 | |
|
154 | |
protected void show(Exception ex) |
155 | |
{ |
156 | 0 | System.err.println("\n\n**********************************************************\n\n"); |
157 | |
|
158 | 0 | new ExceptionAnalyzer().reportException(ex, System.err); |
159 | |
|
160 | 0 | System.err.println("\n**********************************************************\n"); |
161 | |
|
162 | 0 | } |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
169 | |
throws IOException, ServletException |
170 | |
{ |
171 | 0 | doService(request, response); |
172 | 0 | } |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public void init(ServletConfig config) throws ServletException |
183 | |
{ |
184 | 0 | String name = config.getServletName(); |
185 | |
|
186 | 0 | _registryKey = REGISTRY_KEY_PREFIX + name; |
187 | |
|
188 | 0 | long startTime = System.currentTimeMillis(); |
189 | 0 | long elapsedToRegistry = 0; |
190 | |
|
191 | 0 | super.init(config); |
192 | |
|
193 | 0 | _resolver = createClassResolver(); |
194 | |
|
195 | |
try |
196 | |
{ |
197 | 0 | _registry = constructRegistry(config); |
198 | |
|
199 | 0 | elapsedToRegistry = System.currentTimeMillis() - startTime; |
200 | |
|
201 | 0 | initializeApplication(); |
202 | |
|
203 | 0 | config.getServletContext().setAttribute(_registryKey, _registry); |
204 | |
} |
205 | 0 | catch (Exception ex) |
206 | |
{ |
207 | 0 | show(ex); |
208 | |
|
209 | 0 | throw new ServletException(TapestryMessages.servletInitFailure(ex), ex); |
210 | 0 | } |
211 | |
|
212 | 0 | long elapsedOverall = System.currentTimeMillis() - startTime; |
213 | |
|
214 | 0 | LOG.info(TapestryMessages.servletInit(name, elapsedToRegistry, elapsedOverall)); |
215 | 0 | } |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
protected ClassResolver createClassResolver() |
229 | |
{ |
230 | 0 | return new DefaultClassResolver(); |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
protected Registry constructRegistry(ServletConfig config) |
243 | |
{ |
244 | 0 | ErrorHandler errorHandler = constructErrorHandler(config); |
245 | |
|
246 | 0 | RegistryBuilder builder = new RegistryBuilder(errorHandler); |
247 | |
|
248 | 0 | builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver)); |
249 | |
|
250 | 0 | String name = config.getServletName(); |
251 | 0 | ServletContext context = config.getServletContext(); |
252 | |
|
253 | 0 | addModuleIfExists(builder, context, "/WEB-INF/" + name + "/hivemodule.xml"); |
254 | 0 | addModuleIfExists(builder, context, "/WEB-INF/hivemodule.xml"); |
255 | |
|
256 | 0 | return builder.constructRegistry(Locale.getDefault()); |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
protected ErrorHandler constructErrorHandler(ServletConfig config) |
268 | |
{ |
269 | 0 | return new StrictErrorHandler(); |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
protected void addModuleIfExists(RegistryBuilder builder, ServletContext context, String path) |
280 | |
{ |
281 | 0 | Resource r = new ContextResource(context, path); |
282 | |
|
283 | 0 | if (r.getResourceURL() == null) |
284 | 0 | return; |
285 | |
|
286 | 0 | builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver, r)); |
287 | 0 | } |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
protected void initializeApplication() |
297 | |
{ |
298 | 0 | ApplicationInitializer ai = (ApplicationInitializer) _registry.getService( |
299 | |
"tapestry.init.MasterInitializer", |
300 | |
ApplicationInitializer.class); |
301 | |
|
302 | 0 | ai.initialize(this); |
303 | |
|
304 | 0 | _registry.cleanupThread(); |
305 | |
|
306 | 0 | _requestServicer = (ServletRequestServicer) _registry.getService( |
307 | |
"tapestry.request.ServletRequestServicer", |
308 | |
ServletRequestServicer.class); |
309 | 0 | } |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public void destroy() |
317 | |
{ |
318 | 0 | getServletContext().removeAttribute(_registryKey); |
319 | |
|
320 | 0 | if (_registry != null) |
321 | |
{ |
322 | 0 | _registry.shutdown(); |
323 | 0 | _registry = null; |
324 | |
} |
325 | |
|
326 | 0 | super.destroy(); |
327 | 0 | } |
328 | |
} |