1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Random;
20
21 /***
22 * This class generates a unique 10+ character id. This is good for
23 * authenticating users or tracking users around.
24 *
25 * <p>This code was borrowed from Apache
26 * JServ.JServServletManager.java. It is what Apache JServ uses to
27 * generate session ids for users. Unfortunately, it was not included
28 * in Apache JServ as a class, so I had to create one here in order to
29 * use it.
30 *
31 * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
32 * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
33 * @version $Id: GenerateUniqueId.java 264148 2005-08-29 14:21:04Z henning $
34 */
35 public class GenerateUniqueId
36 {
37
38
39
40
41
42
43
44
45 static private int session_count = 0;
46 static private long lastTimeVal = 0;
47 static private Random randomSource = new java.util.Random();
48
49
50
51
52
53
54
55
56 public final static long maxRandomLen = 2176782336L;
57
58
59
60
61
62
63
64 public final static long maxSessionLifespanTics = 46656;
65
66
67
68
69
70 public final static long ticDifference = 2000;
71
72 /***
73 * Get the unique id.
74 *
75 * <p>NOTE: This must work together with
76 * get_jserv_session_balance() in jserv_balance.c
77 *
78 * @return A String with the new unique id.
79 */
80 static synchronized public String getIdentifier()
81 {
82 StringBuffer sessionId = new StringBuffer();
83
84
85 long n = randomSource.nextLong();
86 if (n < 0) n = -n;
87 n %= maxRandomLen;
88
89
90
91 n += maxRandomLen;
92 sessionId.append(Long.toString(n, Character.MAX_RADIX)
93 .substring(1));
94
95 long timeVal = (System.currentTimeMillis() / ticDifference);
96
97
98 timeVal %= maxSessionLifespanTics;
99
100
101 timeVal += maxSessionLifespanTics;
102
103 sessionId.append(Long.toString(timeVal, Character.MAX_RADIX)
104 .substring(1));
105
106
107
108
109
110
111
112
113 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 /***
125 * Get the unique id.
126 *
127 * @param jsIdent A String.
128 * @return A String with the new unique id.
129 */
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 /***
140 * Simple test of the functionality.
141 *
142 * @param args A String[] with the command line arguments.
143 */
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 }