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