Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   * 
   * 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 https://glassfish.dev.java.net/public/CDDL+GPL.html
  * 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.ssl;
 
Asynchronous SSL support over NIO. This Task handles the SSL requests using a non blocking socket. The SSL handshake is done using this class. Once the handshake is successful, the SSLProcessorTask is executed.

Author(s):
Jean-Francois Arcand
 
 public class SSLAsyncReadTask extends SSLReadTask {  
    
    
Allocate themandatory ByteBuffers. Since the ByteBuffer are maintaned on the SSLWorkerThread lazily, this method makes sure the ByteBuffers are properly allocated and configured.
 
     @Override
     public void allocateBuffers(){       
         int expectedSize = .getSession().getPacketBufferSize();
         if ( < expectedSize){
              = expectedSize;
         }
 
         if ( != null && .capacity() < ) {
             ByteBuffer newBB = ByteBuffer.allocate();
             .flip();
             newBB.put();
              = newBB;                                
         } else if ( == null){
              = ByteBuffer.allocate();
         }      
         
          = ByteBuffer.allocate();       
         if ( == null){
              = ByteBuffer.allocate( * 2);
         }
 
         expectedSize = .getSession().getApplicationBufferSize();
         if (expectedSize > .capacity()) {
             ByteBuffer newBB = ByteBuffer.allocate(expectedSize);
             .flip();
             newBB.put();
              = newBB;
         }      
         .position(0);
         .limit(0); 
     }   
    
    
    
Initialize this object.
    @Override
    public void initialize(StreamAlgorithm algorithm,
                      boolean useDirectByteBufferboolean useByteBufferView){
         = ;    
        this. = algorithm;       
         = new SSLAsyncStream();
        
        this. = useDirectByteBuffer;
        this. = useByteBufferView
    }
   
    
    
Manage the SelectionKey
    @Override
    protected void manageKeepAlive(boolean keepAlive,int count
            Exception exception){         
        // The key is invalid when the Task has been cancelled.
        if (count == -1 || exception != null){
            if ( exception != null){
                // Make sure we have detached the processorTask
                detachProcessor();
                if (SelectorThread.logger().isLoggable(.)){
                    SelectorThread.logger().log
                       (.
                           "SocketChannel Read Exception:",exception);
                }
            }
            terminate(false);
        }
    }
    
    
    @Override
    protected boolean process() throws IOException{
        boolean keepAlive = false;     
        SocketChannel socketChannel = (SocketChannel).channel();
        Socket socket = socketChannel.socket();
        .setSocketChannel(socketChannel);    
        ((SSLAsyncStream)).setInputBB();        
                        
        // Get a processor task. If the processorTask != null, that means we
        // failed to load all the bytes in a single channel.read().
        if ( == null){
        } 
        
        // Always true with the NoParsingAlgorithm
        if (.parse()){ 
            return executeProcessorTask();
        } else {
            // Never happens with the default StreamAlgorithm
            return true;
        }
    }
  
    
    
Execute the ProcessorTask

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{                  
        if (SelectorThread.logger().isLoggable(.))
            SelectorThread.logger().log(.,"executeProcessorTask");
        
        if (.getHandler() != null && .getHandler()
                .handle(null,.) == .){
            return true;
        }
             
        if ( == null){
             = new TaskContext();
             = new TaskEvent<TaskContext>();
        }
        
        // Call the listener and execute the task on it's own pipeline.
        .setStatus(.);        
        .attach();
        fireTaskEvent(); 
        return false;
    }
    
    
    
Clear the current state and make this object ready for another request.
    @Override
    public void recycle(){
         = .postParse();   
        .clear(); 
        .recycle();
        .recycle();
         = null;
        .setSelectionKey(null);           
         = true;
        
        .clear();
        .clear();
        .position(0);
        .limit(0);             
         = null;
        detachProcessor();
    }
        
             
    
Complete the transaction.
    @Override
    public void terminate(boolean keepAlive){     
        if ( != null && .isKeepAlive()){
            detachProcessor();        
            registerKey();         
            returnTask();
        } else {
            super.terminate(keepAlive);    
        }
    }
    
    
    
Set appropriate attribute on the ProcessorTask.
    @Override
    public void configureProcessorTask(){
        super.configureProcessorTask();
        if ( !getTaskListeners().contains() ){
            .addTaskListener(this);
            addTaskListener((TaskListener));
        }
        SSLAsyncOutputBuffer outputBuffer = 
                ((SSLAsyncProcessorTask)).getSSLAsyncOutputBuffer();
        if (outputBuffer == null) {
            .initialize();
            outputBuffer = ((SSLAsyncProcessorTask)).getSSLAsyncOutputBuffer();
        }
        outputBuffer.setSSLEngine();
        outputBuffer.setOutputBB();
    } 
New to GrepCode? Check out our FAQ X