Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. 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
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/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 packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [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.

Author(s):
Scott Oaks
Jean-Francois Arcand
 
 public class XAReadTask extends DefaultReadTask {
    
    
If the last request was processed successfully and we need to keep-alive the connection, unattach thsi object from the SelectionKey and return it to the pool.
 
     protected boolean inKeepAliveProcess = false;


    
Read data from the socket and process it using this thread, and only if the StreamAlgorith stategy determine no more bytes are are needed.
 
     @Override
     public void doTask() throws IOException {
         int count = 0;
         Socket socket = null;
         SocketChannel socketChannel = null;
         boolean keepAlive = false;
         Exception exception = null;
         .attach(null);
 
         if (  == null ){
              = 
                     .allocate(,
                               ,.getBufferSize());
         }
         
         try {
              = true;
             socketChannel = (SocketChannel).channel();
             socket = socketChannel.socket();
             .setSocketChannel(socketChannel);
             
             int loop = 0;
             int bufferSize = 0;
             while ( socketChannel.isOpen() && ( ||
                     ((count = socketChannel.read())> -1))){
                 
                 // Avoid calling the Selector.
                 if ( count == 0 && !){
                    loop++;
                    if (loop > 2){
                        break;
                    }
                    continue;
                } 
                if (){
                    count = .position();
                }                
                 = false;
                
                 = .preParse();
                .setByteBuffer();
                .setSelectionKey();
                
                // try to predict which HTTP method we are processing
                if ( .parse() ){
                    keepAlive = executeProcessorTask();
                    if (!keepAlive) {
                         = false;
                        break;
                    }
                } else {
                    // We must call the Selector since we don't have all the
                    // bytes
                    keepAlive = true;
                    // We should not detach this task from the SelectionKey
                    // since the bytes weren't all read.
                     = false;
                    break;
                }
                
                // If the content-length is found, and if it higher than
                // maxPostSize, cancel the task to avoid DoS.
                if ( .contentLength() >  ){
                    cancelTask("Maximum POST size reached: " + ,
                            .);
                    keepAlive = false;
                    break;
                }
            }
            // Catch IO AND NIO exception
        } catch (IOException ex) {
            exception = ex;
        } catch (RuntimeException ex) {
            exception = ex;
        } finally {
            manageKeepAlive(keepAlive,count,exception);
        }
    }
    
    
    /*
     * Two scenarios:
     *  - inKeepAliveProcess = false
     *    the bytes aren't fully read, so we must register again
     *  - inKeepAliveProcess = true
     *    we keep-alive the key, but unattach this object from
     *    the SelectionKey so it can be resused.
     */
    @Override
    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 ){
             = false;
            keepAlive = false;
            
            if ( exception != null){
                // Make sure we have detached the processorTask
                detachProcessor();
                SelectorThread.logger().log(.
                        "SocketChannel Read Exception: ",exception);
            }
        }
        
        final boolean attached = !;
        if (keepAlive) {
            // (1) First remove the attachement.
            if (  ) {
                .attach(null);
            } else {
                .attach(this);
            }
            
            // (2) Register the key
            registerKey();
            
            // We must return since the key has been registered and this
            // task can be reused.
            if ( attached ) return;
          
            // (3) Return that task to the pool.
            if (  ) {
                terminate(keepAlive);
            }
        } else {
            terminate(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.

Returns:
false if the request wasn't fully read by the channel. so we need to respin the key on the Selector.
    @Override
    public boolean executeProcessorTask() throws IOException{
        boolean registerKey = false;
        
        if (SelectorThread.logger().isLoggable(.))
            SelectorThread.logger().log(.,"executeProcessorTask");
        
        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 channel.read().
        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) {
            SelectorThread.logger().log(.,"readTask.processException"e);
            registerKey = true;
        } finally {
            // if registerKey, that means we were'nt able to parse the request
            // properly because the bytes were not all read, so we need to
            // call again the Selector.
            if (registerKey && .isError()) {
                 = .rollbackParseState();
                 = false;
                return registerKey;
            }
        }
        detachProcessor();
        return registerKey;
    }
    
    
    
Complete the processing.
    @Override
    public void terminate(boolean keepAlive){     
        super.terminate(keepAlive);
    }    
    
    
Clear the current state and make this object ready for another request.
    @Override
    public void recycle(){
         = .postParse();   
        .clear(); 
        .recycle();
        .recycle();
         = null;
        .setSelectionKey(null);                             
    }    
    
    
New to GrepCode? Check out our FAQ X