[jboss-cvs] JBossAS SVN: r62766 - branches/Branch_4_2/tomcat/src/main/org/jboss/web/tomcat/service/session.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed May 2 22:49:58 EDT 2007


Author: bstansberry at jboss.com
Date: 2007-05-02 22:49:58 -0400 (Wed, 02 May 2007)
New Revision: 62766

Modified:
   branches/Branch_4_2/tomcat/src/main/org/jboss/web/tomcat/service/session/SessionIDGenerator.java
Log:
Fix jvm singleton; add a main to test for duplicates

Modified: branches/Branch_4_2/tomcat/src/main/org/jboss/web/tomcat/service/session/SessionIDGenerator.java
===================================================================
--- branches/Branch_4_2/tomcat/src/main/org/jboss/web/tomcat/service/session/SessionIDGenerator.java	2007-05-02 22:11:24 UTC (rev 62765)
+++ branches/Branch_4_2/tomcat/src/main/org/jboss/web/tomcat/service/session/SessionIDGenerator.java	2007-05-03 02:49:58 UTC (rev 62766)
@@ -22,6 +22,7 @@
 package org.jboss.web.tomcat.service.session;
 
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Random;
 import java.security.MessageDigest;
@@ -45,13 +46,12 @@
 
    protected MessageDigest digest = null;
    protected Random random = null;
-   protected static SessionIDGenerator s_;
+   protected static final SessionIDGenerator s_ = new SessionIDGenerator();
    
    protected String sessionIdAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-*";
 
    public static SessionIDGenerator getInstance()
    {
-      if (s_ == null) s_ = new SessionIDGenerator();
       return s_;
    }
 
@@ -236,4 +236,42 @@
       return digest;
    }
 
+   public static void main(String[] args)
+   {
+      SessionIDGenerator gen = SessionIDGenerator.getInstance();
+      
+      HashSet set = new HashSet();
+      String id = null;
+      for (int i = 0; i > -1; i++)
+      {
+         id = gen.getSessionId();
+         
+         if (set.add(id) == false)
+         {
+            System.out.println(i + " FAILED -- duplicate id " + id);
+            break;
+         }
+         
+         if (i % 1000 == 0 && i > 0)
+         {
+            System.out.println("Tested " + i + " session ids");
+         }
+         if (i % 50000 == 0 && set.size() > 450000)
+         {
+            System.out.println("Discarding...");
+            int k = 0;
+            int discarded = 0;
+            for (Iterator it = set.iterator(); it.hasNext() && discarded < 50000; k++)
+            {
+               it.next();
+               if (k % 10 == 0) 
+               {
+                  it.remove();
+                  discarded++;
+               }
+            }
+            System.out.println("Discarding completed");
+         }
+      }
+   }
 }




More information about the jboss-cvs-commits mailing list