com.sun.grizzly.comet
Class CometContext<E>

java.lang.Object
  extended by com.sun.grizzly.comet.CometContext<E>
Direct Known Subclasses:
CometContext

public class CometContext<E>
extends Object

The main object used by CometHandler. The CometContext is always available for CometHandler and can be used to notify other CometHandler. Attributes can be added/removed the same way HttpServletSession is doing. It is not recommended to use attributes if this CometContext is not shared amongs multiple context path (uses HttpServletSession instead).

Author:
Jeanfrancois Arcand

Field Summary
protected  boolean blockingNotification
          true if the caller of CometContext.notify should block when notifying other CometHandler.
protected  boolean cancelled
          Is the CometContext instance been cancelled.
protected  int continuationType
          The CometContext continuationType.
protected  ConcurrentHashMap<CometHandler,SelectionKey> handlers
          The list of registered CometHandler
protected static String INVALID_COMET_HANDLER
          Generic error message
protected  NotificationHandler notificationHandler
          The default NotificationHandler.
 
Constructor Summary
CometContext(String contextPath, int continuationType)
          Create a new instance
 
Method Summary
protected  void addActiveCometTask(CometTask cometTask)
          Add a CometTask to the active list.
 void addAttribute(Object key, Object value)
          Add an attibute.
 int addCometHandler(CometHandler handler)
          Add a CometHandler.
 int addCometHandler(CometHandler handler, boolean completeExecution)
          Add a CometHandler.
protected static void addInProgressSelectionKey(SelectionKey key)
          Add a SelectionKey to the list of current operations.
 Object getAttribute(Object key)
          Retrive an attribute.
 CometHandler getCometHandler(int hashCode)
          Retrive a CometHandler using its hashKey;
protected  CometHandler getCometHandler(SelectionKey key)
          Retrive a CometHandler using its SelectionKey.
 String getContextPath()
          Get the context path associated with this instance.
 long getExpirationDelay()
          Return the long delay before a request is resumed.
 NotificationHandler getNotificationHandler()
          Return the associated NotificationHandler
protected  void initialize(SelectionKey key)
          Initialize the newly added CometHandler.
protected  void interrupt(CometTask task)
          Interrupt a CometHandler by invoking CometHandler.onInterrupt(com.sun.grizzly.comet.CometEvent)
 boolean isActive(CometHandler cometHandler)
          Return true if this CometHandler is still active, e.g.
 boolean isBlockingNotification()
          Return true if the invoker of notify() should block when notifying Comet Handlers.
protected  boolean isCancelled()
          Is this instance beeing cancelled by the CometSelector
protected  void notify(CometEvent event, int eventType, SelectionKey key)
          Notify all CometHandler.
 void notify(E attachment)
          Notify all CometHandler.
 void notify(E attachment, int eventType)
          Notify all CometHandler.
 void notify(E attachment, int eventType, int cometHandlerID)
          Notify a single CometHandler.
protected  void recycle()
          Recycle this object.
 boolean registerAsyncRead(CometHandler handler)
          Register for asynchronous read.
 boolean registerAsyncWrite(CometHandler handler)
          Register for asynchronous write.
 Object removeAttribute(Object key)
          Remove an attribute.
 void removeCometHandler(CometHandler handler)
          Remove a CometHandler.
 void removeCometHandler(int hashCode)
          Remove a CometHandler based on its hashcode.
protected static boolean removeInProgressSelectionKey(SelectionKey key)
          Remove a SelectionKey to the list of current operations.
protected  void resetSuspendIdleTimeout()
          Reset the current timestamp on a suspended connection.
 void resumeCometHandler(CometHandler handler)
          Resume the Comet request and remove it from the active CometHandler list.
protected  void resumeCometHandler(CometHandler handler, boolean remove)
          Resume the Comet request.
 void setBlockingNotification(boolean blockingNotification)
          Set to true if the invoker of notify() should block when notifying Comet Handlers.
protected  void setCancelled(boolean cancelled)
          Cancel this object or "uncancel".
protected  void setCometSelector(CometSelector cometSelector)
          Set the CometSelector associated with this instance.
 void setExpirationDelay(long expirationDelay)
          Set the long delay before a request is resumed.
 void setNotificationHandler(NotificationHandler notificationHandler)
          Set the current NotificationHandler
 String toString()
          Helper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

INVALID_COMET_HANDLER

protected static final String INVALID_COMET_HANDLER
Generic error message

See Also:
Constant Field Values

cancelled

protected boolean cancelled
Is the CometContext instance been cancelled.


handlers

protected ConcurrentHashMap<CometHandler,SelectionKey> handlers
The list of registered CometHandler


continuationType

protected int continuationType
The CometContext continuationType. See CometEngine


blockingNotification

protected boolean blockingNotification
true if the caller of CometContext.notify should block when notifying other CometHandler.


notificationHandler

protected NotificationHandler notificationHandler
The default NotificationHandler.

Constructor Detail

CometContext

public CometContext(String contextPath,
                    int continuationType)
Create a new instance

Parameters:
contextPath - the context path
type - when the Comet processing will happen (see CometEngine).
Method Detail

getContextPath

public String getContextPath()
Get the context path associated with this instance.

Returns:
contextPath the context path associated with this instance

addAttribute

public void addAttribute(Object key,
                         Object value)
Add an attibute.

Parameters:
key - the key
value - the value

getAttribute

public Object getAttribute(Object key)
Retrive an attribute.

Parameters:
key - the key
Returns:
Object the value.

removeAttribute

public Object removeAttribute(Object key)
Remove an attribute.

Parameters:
key - the key
Returns:
Object the value

addCometHandler

public int addCometHandler(CometHandler handler,
                           boolean completeExecution)
Add a CometHandler. Client of this method might make sure the CometHandler is removed when the CometHandler.onInterrupt is invoked.

Parameters:
handler - a new CometHandler
completeExecution - Add the Comethandler but don't block waiting for event.

addCometHandler

public int addCometHandler(CometHandler handler)
Add a CometHandler. Client on this method might make sure the CometHandler is removed when the CometHandler.onInterrupt is invoked.

Parameters:
handler - a new CometHandler

getCometHandler

public CometHandler getCometHandler(int hashCode)
Retrive a CometHandler using its hashKey;


getCometHandler

protected CometHandler getCometHandler(SelectionKey key)
Retrive a CometHandler using its SelectionKey. The SelectionKey is not exposed to the Comet API, hence this method must be protected.


notify

protected void notify(CometEvent event,
                      int eventType,
                      SelectionKey key)
               throws IOException
Notify all CometHandler. The attachment can be null. The type will determine which code>CometHandler method will be invoked:

 CometEvent.INTERRUPT -> CometHandler.onInterrupt
 CometEvent.NOTIFY -> CometHandler.onEvent
 CometEvent.INITIALIZE -> CometHandler.onInitialize
 CometEvent.TERMINATE -> CometHandler.onTerminate
 CometEvent.READ -> CometHandler.onEvent
 CometEvent.WRITE -> CometHandler.onEvent
 

Parameters:
attachment - An object shared amongst CometHandler.
type - The type of notification.
key - The SelectionKey associated with the CometHandler.
Throws:
IOException

removeCometHandler

public void removeCometHandler(CometHandler handler)
Remove a CometHandler. If the continuation (connection) associated with this CometHandler no longer have CometHandler associated to it, it will be resumed.


removeCometHandler

public void removeCometHandler(int hashCode)
Remove a CometHandler based on its hashcode.


resumeCometHandler

public void resumeCometHandler(CometHandler handler)
Resume the Comet request and remove it from the active CometHandler list. Once resumed, a CometHandler should never manipulate the HttpServletRequest or HttpServletResponse as those are recycled. If you cache them for later reuse by another thread there is a possibility to introduce corrupted responses next time a request is made.


resumeCometHandler

protected void resumeCometHandler(CometHandler handler,
                                  boolean remove)
Resume the Comet request.

Parameters:
handler - The CometHandler associated with the current continuation.
remove - true if the CometHandler needs to be removed.

isActive

public boolean isActive(CometHandler cometHandler)
Return true if this CometHandler is still active, e.g. there is still a continuation associated with it.


notify

public void notify(E attachment)
            throws IOException
Notify all CometHandler. The attachment can be null. All CometHandler.onEvent() will be invoked.

Parameters:
attachment - An object shared amongst CometHandler.
Throws:
IOException

notify

public void notify(E attachment,
                   int eventType,
                   int cometHandlerID)
            throws IOException
Notify a single CometHandler. The attachment can be null. The type will determine which code>CometHandler method will be invoked:

 CometEvent.INTERRUPT -> CometHandler.onInterrupt
 CometEvent.NOTIFY -> CometHandler.onEvent
 CometEvent.INITIALIZE -> CometHandler.onInitialize
 CometEvent.TERMINATE -> CometHandler.onTerminate
 CometEvent.READ -> CometHandler.onEvent
 

Parameters:
attachment - An object shared amongst CometHandler.
type - The type of notification.
cometHandlerID - Notify a single CometHandler.
Throws:
IOException

initialize

protected void initialize(SelectionKey key)
                   throws IOException
Initialize the newly added CometHandler.

Parameters:
attachment - An object shared amongst CometHandler.
type - The type of notification.
key - The SelectionKey representing the CometHandler.
Throws:
IOException

notify

public void notify(E attachment,
                   int eventType)
            throws IOException
Notify all CometHandler. The attachment can be null. The type will determine which code>CometHandler method will be invoked:

 CometEvent.INTERRUPT -> CometHandler.onInterrupt
 CometEvent.NOTIFY -> CometHandler.onEvent
 CometEvent.INITIALIZE -> CometHandler.onInitialize
 CometEvent.TERMINATE -> CometHandler.onTerminate
 CometEvent.READ -> CometHandler.onEvent
 

Parameters:
attachment - An object shared amongst CometHandler.
type - The type of notification.
Throws:
IOException

resetSuspendIdleTimeout

protected void resetSuspendIdleTimeout()
Reset the current timestamp on a suspended connection.


registerAsyncRead

public boolean registerAsyncRead(CometHandler handler)
Register for asynchronous read. If your client supports http pipelining, invoking this method might result in a state where your CometHandler is invoked with a CometRead that will read the next http request. In that case, it is strongly recommended to not use that method unless your CometHandler can handle the http request.


registerAsyncWrite

public boolean registerAsyncWrite(CometHandler handler)
Register for asynchronous write.


recycle

protected void recycle()
Recycle this object.


isCancelled

protected boolean isCancelled()
Is this instance beeing cancelled by the CometSelector

Returns:
boolean cancelled or not.

setCancelled

protected void setCancelled(boolean cancelled)
Cancel this object or "uncancel".

Parameters:
cancelled - true or false.

setCometSelector

protected void setCometSelector(CometSelector cometSelector)
Set the CometSelector associated with this instance.

Parameters:
CometSelector - the CometSelector associated with this instance.

toString

public String toString()
Helper.

Overrides:
toString in class Object

getExpirationDelay

public long getExpirationDelay()
Return the long delay before a request is resumed.

Returns:
long the long delay before a request is resumed.

setExpirationDelay

public void setExpirationDelay(long expirationDelay)
Set the long delay before a request is resumed.

Parameters:
long - the long delay before a request is resumed.

interrupt

protected void interrupt(CometTask task)
Interrupt a CometHandler by invoking CometHandler.onInterrupt(com.sun.grizzly.comet.CometEvent)


addActiveCometTask

protected void addActiveCometTask(CometTask cometTask)
Add a CometTask to the active list.

Parameters:
cometTask -

isBlockingNotification

public boolean isBlockingNotification()
Return true if the invoker of notify() should block when notifying Comet Handlers.


setBlockingNotification

public void setBlockingNotification(boolean blockingNotification)
Set to true if the invoker of notify() should block when notifying Comet Handlers.


setNotificationHandler

public void setNotificationHandler(NotificationHandler notificationHandler)
Set the current NotificationHandler

Parameters:
notificationHandler -

getNotificationHandler

public NotificationHandler getNotificationHandler()
Return the associated NotificationHandler

Returns:

addInProgressSelectionKey

protected static void addInProgressSelectionKey(SelectionKey key)
Add a SelectionKey to the list of current operations.

Parameters:
key -

removeInProgressSelectionKey

protected static boolean removeInProgressSelectionKey(SelectionKey key)
Remove a SelectionKey to the list of current operations.

Parameters:
key -
Returns:


Copyright © 2008 SUN Microsystems. All Rights Reserved.