FileDocCategorySizeDatePackage
AsyncWebSelectorThread.javaAPI DocGlassfish v2 API13717Fri May 04 22:36:56 BST 2007com.sun.grizzly.asyncweb

AsyncWebSelectorThread

public class AsyncWebSelectorThread extends com.sun.enterprise.web.connector.grizzly.SelectorThread implements org.safehaus.asyncweb.transport.Transport
AsyncWeb Integration into Grizzly.
author
Dave Irving
author
Jeanfrancois Arcand

Fields Summary
protected static boolean
embeddedInGlassFish
Is Grizzly embedded in GlassFish.
private static final String
CONFIG_PROPERTY
private static final String
SERVICE_CONFIG_PROPERTY
private static final String
DEFAULT_SERVICE_CONFIG
private static final String
SERVICE_NAME
The default context for receiving requests.
private org.safehaus.asyncweb.container.ServiceContainer
container
The BasicServiceContainer which handle non blocking request.
private org.safehaus.asyncweb.transport.nio.HttpIOHandler
httpIOHandler
The default IoHandler
Constructors Summary
public AsyncWebSelectorThread()
SelectorThread implementation.

  
    
           
      
        super();
        algorithmClassName = AsyncWebStreamAlgorithm.class.getName();
    
Methods Summary
private voidconfigureAsyncWebRuntime()
Builds an async web container - adding transport and services

return
The built container

        String configDir = null;
        
        if ( embeddedInGlassFish ){
            configDir ="file://" 
                + System.getProperty("com.sun.aas.instanceRoot") 
                + File.separator + "config";
        } else if ( rootFolder != null){
            try{
                 configDir ="file://" 
                    + new File(rootFolder).getCanonicalPath() 
                    + File.separator + "conf";     
            } catch (IOException ex){
                logger.log(Level.SEVERE,"Config error", ex);
            }
        } else {
            throw new IllegalStateException("rootFolder cannot be null");
        }
        
        String[] configs = new String[] { configDir + File.separator
                                  + "AsyncWeb.xml", configDir
                                  + File.separator
                                  + DEFAULT_SERVICE_CONFIG
                                  + File.separator + "*.xml"};
        ApplicationContext ctx = new FileSystemXmlApplicationContext(configs);
        container = (ServiceContainer) ctx.getBean("container");
        container.addTransport(this);
        HttpServiceHandler httpServiceHandler = 
                (HttpServiceHandler)ctx.getBean("httpServiceHandler");
        
        HttpService rooService = new RootAsyncService();
        httpServiceHandler.addHttpService(SERVICE_NAME, rooService);            
    
protected voidexpireIdleKeys()
Cancel keep-alive connections.

        if ( keepAliveTimeoutInSeconds <= 0 || !selector.isOpen()) return;
        long current = System.currentTimeMillis();

        if (current < getNextKeysExpiration()) {
            return;
        }
        setNextKeysExpiration(current + getKaTimeout());
        
        Set<SelectionKey> readyKeys = selector.keys();
        if (readyKeys.isEmpty()){
            return;
        }
        Iterator<SelectionKey> iterator = readyKeys.iterator();
        SelectionKey key;
        while (iterator.hasNext()) {
            key = iterator.next();
            if ( !key.isValid() ) {
                keepAlivePipeline.untrap(key); 
                continue;
            }  
                        
            // Keep-alive expired
            if ( key.attachment() != null ) {   
                long expire = 0L;
                if (key.attachment() instanceof Long){
                   expire = (Long) key.attachment(); 
                } else {
                   expire = (Long) ((ReadTask)key.attachment()).getIdleTime();
                }
                
                if (current - expire >= getKaTimeout()) {
                    cancelKey(key);
                } else if (expire + getKaTimeout() < getNextKeysExpiration()){
                     setNextKeysExpiration(expire + getKaTimeout());
                }
            }
        }                    
    
public voidinitEndpoint()
Init the AsyncWeb runtime and this SelectorThread

        configureAsyncWebRuntime();
        httpIOHandler = new HttpIOHandler();
        httpIOHandler.setContainer(container);
        super.initEndpoint();
    
public com.sun.enterprise.web.connector.grizzly.ProcessorTasknewProcessorTask(boolean initialize)
Creates a new AsyncWebProcessorTask and configure it to delegate AsyncWeb requests to its associated HttpIoHandler

return
ProcessorTask an instance of AsyncWebProcessorTask

        AsyncWebProcessorTask task = new AsyncWebProcessorTask();
        task.setMaxHttpHeaderSize(maxHttpHeaderSize);
        task.setBufferSize(requestBufferSize);
        task.setSelectorThread(this);              
        task.setRecycle(recycleTasks);
        task.setIoHandler(httpIOHandler);
        
        task.initialize();
 
        if ( keepAlivePipeline.dropConnection() ) {
            task.setDropConnection(true);
        }    
        task.setPipeline(processorPipeline); 
        return task;
    
public voidsetServiceContainer(org.safehaus.asyncweb.container.ServiceContainer container)
Set the ServiceContainer used by this SelectorThread

        this.container = container;
    
public voidstartEndpoint()
Start the SelectorThread

        super.startEndpoint();
        try{
            container.start();
        } catch (Exception ex){
            logger.log(Level.SEVERE,"AsyncWeb startup exception.", ex);
            throw new IOException(ex.getMessage());
        }
    
public voidstoptEndpoint()
Stop the SelectorThread

        try{
            container.stop();
        } catch (Exception ex){
            logger.log(Level.SEVERE,"AsyncWeb stop exception.", ex);
            throw new IOException(ex.getMessage());
        }
        super.stopEndpoint();