1 package org.apache.turbine.util; 2 3 18 19 import java.util.Random ; 20 21 35 public class GenerateUniqueId 36 { 37 45 static private int session_count = 0; 46 static private long lastTimeVal = 0; 47 static private Random randomSource = new java.util.Random (); 48 49 51 56 public final static long maxRandomLen = 2176782336L; 58 64 public final static long maxSessionLifespanTics = 46656; 66 70 public final static long ticDifference = 2000; 71 72 80 static synchronized public String getIdentifier() 81 { 82 StringBuffer sessionId = new StringBuffer (); 83 84 long n = randomSource.nextLong(); 86 if (n < 0) n = -n; 87 n %= maxRandomLen; 88 89 n += maxRandomLen; 92 sessionId.append(Long.toString(n, Character.MAX_RADIX) 93 .substring(1)); 94 95 long timeVal = (System.currentTimeMillis() / ticDifference); 96 97 timeVal %= maxSessionLifespanTics; 99 100 timeVal += maxSessionLifespanTics; 102 103 sessionId.append(Long.toString(timeVal, Character.MAX_RADIX) 104 .substring(1)); 105 106 110 111 if (lastTimeVal != timeVal) 114 { 115 lastTimeVal = timeVal; 116 session_count = 0; 117 } 118 sessionId.append(Long.toString(++session_count, 119 Character.MAX_RADIX)); 120 121 return sessionId.toString(); 122 } 123 124 130 synchronized public String getIdentifier(String jsIdent) 131 { 132 if (jsIdent != null && jsIdent.length() > 0) 133 { 134 return getIdentifier() + "." + jsIdent; 135 } 136 return getIdentifier(); 137 } 138 139 144 public static void main(String [] args) 145 { 146 System.out.println(GenerateUniqueId.getIdentifier()); 147 System.out.println(GenerateUniqueId.getIdentifier()); 148 System.out.println(GenerateUniqueId.getIdentifier()); 149 System.out.println(GenerateUniqueId.getIdentifier()); 150 } 151 } 152 | Popular Tags |