com.sun.grizzly
Class NIOContext

java.lang.Object
  extended by com.sun.grizzly.NIOContext
All Implemented Interfaces:
Context, AttributeHolder, Copyable

public class NIOContext
extends Object
implements Context

This Object is used to share information between the Grizzly Framework classes and ProtocolFilter implementation. Since Context is a pooled resource Controller#pollContext(java.nio.channels.SelectionKey) transactions using Context outside its ProtocolChain must invoke incrementRefCount() and Controller.returnContext(com.sun.grizzly.Context) to keep its pooling intact.

Author:
Jeanfrancois Arcand

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.sun.grizzly.Context
Context.AttributeScope, Context.KeyRegistrationState, Context.OpType
 
Field Summary
 
Fields inherited from interface com.sun.grizzly.Context
THROWABLE
 
Constructor Summary
NIOContext()
          Constructor
 
Method Summary
 void cancel()
          Cancel a suspend()ed Context.
 void configureOpType(SelectionKey key)
          Configure the currentOpType based on the SelectionKey.readyOps() values.
 void copyTo(Copyable copy)
          Copies current object content to copy object
 int decrementRefCount()
          Decrements the reference count of this Context.
 void execute()
          Deprecated.  
 void execute(ContextTask contextTask)
          Execute this Context using the Controller's thread pool
 void execute(ContextTask contextTask, boolean runInSeparateThread)
          Execute this Context using either Controller's thread pool or current thread
 AsyncQueueReadable getAsyncQueueReadable()
          Returns AsyncQueueReadable, assciated with the current Context.
 AsyncQueueReader getAsyncQueueReader()
          Return the
 AsyncQueueReaderContextTask getAsyncQueueReaderContextTask(AsyncQueueReader asyncQueueReader)
           
 AsyncQueueWritable getAsyncQueueWritable()
          Returns AsyncQueueWritable, assciated with the current Context.
protected  AsyncQueueWriter getAsyncQueueWriter()
          Return the
 AsyncQueueWriterContextTask getAsyncQueueWriterContextTask(AsyncQueueWriter asyncQueueWriter)
           
 Object getAttribute(String key)
          Return an object based on a key.
 AttributeHolder getAttributeHolderByScope(Context.AttributeScope scope)
          Return AttributeHolder, which corresponds to the given AttributeScope>
 Map<String,Object> getAttributes()
          Return a Map of attribute name/value pairs.
 CallbackHandlerContextTask getCallbackHandlerContextTask(CallbackHandler callbackHandler)
           
 Controller getController()
          Return the current Controller.
 Context.OpType getCurrentOpType()
          Get the current SelectionKey interest ops this instance is executing.
 IOEvent getIOEvent()
          Return the current IOEvent associated with this instance.
 Context.KeyRegistrationState getKeyRegistrationState()
          Return SelectionKey's next registration state.
 Controller.Protocol getProtocol()
          Return the current Controller#Protocol this instance is executing.
 ProtocolChain getProtocolChain()
          Return ProtocolChain executed by this instance.
 ProtocolChainContextTask getProtocolChainContextTask()
           
 ProtocolChainInstanceHandler getProtocolChainInstanceHandler()
          Return the ProtocolChainInstanceListener associated with this Context
 SelectionKey getSelectionKey()
          Return the current SelectionKey.
 SelectorHandler getSelectorHandler()
          Return the current SelectorHandler this instance is executing, or null if not supported.
 ExecutorService getThreadPool()
          Return the ExecutorService executing this instance.
 void incrementRefCount()
          Called by outer Threads that are not instances of WorkerThread to indicate that this Context should not be recycle() or offered back to its pool.
 boolean isSuspended()
          Return true if this Context has been suspended by invoking suspend.
 void recycle()
          Recycle this instance.
 Object removeAttribute(String key)
          Remove a key/value object.
 void resume()
          Resume a suspend()ed Context.
protected  void setAsyncQueueReader(AsyncQueueReader asyncQueueReader)
          Set the
protected  void setAsyncQueueWriter(AsyncQueueWriter asyncQueueWriter)
          Set the
 void setAttribute(String key, Object value)
          Set a key/value object.
 void setAttributes(Map<String,Object> attributes)
          Set a Map of attribute name/value pairs.
 void setController(Controller controller)
          Set the current Controller.
 void setCurrentOpType(Context.OpType currentOpType)
          Set the current OpType value.
 void setIOEvent(IOEvent<Context> ioEvent)
          Set an optional CallbackHandler.
 void setKeyRegistrationState(Context.KeyRegistrationState keyRegistrationState)
          Set the SelectionKey's next registration state
 void setProtocol(Controller.Protocol protocol)
           
 void setProtocolChain(ProtocolChain protocolChain)
          Set the ProtocolChain used by this Context.
 void setSelectionKey(SelectionKey key)
          Set the connection SelectionKey.
 void setSelectorHandler(SelectorHandler selectorHandler)
          Set the current SelectorHandler this instance is executing.
 void setThreadPool(ExecutorService threadPool)
          Set the ExecutorService that will execute this instance.
 void suspend()
          Suspend the execution of this Context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NIOContext

public NIOContext()
Constructor

Method Detail

copyTo

public void copyTo(Copyable copy)
Description copied from interface: Copyable
Copies current object content to copy object

Specified by:
copyTo in interface Copyable
Parameters:
copy - represents target object, where current object's content will be copied

getProtocolChainContextTask

public ProtocolChainContextTask getProtocolChainContextTask()

getCallbackHandlerContextTask

public CallbackHandlerContextTask getCallbackHandlerContextTask(CallbackHandler callbackHandler)

getAsyncQueueReaderContextTask

public AsyncQueueReaderContextTask getAsyncQueueReaderContextTask(AsyncQueueReader asyncQueueReader)

getAsyncQueueWriterContextTask

public AsyncQueueWriterContextTask getAsyncQueueWriterContextTask(AsyncQueueWriter asyncQueueWriter)

removeAttribute

public Object removeAttribute(String key)
Remove a key/value object.

Specified by:
removeAttribute in interface Context
Specified by:
removeAttribute in interface AttributeHolder
Parameters:
key - - name of an attribute
Returns:
attribute which has been removed

setAttribute

public void setAttribute(String key,
                         Object value)
Set a key/value object.

Specified by:
setAttribute in interface AttributeHolder
Parameters:
key - - name of an attribute
value - - value of named attribute

getAttribute

public Object getAttribute(String key)
Return an object based on a key.

Specified by:
getAttribute in interface AttributeHolder
Parameters:
key - - name of an attribute
Returns:
- attribute value for the key, null if key does not exist in attributes

getAttributeHolderByScope

public AttributeHolder getAttributeHolderByScope(Context.AttributeScope scope)
Return AttributeHolder, which corresponds to the given AttributeScope>

Specified by:
getAttributeHolderByScope in interface Context
Parameters:
scope - - AttributeScope>
Returns:
- AttributeHolder instance, which contains AttributeScope> attributes

setAttributes

public void setAttributes(Map<String,Object> attributes)
Set a Map of attribute name/value pairs. Old AttributeHolder values will not be available. Later changes of this Map will lead to changes to the current AttributeHolder.

Specified by:
setAttributes in interface Context
Specified by:
setAttributes in interface AttributeHolder
Parameters:
attributes - - map of name/value pairs

getAttributes

public Map<String,Object> getAttributes()
Return a Map of attribute name/value pairs. Updates, performed on the returned Map will be reflected in this AttributeHolder

Specified by:
getAttributes in interface Context
Specified by:
getAttributes in interface AttributeHolder
Returns:
- Map of attribute name/value pairs

getSelectionKey

public SelectionKey getSelectionKey()
Return the current SelectionKey.

Specified by:
getSelectionKey in interface Context
Returns:
- this Context's SelectionKey

setSelectionKey

public void setSelectionKey(SelectionKey key)
Set the connection SelectionKey.

Parameters:
key - - set this Context's SelectionKey

getController

public Controller getController()
Return the current Controller.

Specified by:
getController in interface Context
Returns:
- this Context's current Controller

setController

public void setController(Controller controller)
Set the current Controller.

Parameters:
{@link - Controller}

recycle

public void recycle()
Recycle this instance.

Specified by:
recycle in interface Context

getKeyRegistrationState

public Context.KeyRegistrationState getKeyRegistrationState()
Return SelectionKey's next registration state.

Specified by:
getKeyRegistrationState in interface Context
Returns:
this Context's SelectionKey registration state

setKeyRegistrationState

public void setKeyRegistrationState(Context.KeyRegistrationState keyRegistrationState)
Set the SelectionKey's next registration state

Specified by:
setKeyRegistrationState in interface Context
Parameters:
{@link - keyRegistrationState} - set this Context's SelectionKey registration state

getProtocolChain

public ProtocolChain getProtocolChain()
Return ProtocolChain executed by this instance.

Specified by:
getProtocolChain in interface Context
Returns:
ProtocolChain instance

setProtocolChain

public void setProtocolChain(ProtocolChain protocolChain)
Set the ProtocolChain used by this Context.

Specified by:
setProtocolChain in interface Context
Parameters:
protocolChain - instance of ProtocolChain to be used by the Context

getCurrentOpType

public Context.OpType getCurrentOpType()
Get the current SelectionKey interest ops this instance is executing.

Specified by:
getCurrentOpType in interface Context
Returns:
OpType the currentOpType.

setCurrentOpType

public void setCurrentOpType(Context.OpType currentOpType)
Set the current OpType value.

Parameters:
currentOpType - sets current operation type

configureOpType

public void configureOpType(SelectionKey key)
Configure the currentOpType based on the SelectionKey.readyOps() values.

Parameters:
key -

execute

public void execute()
Deprecated. 

Execute this Context using the Controller's thread pool

Specified by:
execute in interface Context

execute

public void execute(ContextTask contextTask)
Execute this Context using the Controller's thread pool

Specified by:
execute in interface Context
Parameters:
contextTask - ContextTask, which will be executed by ExecutorService

execute

public void execute(ContextTask contextTask,
                    boolean runInSeparateThread)
Execute this Context using either Controller's thread pool or current thread

Specified by:
execute in interface Context
Parameters:
contextTask - ContextTask, which will be executed by ExecutorService
runInSeparateThread - if true - ContextTask will be executed in separate thread, false - in current thread.

getProtocolChainInstanceHandler

public ProtocolChainInstanceHandler getProtocolChainInstanceHandler()
Return the ProtocolChainInstanceListener associated with this Context

Returns:
ProtocolChainInstanceListener

getThreadPool

public ExecutorService getThreadPool()
Return the ExecutorService executing this instance.

Specified by:
getThreadPool in interface Context
Returns:
ExecutorService

setThreadPool

public void setThreadPool(ExecutorService threadPool)
Set the ExecutorService that will execute this instance.

Specified by:
setThreadPool in interface Context
Parameters:
threadPool - the ExecutorService to set

setIOEvent

public void setIOEvent(IOEvent<Context> ioEvent)
Set an optional CallbackHandler.

Specified by:
setIOEvent in interface Context
Parameters:
ioEvent - the IOEvent to set

getIOEvent

public IOEvent getIOEvent()
Return the current IOEvent associated with this instance.

Specified by:
getIOEvent in interface Context
Returns:
IOEvent the current IOEvent associated with this instance.

getProtocol

public Controller.Protocol getProtocol()
Return the current Controller#Protocol this instance is executing.

Specified by:
getProtocol in interface Context
Returns:
the current Controller.Protocol this instance is executing.

setProtocol

public void setProtocol(Controller.Protocol protocol)
Parameters:
protocol - The current protocol.

getSelectorHandler

public SelectorHandler getSelectorHandler()
Description copied from interface: Context
Return the current SelectorHandler this instance is executing, or null if not supported.

Specified by:
getSelectorHandler in interface Context
Returns:
the current SelectorHandler this instance is executing.

setSelectorHandler

public void setSelectorHandler(SelectorHandler selectorHandler)
Set the current SelectorHandler this instance is executing.

Parameters:
selectorHandler - SelectorHandler

getAsyncQueueReadable

public AsyncQueueReadable getAsyncQueueReadable()
Returns AsyncQueueReadable, assciated with the current Context. This method is not threadsafe.

Returns:
AsyncQueueReadable

getAsyncQueueWritable

public AsyncQueueWritable getAsyncQueueWritable()
Returns AsyncQueueWritable, assciated with the current Context. This method is not threadsafe.

Specified by:
getAsyncQueueWritable in interface Context
Returns:
AsyncQueueWritable

getAsyncQueueReader

public AsyncQueueReader getAsyncQueueReader()
Return the

Specified by:
getAsyncQueueReader in interface Context
Returns:
the

setAsyncQueueReader

protected void setAsyncQueueReader(AsyncQueueReader asyncQueueReader)
Set the

Parameters:
asyncQueueReader -

getAsyncQueueWriter

protected AsyncQueueWriter getAsyncQueueWriter()
Return the

Returns:
the

setAsyncQueueWriter

protected void setAsyncQueueWriter(AsyncQueueWriter asyncQueueWriter)
Set the

Parameters:
asyncQueueWriter -

suspend

public void suspend()
Suspend the execution of this Context. Suspending the execution allow application to store the current instance, and re-use it later by not only the Thread used when called suspend, but also from any other Thread. A suspended Context will not be re-used by any other transaction and Thread. A suspended Context will keep its current state intact, meaning its SelectionKey, attributes, SelectorHandler, etc, will not change. Internally, The Context will not be recyled and will not be re-used by any Thread. When invoked this method will automatically set the Context.setKeyRegistrationState(com.sun.grizzly.Context.KeyRegistrationState) to KeyRegistrationState to KeyRegistrationState.NONE. Invoking this method many times as not effect once suspended.

Specified by:
suspend in interface Context

isSuspended

public boolean isSuspended()
Return true if this Context has been suspended by invoking suspend. When suspended, invoking Context.recycle() will throw an IllegalStateException

Specified by:
isSuspended in interface Context
Returns:
true if this Context has been suspended

resume

public void resume()
Resume a suspend()ed Context. Resume will not call Context.recycle(). So after the caller is finished using Context caller must call Controller.returnContext(com.sun.grizzly.Context) to mark it as a candidate for being re-used by another Thread and connection. Important. When resumed, all operations done on this object are not thread-safe and there is probability that another thread is already using this object. Never use this object once resumed. When invoked this method will automatically set the Context.setKeyRegistrationState(com.sun.grizzly.Context.KeyRegistrationState) to KeyRegistrationState to KeyRegistrationState.REGISTER and automatically re-enable read and write operations. If the Context hasn't been suspended, calling that method has no effet.

Specified by:
resume in interface Context

cancel

public void cancel()
Cancel a suspend()ed Context. Invoking this method will automatically clean the state of this Context and mark it as a candidate for being re-used by another Thread and connection. Important. When cancelled, all operations done on this object are not thread-safe and there is probability that another thread is already using this object. Never use this object once cancelled. When invoked this method will automatically close the underlying connection (represented by its SelectionKey. If the Context hasn't been suspended, calling that method has no effet.

Specified by:
cancel in interface Context

incrementRefCount

public void incrementRefCount()
Called by outer Threads that are not instances of WorkerThread to indicate that this Context should not be recycle() or offered back to its pool. When a outer Thread is done with Context it must call Controller#returnContext(com.sun.grizzly.Context) to ensure that {@link Context} will be properly recycled.

Specified by:
incrementRefCount in interface Context

decrementRefCount

public int decrementRefCount()
Decrements the reference count of this Context. Threads wanting to release Context should not call this method but instead use Controller.returnContext(com.sun.grizzly.Context)

Specified by:
decrementRefCount in interface Context
Returns:
return decremented reference count


Copyright © 2013 Oracle Corporation. All Rights Reserved.