FileDocCategorySizeDatePackage
CometdServlet.javaAPI DocGlassfish v2 API6332Fri May 04 22:36:58 BST 2007com.sun.grizzly.cometd.servlet

CometdServlet

public class CometdServlet extends HttpServlet
Simple CometdChat that route Cometd Request to the EventRouter.
author
Jeanfrancois Arcand
author
TAKAI, Naoto

Fields Summary
private static final long
DEFAULT_EXPIRATION_DELAY
private String
contextPath
All request to that context-path will be considered as cometd enabled.
private com.sun.grizzly.cometd.BayeuxCometHandler
bayeuxCometHandler
The Bayeux CometHandler implementation.
private com.sun.grizzly.cometd.EventRouter
eventRouter
The EventRouter used to route JSON message.
private boolean
initialized
Is the BayeuxCometHandler initialized and added to the Grizzly CometEngine.
Constructors Summary
public CometdServlet()

    
    
      
    
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        ; // Nothing
    
public voiddoPost(javax.servlet.http.HttpServletRequest hreq, javax.servlet.http.HttpServletResponse hres)

        
        CometEngine cometEngine = CometEngine.getEngine();
        CometContext cometContext = cometEngine.getCometContext(contextPath);
        
        // XXX Pool instances
        CometdRequest cometdReq = new CometdRequest<HttpServletRequest>(hreq){            
            public String[] getParameterValues(String s) {
                return request.getParameterValues(s);
            }
        };
        
        CometdResponse cometdRes = new CometdResponse<HttpServletResponse>(hres){            
            public void write(String s) throws IOException{
                response.getWriter().write(s);
            }

            public void flush() throws IOException{
                response.getWriter().flush();
            }

            public void setContentType(String s) {
                response.setContentType(s);
            }
        };        
        
        if (!initialized){
            synchronized(cometContext){
                if (!initialized){        
                    bayeuxCometHandler = new BayeuxCometHandler();
                    eventRouter = new EventRouterImpl(cometContext);                         
                    int mainHandlerHash = 
                            cometContext.addCometHandler(bayeuxCometHandler,true);
                    cometContext.addAttribute(BayeuxCometHandler.BAYEUX_COMET_HANDLER,
                                              mainHandlerHash);
                    initialized = true;
                }
            }
        }
        
        eventRouter.route(cometdReq,cometdRes);
    
public voidinit(javax.servlet.ServletConfig config)
Initialize the Servlet by creating the CometContext.

        super.init(config);
        
        contextPath = config.getInitParameter("contextPath");
        if (contextPath == null) {
            contextPath = config.getServletContext().getContextPath() + "/cometd";
        } 
        CometEngine cometEngine = CometEngine.getEngine();
        CometContext cometContext = cometEngine.register(contextPath);
        
        String expire = config.getInitParameter("expirationDelay");
        if (expire == null) {
            cometContext.setExpirationDelay(DEFAULT_EXPIRATION_DELAY);
        } else {
            cometContext.setExpirationDelay(Long.parseLong(expire));
        }
        cometContext.setBlockingNotification(true);
        cometContext.setNotificationHandler(new CometdNotificationHandler());