FileDocCategorySizeDatePackage
AsyncWebProcessorTask.javaAPI DocGlassfish v2 API16342Fri May 04 22:36:56 BST 2007com.sun.grizzly.asyncweb

AsyncWebProcessorTask

public class AsyncWebProcessorTask extends com.sun.enterprise.web.connector.grizzly.TaskBase implements com.sun.enterprise.web.connector.grizzly.ProcessorTask
ProcessorTask that delegates the request/response handling to AsyncWeb's HttpIoHandler.
author
Jeanfrancois Arcand

Fields Summary
protected com.sun.enterprise.web.connector.grizzly.TaskContext
taskContext
The taskContext associated with this object. The taskContext contains information about the current connection.
protected com.sun.enterprise.web.connector.grizzly.TaskEvent
taskEvent
The TaskEvent associated with this task.
private int
requestBufferSize
Not used right since AsyncWeb has its own config file
private boolean
dropConnection
private com.sun.enterprise.web.connector.grizzly.Handler
handler
private int
maxHttpHeaderSize
private int
timeouts
private boolean
keepAlive
Keep-alive flag.
private boolean
error
Has erros occured?
private MinaIoSession
ioSession
The Mina IoSession used to bridge AsyncWeb with Grizzly.
private org.apache.mina.common.ByteBuffer
minaByteBuffer
Mina ByteBuffer implementation.
private boolean
forcedRead
Read as much bytes as we can before invoking AsyncWeb to avoid attaching to the SelectionKey.attach().
private org.safehaus.asyncweb.transport.nio.HttpIOHandler
httpIOHandler
The AsyncWeb entry point to handle request.
Constructors Summary
public AsyncWebProcessorTask()

    
    
      
        super();
    
Methods Summary
public voiddoTask()
Used when Grizzly ARP is enabled. XXX Not Yet supported.

        try {
            process(taskContext.getInputStream(),
                    taskContext.getOutputStream());
        } catch(Throwable ex){
            SelectorThread.logger().log(Level.FINE,
                    "processorTask.errorProcessingRequest", ex);
        } finally {
            terminateProcess();        
        }        
    
public intgetBufferSize()

        return requestBufferSize;
    
public booleangetDropConnection()

        return dropConnection;
    
public com.sun.enterprise.web.connector.grizzly.HandlergetHandler()

        return handler;
    
public intgetMaxPostSize()

         throw new UnsupportedOperationException();
    
public java.lang.StringgetRequestURI()

        throw new UnsupportedOperationException();
    
public longgetWorkerThreadID()

        throw new UnsupportedOperationException();
    
public voidinitialize()
Initialize the Mina session.

        ioSession = new MinaIoSession();
    
public voidinvokeAdapter()

         throw new UnsupportedOperationException();
    
public booleanisError()

        return error;
    
public booleanisKeepAlive()

        return keepAlive;
    
public voidparseRequest()

         throw new UnsupportedOperationException();
    
public booleanparseRequest(java.io.InputStream input, java.io.OutputStream output, boolean keptAlive)

         throw new UnsupportedOperationException();
    
public booleanparseRequest(java.nio.channels.spi.AbstractSelectableChannel abstractSelectableChannel, boolean b)

        return false;
    
public voidpostProcess()

         throw new UnsupportedOperationException();
    
public voidpostProcess(java.io.InputStream input, java.io.OutputStream output)

         throw new UnsupportedOperationException();
    
public voidpostProcess(java.nio.channels.spi.AbstractSelectableChannel abstractSelectableChannel)

    
public voidpostResponse()

         throw new UnsupportedOperationException();
    
public voidpreProcess()

         throw new UnsupportedOperationException();
    
public voidpreProcess(java.io.InputStream input, java.io.OutputStream output)

         throw new UnsupportedOperationException();
    
public voidpreProcess(java.nio.channels.spi.AbstractSelectableChannel abstractSelectableChannel)

    
public booleanprocess(java.nio.channels.spi.AbstractSelectableChannel abstractSelectableChannel)

        return false;
    
public booleanprocess(java.io.InputStream input, java.io.OutputStream output)
Delegate the request to AsyncWeb by simulating

          
        ByteBufferInputStream bbInputStream =
                (ByteBufferInputStream)input;
        java.nio.ByteBuffer byteBuffer = bbInputStream.getByteBuffer();
        
        if ( forcedRead ) {
            readAllBytes(byteBuffer);
        }
        byteBuffer.flip();
        
        httpIOHandler.sessionOpened(ioSession);    
        
        // We MUST avoid creating a wrapper on every request, but recycle the 
        // one used the previous transaction.
        minaByteBuffer = ByteBuffer.wrap(byteBuffer);
        httpIOHandler.dataRead(ioSession,minaByteBuffer);

        return keepAlive;
    
private voidreadAllBytes(java.nio.ByteBuffer byteBuffer)
Use the temporary Selector to try to loads as much as we can available bytes before delegating the request processing to AsyncWeb

        int count = 1;
        int byteRead = 0;
        Selector readSelector = null;
        SelectionKey tmpKey = null;

        try{
            SocketChannel socketChannel = (SocketChannel)key.channel();
            while (count > 0){
                count = socketChannel.read(byteBuffer);
                if ( count > 0 )
                    byteRead += count;
            }            

            if ( byteRead == 0 ){
                readSelector = SelectorFactory.getSelector();

                if ( readSelector == null ){
                    return;
                }
                count = 1;
                tmpKey = socketChannel
                        .register(readSelector,SelectionKey.OP_READ);               
                tmpKey.interestOps(tmpKey.interestOps() | SelectionKey.OP_READ);
                int code = readSelector.selectNow();
                tmpKey.interestOps(
                    tmpKey.interestOps() & (~SelectionKey.OP_READ));

                if ( code == 0 ){
                    return;
                }

                while (count > 0){
                    count = socketChannel.read(byteBuffer);
                    if ( count > 0 )
                        byteRead += count;                 
                }
            }
        } finally {
            if (tmpKey != null)
                tmpKey.cancel();

            if ( readSelector != null){
                // Bug 6403933
                try{
                    readSelector.selectNow();
                } catch (IOException ex){
                    ;
                }
                SelectorFactory.returnSelector(readSelector);
            }
        }
    
public voidrecycle()
Always called when the connection is closed.

        super.recycle();
        keepAlive = true;        
        
        httpIOHandler.sessionClosed(ioSession);
        ioSession.recycle();
    
public voidsetBufferSize(int requestBufferSize)

        this.requestBufferSize= requestBufferSize;
    
public voidsetDropConnection(boolean dropConnection)

        this.dropConnection = dropConnection;
    
public voidsetHandler(com.sun.enterprise.web.connector.grizzly.Handler handler)

        this.handler = handler;
    
public voidsetIoHandler(org.safehaus.asyncweb.transport.nio.HttpIOHandler httpIOHandler)
Set the HttpIoHandler.

        this.httpIOHandler = httpIOHandler;
    
public voidsetMaxHttpHeaderSize(int maxHttpHeaderSize)

        this.maxHttpHeaderSize = maxHttpHeaderSize;
    
public voidsetMaxPostSize(int mps)

         throw new UnsupportedOperationException();
    
public voidsetSocket(java.net.Socket socket)

    
public voidsetTimeout(int timeouts)

        this.timeouts = timeouts;
    
public voidtaskEvent(com.sun.enterprise.web.connector.grizzly.TaskEvent event)
Receive notifications from Task

        if ( event.getStatus() == TaskEvent.START) {
            taskContext = (TaskContext)event.attachement();
            if (  taskEvent == null ) {
                taskEvent = new TaskEvent<TaskContext>();
            }
            
            taskEvent.attach(taskContext);
        }
    
public voidterminateProcess()
Stop keep-aliving the connection.

        keepAlive = false;