harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndbe...@apache.org
Subject svn commit: r726601 - /harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java
Date Mon, 15 Dec 2008 01:44:59 GMT
Author: ndbeyer
Date: Sun Dec 14 17:44:58 2008
New Revision: 726601

URL: http://svn.apache.org/viewvc?rev=726601&view=rev
Log:
rework SSLSessionImpl - eliminate TwoKeyHashMap, use VM cloning, format, remove unnecessary
comments

Modified:
    harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java

Modified: harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java?rev=726601&r1=726600&r2=726601&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/x-net/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java
Sun Dec 14 17:44:58 2008
@@ -24,7 +24,8 @@
 import java.security.cert.Certificate;
 import java.security.cert.CertificateEncodingException;
 import java.security.cert.X509Certificate;
-import java.util.Iterator;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Vector;
 
 import javax.net.ssl.SSLPeerUnverifiedException;
@@ -34,26 +35,67 @@
 import javax.net.ssl.SSLSessionBindingListener;
 import javax.net.ssl.SSLSessionContext;
 
-import org.apache.harmony.luni.util.TwoKeyHashMap;
-import org.apache.harmony.xnet.provider.jsse.SSLSessionContextImpl;
-
 /**
  * 
  * SSLSession implementation
- *
+ * 
  * @see javax.net.ssl.SSLSession
  */
-public class SSLSessionImpl implements SSLSession {
+public class SSLSessionImpl implements SSLSession, Cloneable  {
 
     /**
-     * Session object reporting an invalid cipher suite of 
-     * "SSL_NULL_WITH_NULL_NULL"
+     * Session object reporting an invalid cipher suite of "SSL_NULL_WITH_NULL_NULL"
      */
     public static final SSLSessionImpl NULL_SESSION = new SSLSessionImpl(null);
 
+    /**
+     * Container class for the 'value' map's keys.
+     */
+    private static final class ValueKey {
+        final String name;
+        final AccessControlContext acc;
+
+        ValueKey(String name) {
+            super();
+            this.name = name;
+            this.acc = AccessController.getContext();
+        }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + ((acc == null) ? 0 : acc.hashCode());
+            result = prime * result + ((name == null) ? 0 : name.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (obj == null)
+                return false;
+            if (!(obj instanceof ValueKey))
+                return false;
+            ValueKey other = (ValueKey) obj;
+            if (acc == null) {
+                if (other.acc != null)
+                    return false;
+            } else if (!acc.equals(other.acc))
+                return false;
+            if (name == null) {
+                if (other.name != null)
+                    return false;
+            } else if (!name.equals(other.name))
+                return false;
+            return true;
+        }
+    }
+
     private long creationTime;
     private boolean isValid = true;
-    private TwoKeyHashMap values = new TwoKeyHashMap();
+    private Map<ValueKey, Object> values = new HashMap<ValueKey, Object>();
 
     /**
      * ID of the session
@@ -61,7 +103,7 @@
     byte[] id;
 
     /**
-     * Last time the session was accessed 
+     * Last time the session was accessed
      */
     long lastAccessedTime;
 
@@ -80,9 +122,8 @@
      */
     SSLSessionContextImpl context;
 
-
     /**
-     * certificates were sent to the peer 
+     * certificates were sent to the peer
      */
     X509Certificate[] localCertificates;
 
@@ -92,21 +133,20 @@
     X509Certificate[] peerCertificates;
 
     /**
-     * Peer host name 
+     * Peer host name
      */
-    String peerHost;
+    private String peerHost;
 
     /**
      * Peer port number
      */
-    int peerPort = -1;
+    private int peerPort = -1;
 
     /**
      * Master secret
      */
     byte[] master_secret;
 
-
     /**
      * clientRandom
      */
@@ -120,10 +160,11 @@
     /**
      * True if this entity is considered the server
      */
-    boolean isServer = false;
+    final boolean isServer;
 
     /**
      * Creates SSLSession implementation
+     * 
      * @param cipher_suite
      * @param sr
      */
@@ -138,11 +179,11 @@
             this.cipherSuite = cipher_suite;
             id = new byte[32];
             sr.nextBytes(id);
-            long time = new java.util.Date().getTime() / 1000;
+            long time = creationTime / 1000;
             id[28] = (byte) ((time & 0xFF000000) >>> 24);
-            id[29] = (byte) ((time & 0xFF0000) >>> 16);
-            id[30] = (byte) ((time & 0xFF00) >>> 8);
-            id[31] = (byte) (time & 0xFF);
+            id[29] = (byte) ((time & 0x00FF0000) >>> 16);
+            id[30] = (byte) ((time & 0x0000FF00) >>> 8);
+            id[31] = (byte) ((time & 0x000000FF));
             isServer = true;
         }
 
@@ -150,60 +191,37 @@
 
     /**
      * Creates SSLSession implementation
+     * 
      * @param sr
      */
     public SSLSessionImpl(SecureRandom sr) {
         this(null, sr);
     }
 
-    private SSLSessionImpl() {
-    }
-
-    /**
-     * @see javax.net.ssl.SSLSession.getApplicationBufferSize()
-     */
     public int getApplicationBufferSize() {
         return SSLRecordProtocol.MAX_DATA_LENGTH;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getCipherSuite()
-     */
     public String getCipherSuite() {
         return cipherSuite.getName();
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getCreationTime()
-     */
     public long getCreationTime() {
         return creationTime;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getId()
-     */
     public byte[] getId() {
         return id;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getLastAccessedTime()
-     */
     public long getLastAccessedTime() {
         return lastAccessedTime;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getLocalCertificates()
-     */
     public Certificate[] getLocalCertificates() {
         return localCertificates;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getLocalPrincipal()
-     */
     public Principal getLocalPrincipal() {
         if (localCertificates != null && localCertificates.length > 0) {
             return localCertificates[0].getSubjectX500Principal();
@@ -211,16 +229,10 @@
         return null;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getPacketBufferSize()
-     */
     public int getPacketBufferSize() {
         return SSLRecordProtocol.MAX_SSL_PACKET_SIZE;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getPeerCertificateChain()
-     */
     public javax.security.cert.X509Certificate[] getPeerCertificateChain()
             throws SSLPeerUnverifiedException {
         if (peerCertificates == null) {
@@ -229,8 +241,8 @@
         javax.security.cert.X509Certificate[] certs = new javax.security.cert.X509Certificate[peerCertificates.length];
         for (int i = 0; i < certs.length; i++) {
             try {
-                certs[i] = javax.security.cert.X509Certificate
-                        .getInstance(peerCertificates[i].getEncoded());
+                certs[i] = javax.security.cert.X509Certificate.getInstance(peerCertificates[i]
+                        .getEncoded());
             } catch (javax.security.cert.CertificateException e) {
             } catch (CertificateEncodingException e) {
             }
@@ -238,34 +250,21 @@
         return certs;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getPeerCertificates()
-     */
-    public Certificate[] getPeerCertificates()
-            throws SSLPeerUnverifiedException {
+    public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
         if (peerCertificates == null) {
             throw new SSLPeerUnverifiedException("No peer certificate");
         }
         return peerCertificates;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getPeerHost()
-     */
     public String getPeerHost() {
         return peerHost;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getPeerPort()
-     */
     public int getPeerPort() {
         return peerPort;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getPeerPrincipal()
-     */
     public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
         if (peerCertificates == null) {
             throw new SSLPeerUnverifiedException("No peer certificate");
@@ -273,16 +272,10 @@
         return peerCertificates[0].getSubjectX500Principal();
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getProtocol()
-     */
     public String getProtocol() {
         return protocol.name;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getSessionContext()
-     */
     public SSLSessionContext getSessionContext() {
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
@@ -291,110 +284,71 @@
         return context;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getValue(String name)
-     */
     public Object getValue(String name) {
         if (name == null) {
             throw new IllegalArgumentException("Parameter is null");
         }
-        return values.get(name, AccessController.getContext());
+        return values.get(new ValueKey(name));
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.getValueNames()
-     */
     public String[] getValueNames() {
-        Vector v = new Vector();
-        AccessControlContext current = AccessController.getContext();
-        AccessControlContext cont;
-        for (Iterator it = values.entrySet().iterator(); it.hasNext();) {
-            TwoKeyHashMap.Entry entry = (TwoKeyHashMap.Entry) it.next();
-            cont = (AccessControlContext) entry.getKey2();
-            if ((current == null && cont == null)
-                    || (current != null && current.equals(cont))) {
-                v.add(entry.getKey1());
+        final Vector<String> v = new Vector<String>();
+        final AccessControlContext currAcc = AccessController.getContext();
+        for (ValueKey key : values.keySet()) {
+            if ((currAcc == null && key.acc == null)
+                    || (currAcc != null && currAcc.equals(key.acc))) {
+                v.add(key.name);
             }
         }
-        return (String[]) v.toArray(new String[0]);
+        return v.toArray(new String[v.size()]);
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.invalidate()
-     */
     public void invalidate() {
         isValid = false;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.isValid()
-     */
     public boolean isValid() {
-        if (isValid
-                && context != null
-                && context.getSessionTimeout() != 0
-                && lastAccessedTime + context.getSessionTimeout() > System
-                        .currentTimeMillis()) {
+        if (isValid && context != null && context.getSessionTimeout() !=
0
+                && lastAccessedTime + context.getSessionTimeout() > System.currentTimeMillis())
{
             isValid = false;
         }
         return isValid;
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.putValue(String name, Object value)
-     */
     public void putValue(String name, Object value) {
         if (name == null || value == null) {
             throw new IllegalArgumentException("Parameter is null");
         }
-        Object old = values.put(name, AccessController.getContext(), value);
+        Object old = values.put(new ValueKey(name), value);
         if (value instanceof SSLSessionBindingListener) {
-            ((SSLSessionBindingListener) value)
-                    .valueBound(new SSLSessionBindingEvent(this, name));
+            ((SSLSessionBindingListener) value).valueBound(new SSLSessionBindingEvent(this,
name));
         }
         if (old != null && old instanceof SSLSessionBindingListener) {
-            ((SSLSessionBindingListener) old)
-                    .valueUnbound(new SSLSessionBindingEvent(this, name));
+            ((SSLSessionBindingListener) old).valueUnbound(new SSLSessionBindingEvent(this,
name));
         }
 
     }
 
-    /**
-     * @see javax.net.ssl.SSLSession.removeValue(String name)
-     */
     public void removeValue(String name) {
         if (name == null) {
             throw new IllegalArgumentException("Parameter is null");
         }
-        values.remove(name, AccessController.getContext());
+        values.remove(new ValueKey(name));
 
     }
 
     @Override
     public Object clone() {
-        SSLSessionImpl ses = new SSLSessionImpl();
-        ses.id = this.id;
-        ses.creationTime = this.creationTime;
-        ses.lastAccessedTime = this.lastAccessedTime;
-        ses.isValid = this.isValid;
-        ses.cipherSuite = this.cipherSuite;
-        ses.localCertificates = this.localCertificates;
-        ses.peerCertificates = this.peerCertificates;
-        ses.master_secret = this.master_secret;
-        ses.clientRandom = this.clientRandom;
-        ses.serverRandom = this.serverRandom;
-        ses.peerHost = this.peerHost;
-        ses.peerPort = this.peerPort;
-        ses.isServer = this.isServer;
-        ses.context = this.context;
-        ses.protocol = this.protocol;
-        ses.values = this.values;
-        return ses;
+        try {
+            return super.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new AssertionError(e);
+        }
     }
 
-
     /**
      * Sets the address of the peer
+     * 
      * @param peerHost
      * @param peerPort
      */



Mime
View raw message