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

AbstractSessionContext

public abstract class AbstractSessionContext extends Object implements SSLSessionContext
Supports SSL session caches.

Fields Summary
volatile int
maximumSize
volatile int
timeout
final SSLParameters
parameters
static final int
OPEN_SSL
Identifies OpenSSL sessions.
Constructors Summary
AbstractSessionContext(SSLParameters parameters, int maximumSize, int timeout)
Constructs a new session context.

param
parameters
param
maximumSize of cache
param
timeout for cache entries


                         
       
              
        this.parameters = parameters;
        this.maximumSize = maximumSize;
        this.timeout = timeout;
    
Methods Summary
public final java.util.EnumerationgetIds()

        final Iterator<SSLSession> iterator = sessionIterator();
        return new Enumeration<byte[]>() {
            public boolean hasMoreElements() {
                return iterator.hasNext();
            }
            public byte[] nextElement() {
                return iterator.next().getId();
            }
        };
    
public final intgetSessionCacheSize()

        return maximumSize;
    
public final intgetSessionTimeout()

        return timeout;
    
static voidlog(java.lang.Throwable t)

        java.util.logging.Logger.global.log(Level.WARNING,
                "Error converting session.", t);
    
abstract java.util.IteratorsessionIterator()
Returns the collection of sessions ordered by least-recently-used first.

public final voidsetSessionCacheSize(int size)

        if (size < 0) {
            throw new IllegalArgumentException("size < 0");
        }

        int oldMaximum = maximumSize;
        maximumSize = size;

        // Trim cache to size if necessary.
        if (size < oldMaximum) {
            trimToSize();
        }
    
byte[]toBytes(javax.net.ssl.SSLSession session)
Converts the given session to bytes.

return
session data as bytes or null if the session can't be converted

        // TODO: Support SSLSessionImpl, too.
        if (!(session instanceof OpenSSLSessionImpl)) {
            return null;
        }

        OpenSSLSessionImpl sslSession = (OpenSSLSessionImpl) session;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream daos = new DataOutputStream(baos);

            daos.writeInt(OPEN_SSL); // session type ID

            // Session data.
            byte[] data = sslSession.getEncoded();
            daos.writeInt(data.length);
            daos.write(data);

            // Certificates.
            X509Certificate[] certs = session.getPeerCertificateChain();
            daos.writeInt(certs.length);

            // TODO: Call nativegetpeercertificates()
            for (X509Certificate cert : certs) {
                data = cert.getEncoded();
                daos.writeInt(data.length);
                daos.write(data);
            }

            return baos.toByteArray();
        } catch (IOException e) {
            log(e);
            return null;
        } catch (CertificateEncodingException e) {
            log(e);
            return null;
        }
    
javax.net.ssl.SSLSessiontoSession(byte[] data, java.lang.String host, int port)
Creates a session from the given bytes.

return
a session or null if the session can't be converted

        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dais = new DataInputStream(bais);
        try {
            int type = dais.readInt();
            if (type != OPEN_SSL) {
                log(new AssertionError("Unexpected type ID: " + type));
                return null;
            }

            int length = dais.readInt();
            byte[] sessionData = new byte[length];
            dais.readFully(sessionData);

            int count = dais.readInt();
            X509Certificate[] certs = new X509Certificate[count];
            for (int i = 0; i < count; i++) {
                length = dais.readInt();
                byte[] certData = new byte[length];
                dais.readFully(certData);
                certs[i] = X509Certificate.getInstance(certData);
            }

            return new OpenSSLSessionImpl(sessionData, parameters, host, port,
                    certs, this);
        } catch (IOException e) {
            log(e);
            return null;
        } catch (CertificateException e) {
            log(e);
            return null;
        }
    
abstract voidtrimToSize()
Makes sure cache size is < maximumSize.