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