FileDocCategorySizeDatePackage
PortUnificationPipeline.javaAPI DocGlassfish v2 API21249Thu Jun 07 10:31:50 BST 2007com.sun.enterprise.web.portunif

PortUnificationPipeline

public class PortUnificationPipeline extends com.sun.enterprise.web.connector.grizzly.ssl.SSLPipeline
This Pipeline goal is to try to determine the TCP protocol used (http, soap, https, etc.). First, all ProtocolFinder are executed trying to determine which protocol is used. Once the protocol is found, the associated ProtocolHandler. You can add ProtocolHandler and ProtocolFinder by doing (from a SelectorThread) SelectorThread st = SelectorThread.getSelector(port); PortUnificationPipeline php = (PortUnificationPipeline)st.getReadPipeline(); php.addProtocolFinder(..); php.addProtocolHandler(...);
author
Jeanfrancois Arcand

Fields Summary
public static final String
PROTOCOL_FINDERS
public static final String
PROTOCOL_HANDLERS
private ConcurrentHashMap
protocolHandlers
List of available ProtocolHandler.
private ConcurrentLinkedQueue
protocolFinders
List of available ProtocolFinder.
private Map
mappedProtocols
List of available ProtocolFinder.
private SSLContext
sslContext
The SSLContext used for handshaking.
private boolean
isSet
Is the protocol of this listener configured
private boolean
isRequestedTransportSecure
Is the protocol used secure.
private ConcurrentLinkedQueue
puTasks
A cached list of PUTask.
private static Logger
logger
Logger
Constructors Summary
public PortUnificationPipeline()

    
    // ----------------------------------------------------------------------//
    
    
     
        super();
        loadFinders();
        loadHandlers();
    
Methods Summary
public voidaddProtocolFinder(ProtocolFinder protocolFinder)
Add an instance of ProtocolFinder

        protocolFinders.offer(protocolFinder);
    
public voidaddProtocolHandler(ProtocolHandler protocolHandler)
Add an instance of ProtocolHandler

        String[] protocols = protocolHandler.getProtocols();
        for(String protocol: protocols){
            protocolHandlers.put(protocol,protocolHandler);  
        }
    
public voidaddTask(com.sun.enterprise.web.connector.grizzly.Task task)
Seek for the TCP protocol used. First all ProtocolFinder will be invoked. If the protocol is found, the associated ProtocolHandler will be executed. The execution of this method will occurs on the same thread and the main Selector (SelectorThread).

param
task An implementation of ReadTask

    

        // Performance optimization used when a single protocol
        // is supported by this Pipeline.
        ProtocolHandler protocolHandler = null;
        boolean cachedHandler = mappedProtocols != null 
                && (protocolHandler = 
                        mappedProtocols.get(task.getSelectionKey())) != null;
        SelectorThread selectorThread = task.getSelectorThread();
        KeepAlivePipeline kap = selectorThread == null ? null : 
                                        selectorThread.getKeepAlivePipeline();
        if (protocolFinders.isEmpty() 
                || kap == null
                || protocolHandlers.isEmpty()
                || task.getType() != Task.READ_TASK 
                || (kap.isKeepAlive(task.getSelectionKey()) && !cachedHandler)){
            super.addTask(task);
            return;
        } 
        task.getSelectionKey().attach(null);
        
        if (!isSet) {
            isRequestedTransportSecure = 
                (task.getSelectorThread() instanceof SecureSelector) 
                    ? true: false;
            isSet = true;
            if (isRequestedTransportSecure){
                sslContext = ((SSLSelectorThread)task.getSelectorThread())
                                .getSSLContext();
            }
            
            if (!isRequestedTransportSecure || sslContext == null) {
                if ( sslContext == null ){
                    Enumeration<SelectorThread> selectors 
                        = SelectorThread.getSelectors();                          
                    SelectorThread sel;
                    while (selectors.hasMoreElements()){
                        sel = selectors.nextElement();
                        if (sel instanceof SSLSelectorThread){
                            sslContext = 
                                    ((SSLSelectorThread)sel).getSSLContext();
                            if (sslContext != null)
                                break;
                        }
                    }
                }
            }
            
            if (sslContext == null){
                try{
                    SSLImplementation sslHelper = SSLImplementation.getInstance();
                    ServerSocketFactory serverSF = 
                            sslHelper.getServerSocketFactory();
                    serverSF.setAttribute("keystoreType","JKS");
                    serverSF.setAttribute("keystore",
                            System.getProperty("javax.net.ssl.keyStore"));
                    serverSF.setAttribute("truststoreType","JKS");
                    serverSF.setAttribute("truststore",
                            System.getProperty("javax.net.ssl.trustStore"));                    
                    serverSF.init();
                    sslContext = serverSF.getSSLContext();                    
                } catch(Throwable ex){
                    logger.log(Level.FINE,
                               "SSL not supported.",ex);                    
                }
            }
        }      
        super.addTask(getPUTask((DefaultReadTask)task,protocolHandler));
    
public booleanexpireKey(java.nio.channels.SelectionKey key)
Invoked when the SelectorThread is about to expire a SelectionKey.

return
true if the SelectorThread should expire the SelectionKey, false if not.

        ProtocolHandler ph = mappedProtocols.get(key);
        if (ph != null){
            return ph.expireKey(key);
        } else {
            return true;
        }
    
private com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTaskgetPUTask(com.sun.enterprise.web.connector.grizzly.DefaultReadTask readTask, ProtocolHandler protocolHandler)

        PUTask task = puTasks.poll();
        if (task == null){
            task = new PUTask();
        }
        task.readTask = readTask;
        task.protocolHandler = protocolHandler;
        task.setSelectionKey(readTask.getSelectionKey());
        task.setSelectorThread(readTask.getSelectorThread());
        return task;
    
private voidloadFinders()
Load ProtocolFinder defined as a System property (-Dcom.sun.enterprise.web.connector.grizzly.protocolFinders=... , ...)

      
        if ( System.getProperty(PROTOCOL_FINDERS) != null){
            StringTokenizer st = new StringTokenizer(
                    System.getProperty(PROTOCOL_FINDERS),",");
            
            while (st.hasMoreTokens()){
                ProtocolFinder protocolFinder = (ProtocolFinder)
                    loadInstance(st.nextToken());
                protocolFinders.offer(protocolFinder);
            } 
        }   
    
private voidloadHandlers()
Load ProtocolHandler defined as a System property (-Dcom.sun.enterprise.web.connector.grizzly.protocolHandlers=... , ...)

      
        if ( System.getProperty(PROTOCOL_HANDLERS) != null){
            StringTokenizer st = new StringTokenizer(
                    System.getProperty(PROTOCOL_HANDLERS),",");
            
            while (st.hasMoreTokens()){
                ProtocolHandler protocolHandler = (ProtocolHandler)
                    loadInstance(st.nextToken());
                if ( protocolHandler != null) {
                    String[] protocols = protocolHandler.getProtocols();
                    for(String protocol: protocols){
                        protocolHandlers.put(protocol,protocolHandler);  
                    }
                }
            } 
        }   
    
private java.lang.ObjectloadInstance(java.lang.String property)
Util to load classes using reflection.

        
        Class className = null;                               
        try{                              
            className = Class.forName(property,true,
                    Thread.currentThread().getContextClassLoader());
            return className.newInstance();
        } catch (Throwable t) {
            // Log me 
        }   
        return null;
    
public voidremoveProtocolFinder(ProtocolFinder protocolFinder)
Remove a ProtocolFinder

        protocolFinders.remove(protocolFinder);
    
public voidremoveProtocolHandler(ProtocolHandler protocolHandler)
Remove a ProtocolHandler

        String[] protocols = protocolHandler.getProtocols();
        for(String protocol: protocols){
            protocolHandlers.remove(protocol);  
        }