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.
  */
 
 /*
  *  Copyright 1999-2004 The Apache Software Foundation
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
  *  You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package com.sun.enterprise.web.connector.grizzly.blocking;
 
 
Blocking SocketServer implementation.

Author(s):
jean-Francois Arcand
 
 public class SelectorBlockingThread extends SelectorThread 
         implements SecureSelector<SSLImplementation>{ 
 
     protected SSLImplementation sslImplementation = null;

    
Is SSL enabled
 
     private boolean secure = false;
 
     private ServerSocketFactory factory;
   
    
initialized the endpoint by creating the ServerSocketChannel and by initializing the server socket.
 
     @Override
     public void initEndpoint() throws IOExceptionInstantiationException {
         SelectorThreadConfig.configure(this);
         
        initFileCacheFactory();
        initAlgorithm();
        initPipeline();
        initMonitoringLevel();
        
        setName("SelectorThread-" + getPort());
        
        try{
            if (getInet() == null) {
                setServerSocket(getServerSocketFactory().createSocket(getPort(),
                        getSsBackLog()));
            } else {
                setServerSocket(getServerSocketFactory().createSocket(getPort(), 
                        getSsBackLog(), getInet()));
            }
            getServerSocket().setReuseAddress(true);            
        } catch (SocketException ex){
            throw new BindException(ex.getMessage() + ": " + getPort());
        }
        
        
        setInitialized(true);   
        getLogger().log(.,"Initializing Grizzly Blocking Mode");  
        // Since NIO is not used, rely on Coyote for buffering the response.
        setBufferResponse(true);
    }
    
    
    
Create a pool of ReadBlockingTask
    private void initReadBlockingTask(int size){         
        for (int i=0; i < sizei++){
            getReadTasks().offer(newReadBlockingTask(false));
        }
    }

    
    
Create a ReadBlockingTask instance used with blocking socket.
    private ReadBlockingTask newReadBlockingTask(boolean initialize){
                                                        
        ReadBlockingTask task = new ReadBlockingTask();
        task.setSelectorThread(this);
        task.setPipeline(getProcessorPipeline());
 
        task.setRecycle(isRecycleTasks());
        task.attachProcessor(newProcessorTask(initialize));
        task.setPipelineStatistic(getPipelineStat());
        task.setSecure();
 
        return task;
    }
    
    
    
Return a ReadBlockingTask from the pool. If the pool is empty, create a new instance.
    protected ReadBlockingTask getReadBlockingTask(Socket socket){
        ReadBlockingTask task = null;
        if (isRecycleTasks()) {
            task = (ReadBlockingTask)getReadTasks().poll();
        }
               
        if (task == null){
            task = newReadBlockingTask(false);
        }   
        
        ProcessorTask processorTask = task.getProcessorTask(); 
        processorTask.setSocket(socket);
        
        return task;
    }    
        
    
    
Handle a blocking operation on the socket.
    private void handleConnection(Socket socketthrows IOException{
                
        if (isMonitoringEnabled()) {
            getPipelineStat().incrementTotalAcceptCount();
        }
        getReadBlockingTask(socket).execute();
    }   
    @Override
    protected void setSocketOptions(Socket socket){
        super.setSocketOptions(socket);
        ifgetKeepAliveTimeoutInSeconds() > 0){
            try{
                socket.setSoTimeoutgetKeepAliveTimeoutInSeconds() * 1000 );
            } catch (SocketException ex){
                .log(.,
                        "setSoTimeout exception ",ex);                
            }
        }
    }
    
    
    
Start the Acceptor Thread and wait for incoming connection, in a non blocking mode.
    @Override
    public void startEndpoint() throws IOExceptionInstantiationException {
        setRunning(true);
        
        rampUpProcessorTask();
        registerComponents();
        startPipelines();
        startListener();
    }    
    
    
Stop a blocking server Socket
    @Override
    public void stopEndpoint() {
        if (!isRunning()) {
            return;
        }
        setRunning(false);
        try {
            try{
                if ( getServerSocket() != null ) {
                    getServerSocket().close();
                }
            } catch (Throwable t){
                getLogger().log(.,
                        "selectorThread.closeSocketException"t);
            }
            unregisterComponents();
            clearTasks();
        } catch (Throwable t) {
            getLogger().log(.,"selectorThread.stopException"t);
        }
    }
    protected Socket acceptSocket() {
        if( !isRunning() || getServerSocket()==null ) return null;
        Socket socket = null;
    	try {
            if(getServerSocketFactory()==null) {
                socket = getServerSocketChannel().accept().socket();
            } else {
                socket = getServerSocketFactory().acceptSocket(getServerSocket());
            }
            if (null == socket) {
                getLogger().log(.,"selectorThread.acceptSocket");
            } else {
                if (!isRunning()) {
                    socket.close();  // rude, but unlikely!
                    socket = null;
                } else if (getServerSocketFactory() != null) {
                    getServerSocketFactory().initSocketsocket );
                }
            }
        } catch(InterruptedIOException iioe) {
            // normal part -- should happen regularly so
            // that the endpoint can release if the server
            // is shutdown.
        } catch (AccessControlException ace) {
            // When using the Java SecurityManager this exception
            // can be thrown if you are restricting access to the
            // socket with SocketPermission's.
            // Log the unauthorized access and continue
            getLogger().log(.,"selectorThread.wrongPermission",
                       new Object[]{getServerSocket(), ace});
        } catch (IOException e) {
            String msg = null;
            if (isRunning()) {
                getLogger().log(.,"selectorThread.shutdownException"
                           new Object[]{getServerSocket(), e});
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch(Throwable ex) {
                    getLogger().log(.,"selectorThread.shutdownException"
                               new Object[]{getServerSocket(), ex});
                }
                socket = null;
            }
            if( !isRunning() ) return null;
        } catch (Throwable t) {                                
            try{
                if (socket != null)
                    socket.close();
            } catch (IOException ex){
                ;
            }
            getLogger().log(.,
                       "selectorThread.errorOnRequest",
                       t);
        }
        return socket;
    }  
      
            
    
Start a blocking server Socket
    @Override
    protected void startListener(){
        Socket socket = null;
        while (isRunning()){
            socket = acceptSocket();
            if (socket == null) {
                continue;
            }
            
            try {
                handleConnection(socket);
            } catch (Throwable ex) {
                getLogger().log(.
                           "selectorThread.handleConnectionException",
                           ex);
                try {
                    socket.close();                   
                } catch (IOException ioe){
                    // Do nothing
                }
                continue;
            }
        }         
    }
    
        
    
Create ProcessorTask objects and configure it to be ready to proceed request.
    @Override
    protected DefaultProcessorTask newProcessorTask(boolean initialize){                                                      
        ProcessorBlockingTask task = new ProcessorBlockingTask(initialize);
        configureProcessorTask(task);
        if (){
            task.setSSLImplementation();
        }
        return task;        
    }

    
    
Return the ServerSocketFactory used when a blocking IO is enabled.
   
        return ;
    }

    
    
Set the ServerSocketFactory used when a blocking IO is enabled.
    public void setServerSocketFactory(ServerSocketFactory factory){
        this. = factory;
    }
    
    
    public void setSecure(boolean secure){
        this. = secure;
    }

    
    
Return the current SSLImplementation this Thread
        return ;
    }


    
Set the SSLImplementation used by this thread.It usually means HTTPS will be used.
    public void setSSLImplementationSSLImplementation sslImplementation) {
        this. = sslImplementation;
    }
    
    // --------------------------------------------------- Not used ----------//
    
    public String[] getEnabledCipherSuites() {
        return null;
    }
    public void setEnabledCipherSuites(String[] enabledCipherSuites) {
    }
    public String[] getEnabledProtocols() {
        return null;
    }
    public void setEnabledProtocols(String[] enabledProtocols) {
    }
    public boolean isClientMode() {
        return false;
    }
    public void setClientMode(boolean clientMode) {
    }
    public boolean isNeedClientAuth() {
        return false;
    }
    public void setNeedClientAuth(boolean needClientAuth) {
    }
    public boolean isWantClientAuth() {
        return false;
    }
    public void setWantClientAuth(boolean wantClientAuth) {
    }
New to GrepCode? Check out our FAQ X