Fields Summary |
---|
protected static final String | INVALID_COMET_HANDLERGeneric error message |
private static final Logger | loggerMain logger |
private ConcurrentHashMap | attributesAttributes placeholder. |
private String | contextPathThe context path associated with this instance. |
protected boolean | cancelledIs the CometContext instance been cancelled. |
private ConcurrentHashMap | handlersThe list of registered CometHandler |
private ConcurrentLinkedQueue | asyncTasksThe list of registered AsyncProcessorTask . This object
are mainly keeping the state of the Comet request. |
private CometSelector | cometSelectorThe CometSelector used to register SelectionKey
for upcoming bytes. |
protected int | continuationTypeThe CometContext continuationType. See CometEngine |
private long | expirationDelayThe default delay expiration before a CometContext 's
CometHandler are interrupted. |
private boolean | blockingNotificationtrue if the caller of CometContext.notify should block when
notifying other CometHandler. |
private NotificationHandler | notificationHandlerThe default NotificationHandler. |
Methods Summary |
---|
protected void | addAsyncProcessorTask(com.sun.enterprise.web.connector.grizzly.async.AsyncProcessorTask asyncTask)Add a AsyncProcessorTask .
asyncTasks.add(asyncTask);
|
public void | addAttribute(java.lang.Object key, java.lang.Object value)Add an attibute.
attributes.put(key,value);
|
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.
Long threadId = Thread.currentThread().getId();
SelectionKey key = CometEngine.getEngine().
activateContinuation(threadId, this,completeExecution);
if (key == null){
throw new
IllegalStateException("Grizzly Comet hasn't been registered");
}
if (handler == null){
throw new
IllegalStateException(INVALID_COMET_HANDLER);
}
if (!completeExecution){
handlers.putIfAbsent(handler,key);
} else {
handlers.putIfAbsent(handler,new SelectionKey() {
public void cancel() {
}
public SelectableChannel channel() {
throw new IllegalStateException();
}
public int interestOps() {
throw new IllegalStateException();
}
public SelectionKey interestOps(int ops) {
throw new IllegalStateException();
}
public boolean isValid() {
return true;
}
public int readyOps() {
throw new IllegalStateException();
}
public Selector selector() {
throw new IllegalStateException();
}
});
}
return handler.hashCode();
|
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.
return addCometHandler(handler,false);
|
private void | closeConnection(CometEvent event, java.nio.channels.SelectionKey key)Advise CometHandler the connection will be closed.
Iterator<CometHandler> iterator = handlers.keySet().iterator();
CometHandler handler;
while(iterator.hasNext()){
handler = iterator.next();
if ( handlers.get(handler).equals(key) ){
try{
handler.onInterrupt(event);
iterator.remove();
} catch (IOException ex){
logger.log(Level.WARNING,"Exception: ",ex);
}
break;
}
}
|
protected java.util.concurrent.ConcurrentLinkedQueue | getAsyncProcessorTask()Return the list of AsyncProcessorTask
return asyncTasks;
|
public java.lang.Object | getAttribute(java.lang.Object key)Retrive an attribute.
return attributes.get(key);
|
public CometHandler | getCometHandler(int hashCode)Retrive a CometHandler using its hashKey;
Iterator<CometHandler> iterator = handlers.keySet().iterator();
CometHandler cometHandler = null;
while (iterator.hasNext()){
cometHandler = iterator.next();
if ( cometHandler.hashCode() == hashCode ){
return cometHandler;
}
}
return null;
|
protected CometHandler | getCometHandler(java.nio.channels.SelectionKey key)Retrive a CometHandler using its SelectionKey. The
SelectionKey is not exposed to the Comet API, hence this
method must be protected.
Iterator<CometHandler> iterator = handlers.keySet().iterator();
CometHandler cometHandler = null;
while (iterator.hasNext()){
cometHandler = iterator.next();
if (handlers.get(cometHandler) == key){
return cometHandler;
}
}
return null;
|
protected CometSelector | getCometSelector()Get the CometSelector associated with this instance.
return cometSelector;
|
public java.lang.String | getContextPath()Get the context path associated with this instance.
return contextPath;
|
public long | getExpirationDelay()Return the long delay before a request is resumed.
return expirationDelay;
|
public NotificationHandler | getNotificationHandler()
return notificationHandler;
|
protected void | initialize(java.nio.channels.SelectionKey key)Initialize the newly added CometHandler .
CometEvent event = new CometEvent<E>();
event.setType(event.INITIALIZE);
event.setCometContext(this);
Iterator<CometHandler> iterator = handlers.keySet().iterator();
CometHandler cometHandler = null;
while(iterator.hasNext()){
cometHandler = iterator.next();
if(handlers.get(cometHandler).equals(key)){
cometHandler.onInitialize(event);
break;
}
}
|
protected void | interrupt(java.nio.channels.SelectionKey key)Invoke CometHandler.onTerminate
CometEvent event = new CometEvent<E>();
event.setType(CometEvent.INTERRUPT);
event.attach(null);
event.setCometContext(this);
closeConnection(event,key);
|
public boolean | isActive(CometHandler cometHandler)Return true if this CometHandler is still active, e.g. there is
still a continuation associated with it.
if (cometHandler == null){
throw new IllegalStateException(INVALID_COMET_HANDLER);
}
return (handlers.get(cometHandler) != null);
|
public boolean | isBlockingNotification()Return true if the invoker of notify() should block when
notifying Comet Handlers.
return blockingNotification;
|
protected boolean | isCancelled()Is this instance beeing cancelled by the CometSelector
return cancelled;
|
protected void | notify(CometEvent event, int eventType, java.nio.channels.SelectionKey key)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
CometHandler cometHandler = getCometHandler(key);
if (cometHandler == null){
throw new IllegalStateException(INVALID_COMET_HANDLER);
}
event.setCometContext(CometContext.this);
cometHandler.onEvent(event);
|
public void | notify(E attachment)Notify all CometHandler . The attachment can be null. All
CometHandler.onEvent() will be invoked.
CometEvent event = new CometEvent<E>();
event.setType(CometEvent.NOTIFY);
event.attach(attachment);
event.setCometContext(CometContext.this);
Iterator<CometHandler> iterator = handlers.keySet().iterator();
notificationHandler.setBlockingNotification(blockingNotification);
notificationHandler.notify(event,iterator);
registerKeys();
|
public void | notify(E attachment, int eventType, int cometHandlerID)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
CometHandler cometHandler = getCometHandler(cometHandlerID);
if (cometHandler == null){
throw new IllegalStateException(INVALID_COMET_HANDLER);
}
CometEvent event = new CometEvent<E>();
event.setType(eventType);
event.attach(attachment);
event.setCometContext(CometContext.this);
notificationHandler.setBlockingNotification(blockingNotification);
notificationHandler.notify(event,cometHandler);
if (event.getType() == CometEvent.TERMINATE
|| event.getType() == CometEvent.INTERRUPT) {
resumeCometHandler(cometHandler);
} else {
registerKeys();
}
|
public void | notify(E attachment, int eventType)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
// XXX Use a pool of CometEvent instance.
CometEvent event = new CometEvent<E>();
event.setType(eventType);
event.attach(attachment);
event.setCometContext(CometContext.this);
Iterator<CometHandler> iterator = handlers.keySet().iterator();
notificationHandler.setBlockingNotification(blockingNotification);
notificationHandler.notify(event,iterator);
if (event.getType() == CometEvent.TERMINATE
|| event.getType() == CometEvent.INTERRUPT) {
while(iterator.hasNext()){
resumeCometHandler(iterator.next());
}
} else {
registerKeys();
}
|
protected void | recycle()Recycle this object.
handlers.clear();
attributes.clear();
cancelled = false;
asyncTasks.clear();
|
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.
SelectionKey key = handlers.get(handler);
if (handler == null){
throw new
IllegalStateException(INVALID_COMET_HANDLER);
}
// Retrieve the CometSelector key.
SelectionKey cometKey = cometSelector.cometKeyFor(key.channel());
if (cometKey != null){
cometKey.interestOps(cometKey.interestOps() | SelectionKey.OP_READ);
if (cometKey.attachment() != null){
((CometTask)cometKey.attachment()).setAsyncReadSupported(true);
}
return true;
} else {
return false;
}
|
public boolean | registerAsyncWrite(CometHandler handler)Register for asynchronous write.
SelectionKey key = handlers.get(handler);
if (handler == null || key == null){
throw new
IllegalStateException(INVALID_COMET_HANDLER);
}
// Retrieve the CometSelector key.
SelectionKey cometKey = cometSelector.cometKeyFor(key.channel());
if (cometKey != null){
cometKey.interestOps(cometKey.interestOps() | SelectionKey.OP_WRITE);
return true;
} else {
return false;
}
|
private synchronized void | registerKeys()Register the current AsyncProcessorTask keys to the
CometSelector so new bytes can be processed.
CometTask cometTask;
for (AsyncProcessorTask asyncTask: asyncTasks){
SelectionKey key = asyncTask.getSelectionKey();
if (key == null) continue;
cometTask = (CometTask)key.attachment();
// Will hapens when a single CometHandler is invoked.
if (cometTask == null){
cometTask = CometEngine.getEngine().getCometTask(this,key);
key.attach(cometTask);
}
cometTask.setExpirationDelay(expirationDelay);
cometTask.setExpireTime(System.currentTimeMillis());
}
|
public java.lang.Object | removeAttribute(java.lang.Object key)Remove an attribute.
return attributes.remove(key);
|
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(handler,true);
|
private void | removeCometHandler(CometHandler handler, boolean resume)Remove a CometHandler . If the continuation (connection)
associated with this CometHandler no longer have
CometHandler associated to it, it will be resumed.
SelectionKey key = handlers.remove(handler);
if (resume && !handlers.containsValue(key)){
CometEngine.getEngine().resume(key,this);
}
|
public void | removeCometHandler(int hashCode)Remove a CometHandler based on its hashcode.
Iterator<CometHandler> iterator = handlers.keySet().iterator();
CometHandler cometHandler = null;
while (iterator.hasNext()){
cometHandler = iterator.next();
if ( cometHandler.hashCode() == hashCode ){
iterator.remove();
return;
}
}
|
public void | resumeCometHandler(CometHandler handler)Resume the Comet request and remove it from the active CometHandler list.
resumeCometHandler(handler,true);
|
protected void | resumeCometHandler(CometHandler handler, boolean remove)Resume the Comet request.
SelectionKey key = handlers.get(handler);
if (key == null){
throw new
IllegalStateException("Invalid CometHandler");
}
CometEngine.getEngine().resume(key,this);
if (remove){
removeCometHandler(handler,false);
}
|
public void | setBlockingNotification(boolean blockingNotification)Set to true if the invoker of notify() should block when
notifying Comet Handlers.
this.blockingNotification = blockingNotification;
|
protected void | setCancelled(boolean cancelled)Cancel this object or "uncancel".
this.cancelled = cancelled;
|
protected void | setCometSelector(CometSelector cometSelector)Set the CometSelector associated with this instance.
this.cometSelector = cometSelector;
|
public void | setExpirationDelay(long expirationDelay)Set the long delay before a request is resumed.
this.expirationDelay = expirationDelay;
|
public void | setNotificationHandler(NotificationHandler notificationHandler)
this.notificationHandler = notificationHandler;
|
public java.lang.String | toString()Helper.
return contextPath;
|