FileDocCategorySizeDatePackage
SelectorBlockingThread.javaAPI DocGlassfish v2 API13161Fri May 04 22:37:08 BST 2007com.sun.enterprise.web.connector.grizzly.blocking

SelectorBlockingThread

public class SelectorBlockingThread extends com.sun.enterprise.web.connector.grizzly.SelectorThread implements com.sun.enterprise.web.connector.grizzly.SecureSelector
Blocking SocketServer implementation.
author
jean-Francois Arcand

Fields Summary
protected org.apache.tomcat.util.net.SSLImplementation
sslImplementation
private boolean
secure
Is SSL enabled
private org.apache.tomcat.util.net.ServerSocketFactory
factory
Constructors Summary
Methods Summary
protected java.net.SocketacceptSocket()

        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(Level.WARNING,"selectorThread.acceptSocket");
            } else {
                if (!isRunning()) {
                    socket.close();  // rude, but unlikely!
                    socket = null;
                } else if (getServerSocketFactory() != null) {
                    getServerSocketFactory().initSocket( socket );
                }
            }
        } 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(Level.WARNING,"selectorThread.wrongPermission",
                       new Object[]{getServerSocket(), ace});
        } catch (IOException e) {

            String msg = null;

            if (isRunning()) {
                getLogger().log(Level.SEVERE,"selectorThread.shutdownException", 
                           new Object[]{getServerSocket(), e});
            }

            if (socket != null) {
                try {
                    socket.close();
                } catch(Throwable ex) {
                    getLogger().log(Level.SEVERE,"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(Level.FINE,
                       "selectorThread.errorOnRequest",
                       t);
        }

        return socket;
    
public java.lang.String[]getEnabledCipherSuites()

        return null;
    
public java.lang.String[]getEnabledProtocols()

        return null;
    
protected ReadBlockingTaskgetReadBlockingTask(java.net.Socket socket)
Return a ReadBlockingTask from the pool. If the pool is empty, create a new instance.

        ReadBlockingTask task = null;
        if (isRecycleTasks()) {
            task = (ReadBlockingTask)getReadTasks().poll();
        }
               
        if (task == null){
            task = newReadBlockingTask(false);
        }   
        
        ProcessorTask processorTask = task.getProcessorTask(); 
        processorTask.setSocket(socket);
        
        return task;
    
public org.apache.tomcat.util.net.SSLImplementationgetSSLImplementation()
Return the current SSLImplementation this Thread

        return sslImplementation;
    
public org.apache.tomcat.util.net.ServerSocketFactorygetServerSocketFactory()
Return the ServerSocketFactory used when a blocking IO is enabled.

        return factory;
    
private voidhandleConnection(java.net.Socket socket)
Handle a blocking operation on the socket.

                
        if (isMonitoringEnabled()) {
            getGlobalRequestProcessor().increaseCountOpenConnections();
            getPipelineStat().incrementTotalAcceptCount();
        }

        getReadBlockingTask(socket).execute();
    
public voidinitEndpoint()
initialized the endpoint by creating the ServerSocketChannel and by initializing the server socket.

   
                      
          
        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());
        }
        
        getServerSocket().setSoTimeout(getServerTimeout());
        initReadBlockingTask(getMinReadQueueLength());
        
        setInitialized(true);   
        getLogger().log(Level.FINE,"Initializing Grizzly Blocking Mode");            
    
private voidinitReadBlockingTask(int size)
Create a pool of ReadBlockingTask

         
        for (int i=0; i < size; i++){
            getReadTasks().offer(newReadBlockingTask(false));
        }
    
public booleanisClientMode()

        return false;
    
public booleanisNeedClientAuth()

        return false;
    
public booleanisWantClientAuth()

        return false;
    
protected com.sun.enterprise.web.connector.grizzly.DefaultProcessorTasknewProcessorTask(boolean initialize)
Create ProcessorTask objects and configure it to be ready to proceed request.

                                                      
        ProcessorBlockingTask task = new ProcessorBlockingTask(initialize);
        configureProcessorTask(task);
        task.setMaxKeepAliveRequests(getMaxKeepAliveRequests());
        if (secure){
            task.setSSLImplementation(sslImplementation);
        }
        return task;        
    
private ReadBlockingTasknewReadBlockingTask(boolean initialize)
Create a ReadBlockingTask instance used with blocking socket.

                                                        
        ReadBlockingTask task = new ReadBlockingTask();
        task.setSelectorThread(this);
        task.setPipeline(getProcessorPipeline());
 
        task.setRecycle(isRecycleTasks());
        task.attachProcessor(newProcessorTask(initialize));
        task.setPipelineStatistic(getPipelineStat());
        task.setSecure(secure);
 
        return task;
    
public voidsetClientMode(boolean clientMode)

    
public voidsetEnabledCipherSuites(java.lang.String[] enabledCipherSuites)

    
public voidsetEnabledProtocols(java.lang.String[] enabledProtocols)

    
public voidsetNeedClientAuth(boolean needClientAuth)

    
public voidsetSSLImplementation(org.apache.tomcat.util.net.SSLImplementation sslImplementation)
Set the SSLImplementation used by this thread.It usually means HTTPS will be used.

        this.sslImplementation = sslImplementation;
    
public voidsetSecure(boolean secure)

        this.secure = secure;
    
public voidsetServerSocketFactory(org.apache.tomcat.util.net.ServerSocketFactory factory)
Set the ServerSocketFactory used when a blocking IO is enabled.

        this.factory = factory;
    
protected voidsetSocketOptions(java.net.Socket socket)

        super.setSocketOptions(socket);
        if( getKeepAliveTimeoutInSeconds() > 0){
            try{
                socket.setSoTimeout( getKeepAliveTimeoutInSeconds() * 1000 );
            } catch (SocketException ex){
                logger.log(Level.WARNING,
                        "setSoTimeout exception ",ex);                
            }
        }
    
public voidsetWantClientAuth(boolean wantClientAuth)

    
public voidstartEndpoint()
Start the Acceptor Thread and wait for incoming connection, in a non blocking mode.

        setRunning(true);
        
        rampUpProcessorTask();
        registerComponents();

        startPipelines();
        startListener();
    
protected voidstartListener()
Start a blocking server Socket

        Socket socket = null;
        while (isRunning()){
            socket = acceptSocket();
            if (socket == null) {
                continue;
            }
            
            try {
                handleConnection(socket);
            } catch (Throwable ex) {
                getLogger().log(Level.FINE, 
                           "selectorThread.handleConnectionException",
                           ex);
                try {
                    socket.close();                   
                } catch (IOException ioe){
                    // Do nothing
                }
                continue;
            }
        }