FileDocCategorySizeDatePackage
ClientSessionContext.javaAPI DocAndroid 1.5 API7067Wed May 06 22:41:06 BST 2009org.apache.harmony.xnet.provider.jsse

ClientSessionContext

public class ClientSessionContext extends AbstractSessionContext
Caches client sessions. Indexes by host and port. Users are typically looking to reuse any session for a given host and port. Users of the standard API are forced to iterate over the sessions semi-linearly as opposed to in constant time.

Fields Summary
final Map
sessions
Sessions indexed by host and port in access order.
Map
sessionsById
Sessions indexed by ID. Initialized on demand. Protected from concurrent access by holding a lock on sessions.
final SSLClientSessionCache
persistentCache
Constructors Summary
public ClientSessionContext(SSLParameters parameters, SSLClientSessionCache persistentCache)


      
              
        super(parameters, 10, 0);
        this.persistentCache = persistentCache;
    
Methods Summary
public javax.net.ssl.SSLSessiongetSession(byte[] sessionId)
{@inheritDoc}

see
#getSession(String, int) for an implementation-specific but more efficient approach

        /*
         * This method is typically used in conjunction with getIds() to
         * iterate over the sessions linearly, so it doesn't make sense for
         * it to impact access order.
         *
         * It also doesn't load sessions from the persistent cache as doing
         * so would likely force every session to load.
         */

        ByteArray id = new ByteArray(sessionId);
        synchronized (sessions) {
            indexById();
            return sessionsById.get(id);
        }
    
public javax.net.ssl.SSLSessiongetSession(java.lang.String host, int port)
Finds a cached session for the given host name and port.

param
host of server
param
port of server
return
cached session or null if none found

        synchronized (sessions) {
            SSLSession session = sessions.get(new HostAndPort(host, port));
            if (session != null) {
                return session;
            }
        }

        // Look in persistent cache.
        if (persistentCache != null) {
            byte[] data = persistentCache.getSessionData(host, port);
            if (data != null) {
                SSLSession session = toSession(data, host, port);
                if (session != null) {
                    synchronized (sessions) {
                        sessions.put(new HostAndPort(host, port), session);
                        indexById(session);
                    }
                    return session;
                }
            }
        }

        return null;
    
private voidindexById()
Ensures that the ID-based index is initialized.

        if (sessionsById == null) {
            sessionsById = new HashMap<ByteArray, SSLSession>();
            for (SSLSession session : sessions.values()) {
                sessionsById.put(new ByteArray(session.getId()), session);
            }
        }
    
private voidindexById(javax.net.ssl.SSLSession session)
Adds the given session to the ID-based index if the index has already been initialized.

        if (sessionsById != null) {
            sessionsById.put(new ByteArray(session.getId()), session);
        }
    
voidputSession(javax.net.ssl.SSLSession session)

        HostAndPort key = new HostAndPort(session.getPeerHost(),
                session.getPeerPort());
        synchronized (sessions) {
            sessions.put(key, session);
            indexById(session);
        }

        // TODO: This in a background thread.
        if (persistentCache != null) {
            byte[] data = toBytes(session);
            if (data != null) {
                persistentCache.putSessionData(session, data);
            }
        }
    
voidremoveById(javax.net.ssl.SSLSession session)

        if (sessionsById != null) {
            sessionsById.remove(new ByteArray(session.getId()));
        }
    
java.util.IteratorsessionIterator()

        synchronized (sessions) {
            SSLSession[] array = sessions.values().toArray(
                    new SSLSession[sessions.size()]);
            return Arrays.asList(array).iterator();
        }
    
public final voidsetSessionTimeout(int seconds)

        if (seconds < 0) {
            throw new IllegalArgumentException("seconds < 0");
        }
        timeout = seconds;
    
voidtrimToSize()

        synchronized (sessions) {
            int size = sessions.size();
            if (size > maximumSize) {
                int removals = size - maximumSize;
                Iterator<SSLSession> i = sessions.values().iterator();
                do {
                    removeById(i.next());
                    i.remove();
                } while (--removals > 0);                
            }
        }