01: package com.icesoft.faces.webapp.http.servlet;
02:
03: import com.icesoft.faces.webapp.http.common.ResponseHandler;
04: import com.icesoft.faces.webapp.http.common.Server;
05: import edu.emory.mathcs.backport.java.util.concurrent.Semaphore;
06:
07: import javax.servlet.http.HttpServletRequest;
08: import javax.servlet.http.HttpServletResponse;
09:
10: public class ThreadBlockingAdaptingServlet implements PseudoServlet {
11: private Server server;
12:
13: public ThreadBlockingAdaptingServlet(Server server) {
14: this .server = server;
15: }
16:
17: public void service(HttpServletRequest request,
18: HttpServletResponse response) throws Exception {
19: ThreadBlockingRequestResponse requestResponse = new ThreadBlockingRequestResponse(
20: request, response);
21: server.service(requestResponse);
22: requestResponse.blockUntilRespond();
23: }
24:
25: public void shutdown() {
26: server.shutdown();
27: }
28:
29: private class ThreadBlockingRequestResponse extends
30: ServletRequestResponse {
31: private boolean blockResponse = true;
32: private Semaphore semaphore;
33:
34: public ThreadBlockingRequestResponse(
35: HttpServletRequest request, HttpServletResponse response)
36: throws Exception {
37: super (request, response);
38: }
39:
40: public void respondWith(ResponseHandler handler)
41: throws Exception {
42: super .respondWith(handler);
43: if (semaphore == null) {
44: blockResponse = false;
45: } else {
46: semaphore.release();
47: }
48: }
49:
50: public void blockUntilRespond() throws InterruptedException {
51: if (blockResponse) {
52: semaphore = new Semaphore(1);
53: semaphore.acquire();
54: semaphore.acquire();
55: semaphore.release();
56: }
57: }
58: }
59: }
|