Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
 package com.sun.enterprise.web.connector.grizzly;
Read available data on a non blocking SocketChannel. StreamAlgorithm stategy will decide if more bytes are required or not. Once the StreamAlgorithm is ready, the ProcessorTask attached to this class will be executed.

Scott Oaks
Jean-Francois Arcand
 public class DefaultReadTask extends TaskBase implements ReadTask {
The time in milliseconds before this Task can stay idle. This is only used when the SelectionKey.attach() is used.
     private long idleTime = 30 * 1000; // 30 seconds
The TaskContext instance associated with this object. The TaskContext is initialized at startup and then recycled.
     protected TaskContext taskContext;
The TaskEvent instance used by this object to notify its listeners
     protected TaskEvent taskEvent;
The ByteBuffer used by this task to buffer the request stream.
     protected ByteBuffer byteBuffer;   
The ProcessorTask used by this class.
     protected ProcessorTask processorTask;
Max post size.
     protected int maxPostSize = 25 * 1024 * 1024;
The recycled OutputStream used by this buffer.
     protected ByteBufferInputStream inputStream;

The Algorithm used to parse the request and determine of the bytes has been all read from the SocketChannel
    protected StreamAlgorithm algorithm;
true only when another object has already read bytes from the channel.
    protected boolean bytesAvailable = false;

Is the ByteBuffer used by the ReadTask use direct ByteBuffer or not.
    protected boolean useDirectByteBuffer = true;
Create view ByteBuffer from another ByteBuffer
    protected boolean useByteBufferView = false;
    // ----------------------------------------------------- Constructor ----/
    public DefaultReadTask(){
    public void initialize(StreamAlgorithm algorithm,
                      boolean useDirectByteBufferboolean useByteBufferView){
         = ;    
        this. = algorithm;       
         = new ByteBufferInputStream();
        this. = useDirectByteBuffer;
        this. = useByteBufferView;
Force this task to always use the same ProcessorTask instance.
    public void attachProcessor(ProcessorTask processorTask){
        this. = processorTask;        
Set appropriate attribute on the ProcessorTask.
    protected void configureProcessorTask(){
        // Disable blocking keep-alive mechanism. Keep-Alive mechanism
        // will be managed by this class instead.
Return the ProcessorTask to the pool.
    public void detachProcessor(){
        if ( != null){
        // Notify listeners
        if (  != null ) {
            for (int i=.size()-1; i > -1; i--){
                if (  == null ) {
                     = new TaskEvent<ReadTask>();
        if ( &&  != null){
             = null;
Read data from the socket and process it using this thread, and only if the StreamAlgorith stategy determine no more bytes are are needed.
    public void doTask() throws IOException {   
        if (  == null) {
            WorkerThread workerThread = (WorkerThread)Thread.currentThread();
             = workerThread.getByteBuffer();
            if ( workerThread.getByteBuffer() == null){
                 = .allocate(,
Pull data from the socket and store it inside the ByteBuffer. The StreamAlgorithM implementation will take care of determining if we need to register again to the main Selector or execute the request using temporary Selector

    protected void doTask(ByteBuffer byteBuffer){
        int count = 0;
        Socket socket = null;
        SocketChannel socketChannel = null;
        boolean keepAlive = false;
        Exception exception = null;
        try {
            socketChannel = (SocketChannel).channel();
            socket = socketChannel.socket();
            int loop = 0;
            int bufferSize = 0;
            while ( socketChannel.isOpen() && ( || 
                    ((count => -1))){
                // Avoid calling the Selector.
                if ( count == 0 && !){
                    if (loop > 2){
                if (){
                    count = byteBuffer.position();
                 = false;
                byteBuffer = .preParse(byteBuffer);
                // try to predict which HTTP method we are processing
                if ( .parse(byteBuffer) ){ 
                    keepAlive = executeProcessorTask();
                    if (!keepAlive) {
                } else {
                    // We must call the Selector since we don't have all the 
                    // bytes
                    keepAlive = true;
        // Catch IO AND NIO exception
        } catch (IOException ex) {
            exception = ex;
        } catch (RuntimeException ex) {
            exception = ex;    
        } finally {                   

Evaluate if the SelectionKey needs to be registered to the main Selector
    protected void manageKeepAlive(boolean keepAlive,int count
            Exception exception){         
        // The key is invalid when the Task has been cancelled.
        if ( count == -1 || !.isValid() || exception != null ){
            keepAlive = false;
            if ( exception != null){
                // Make sure we have detached the processorTask
                if (SelectorThread.logger().isLoggable(.)){
                           "SocketChannel Read Exception:",exception);
        if (keepAlive) {    
Execute the ProcessorTask only if the request has been fully read. Guest the size of the request by using the content-type HTTP headers.

false if the request wasn't fully read by the channel. so we need to respin the key on the Selector.
    public boolean executeProcessorTask() throws IOException{                  
        boolean registerKey = false;
        if (SelectorThread.logger().isLoggable(.))
        if (  .getHandler() != null && .getHandler()
                .handle(null.) == . ){
            return true;
        // Get a processor task. If the processorTask != null, that means we
        // failed to load all the bytes in a single
        if ( == null){
        try {           
            // The socket might not have been read entirely and the parsing
            // will fail, so we need to respin another event.
            registerKey = .process(,null);           
        } catch (Exception e) {
        return registerKey;

Return this object to the pool
    protected void returnTask(){
        if () {
    public void taskEvent(TaskEvent event){
        if (event.getStatus() == . 
                || event.getStatus() == .){

Complete the processing.
    public void terminate(boolean keepAlive){          
        if ( !keepAlive ){
Clear the current state and make this object ready for another request.
    public void recycle(){
        if ( != null){ 
                final WorkerThread workerThread = 
                if (workerThread.getByteBuffer() == null){
            } catch (ClassCastException ex){
                // Avoid failling if the Grizzly extension doesn't support
                // the WorkerThread interface.
                if (SelectorThread.logger().isLoggable(.))
            } finally{
                 = .postParse();   
         = null;
         = null;
    // -------------------------------------------------------- TaskEvent ---// 

Cancel the SelectionKey and close its underlying SocketChannel. Add this Task to the Keep-Alive sub-system.
    protected void finishConnection(){
        if (SelectorThread.logger().isLoggable(.))
            if ( != null){
        } catch (IOException ioe){

Register the SelectionKey with the Selector
    public void registerKey(){
        if (.isValid()){            
            if (SelectorThread.logger().isLoggable(.))
    // -------------------------------------------------------- getter/setter--/

Return the associated ProcessorTask.

the associated ProcessorTask, null if not used.
        return ;
Return the underlying ByteBuffer used by this class.
    public ByteBuffer getByteBuffer(){
        if (  == null) {
        return ;

Set the underlying ByteBuffer used by this class.
    public void setByteBuffer(ByteBuffer byteBuffer){
        this. = byteBuffer;
If the attached byteBuffer was already filled, tell the Algorithm to re-use the bytes.
    public void setBytesAvailable(boolean bytesAvailable){
        this. = bytesAvailable;

Set the time in milliseconds this Task is allowed to be idle.
    public void setIdleTime(long idleTime){
        this. = idleTime;
Return the time in milliseconds this Task is allowed to be idle.
    public long getIdleTime(){
        return ;
New to GrepCode? Check out our FAQ X