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 org.mortbay.util.ajax.Continuation;
06: import org.mortbay.util.ajax.ContinuationSupport;
07:
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10: import java.util.HashMap;
11: import java.util.Map;
12:
13: public class ContinuationAdaptingServlet implements PseudoServlet {
14: private Map requests = new HashMap();
15: private Server server;
16:
17: public ContinuationAdaptingServlet(Server server) {
18: this .server = server;
19: }
20:
21: public void service(final HttpServletRequest request,
22: HttpServletResponse response) throws Exception {
23: if (requests.containsKey(request)) {
24: ResponseHandler handler = (ResponseHandler) requests
25: .remove(request);
26: handler.respond(new ServletRequestResponse(request,
27: response));
28: } else {
29: ContinuationRequestResponse requestResponse = new ContinuationRequestResponse(
30: request, response);
31: server.service(requestResponse);
32: requestResponse.captureContinuation();
33: }
34: }
35:
36: public void shutdown() {
37: server.shutdown();
38: }
39:
40: private class ContinuationRequestResponse extends
41: ServletRequestResponse {
42: private boolean captureContinuation = true;
43: private Continuation continuation;
44:
45: public ContinuationRequestResponse(HttpServletRequest request,
46: HttpServletResponse response) throws Exception {
47: super (request, response);
48: }
49:
50: public synchronized void respondWith(ResponseHandler handler)
51: throws Exception {
52: if (continuation == null) {
53: captureContinuation = false;
54: super .respondWith(handler);
55: } else {
56: continuation.resume();
57: requests.put(request, handler);
58: }
59: }
60:
61: public synchronized void captureContinuation() {
62: if (captureContinuation) {
63: continuation = ContinuationSupport.getContinuation(
64: request, this );
65: continuation.suspend(request.getSession()
66: .getMaxInactiveInterval() * 1000);
67: }
68: }
69: }
70: }
|