|
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 |
369
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
|
|
77 |
| ServletException |
|
78 |
| { |
|
79 |
369
| 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 |
369
| protected void doService(HttpServletRequest request, HttpServletResponse response)
|
|
117 |
| throws IOException, ServletException |
|
118 |
| { |
|
119 |
369
| try
|
|
120 |
| { |
|
121 |
369
| _registry.setupThread();
|
|
122 |
| |
|
123 |
369
| _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 |
369
| _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 |
117
| public void init(ServletConfig config) throws ServletException
|
|
180 |
| { |
|
181 |
117
| String name = config.getServletName();
|
|
182 |
| |
|
183 |
117
| _registryKey = REGISTRY_KEY_PREFIX + name;
|
|
184 |
| |
|
185 |
117
| long startTime = System.currentTimeMillis();
|
|
186 |
117
| long elapsedToRegistry = 0;
|
|
187 |
| |
|
188 |
117
| super.init(config);
|
|
189 |
| |
|
190 |
117
| _resolver = createClassResolver();
|
|
191 |
| |
|
192 |
117
| try
|
|
193 |
| { |
|
194 |
117
| _registry = constructRegistry(config);
|
|
195 |
| |
|
196 |
117
| elapsedToRegistry = System.currentTimeMillis() - startTime;
|
|
197 |
| |
|
198 |
117
| initializeApplication();
|
|
199 |
| |
|
200 |
117
| 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 |
117
| long elapsedOverall = System.currentTimeMillis() - startTime;
|
|
210 |
| |
|
211 |
117
| LOG.info(TapestryMessages.servletInit(name, elapsedToRegistry, elapsedOverall));
|
|
212 |
| } |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
117
| protected ClassResolver createClassResolver()
|
|
226 |
| { |
|
227 |
117
| return new DefaultClassResolver();
|
|
228 |
| } |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
117
| protected Registry constructRegistry(ServletConfig config)
|
|
240 |
| { |
|
241 |
117
| ErrorHandler errorHandler = constructErrorHandler(config);
|
|
242 |
| |
|
243 |
117
| RegistryBuilder builder = new RegistryBuilder(errorHandler);
|
|
244 |
| |
|
245 |
117
| builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver));
|
|
246 |
| |
|
247 |
117
| String name = config.getServletName();
|
|
248 |
117
| ServletContext context = config.getServletContext();
|
|
249 |
| |
|
250 |
117
| addModuleIfExists(builder, context, "/WEB-INF/" + name + "/hivemodule.xml");
|
|
251 |
117
| addModuleIfExists(builder, context, "/WEB-INF/hivemodule.xml");
|
|
252 |
| |
|
253 |
117
| return builder.constructRegistry(Locale.getDefault());
|
|
254 |
| } |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
117
| protected ErrorHandler constructErrorHandler(ServletConfig config)
|
|
265 |
| { |
|
266 |
117
| return new StrictErrorHandler();
|
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
234
| protected void addModuleIfExists(RegistryBuilder builder, ServletContext context, String path)
|
|
277 |
| { |
|
278 |
234
| Resource r = new ContextResource(context, path);
|
|
279 |
| |
|
280 |
234
| if (r.getResourceURL() == null)
|
|
281 |
234
| return;
|
|
282 |
| |
|
283 |
0
| builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(_resolver, r));
|
|
284 |
| } |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
117
| protected void initializeApplication()
|
|
294 |
| { |
|
295 |
117
| ApplicationInitializer ai = (ApplicationInitializer) _registry.getService(
|
|
296 |
| "tapestry.init.MasterInitializer", |
|
297 |
| ApplicationInitializer.class); |
|
298 |
| |
|
299 |
117
| ai.initialize(this);
|
|
300 |
| |
|
301 |
117
| _registry.cleanupThread();
|
|
302 |
| |
|
303 |
117
| _requestServicer = (ServletRequestServicer) _registry.getService(
|
|
304 |
| "tapestry.request.ServletRequestServicer", |
|
305 |
| ServletRequestServicer.class); |
|
306 |
| } |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
117
| public void destroy()
|
|
314 |
| { |
|
315 |
117
| getServletContext().removeAttribute(_registryKey);
|
|
316 |
| |
|
317 |
117
| if (_registry != null)
|
|
318 |
| { |
|
319 |
117
| _registry.shutdown();
|
|
320 |
117
| _registry = null;
|
|
321 |
| } |
|
322 |
| } |
|
323 |
| } |