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