KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > DefaultIdGenerator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.terracotta.session.util;
6
7 import com.tc.object.bytecode.Manager;
8 import com.terracotta.session.SessionId;
9
10 import java.security.SecureRandom JavaDoc;
11
12 /**
13  * generates session cookie id of the format: <4hex:random><4hex:nextId>[[16hex:random]*]
14  */

15 public class DefaultIdGenerator implements SessionIdGenerator {
16
17   // NOTE: IMPORTANT!!! don't change MIN_LENGTH without reviewing generateKey method
18
private static final int MIN_LENGTH = 8;
19   private static final char DLM = '!';
20   private final SecureRandom JavaDoc random;
21   private final int idLength;
22   private final String JavaDoc serverId;
23   private short nextId = Short.MIN_VALUE;
24   private final int lockType;
25
26   public static SessionIdGenerator makeInstance(ConfigProperties cp, int lockType) {
27     Assert.pre(cp != null);
28     final int idLength = cp.getSessionIdLength();
29     final String JavaDoc servetId = cp.getServerId();
30     return new DefaultIdGenerator(idLength, servetId, lockType);
31   }
32
33   // for non-synchronous-write tests
34
public DefaultIdGenerator(final int idLength, final String JavaDoc serverId) {
35     this(idLength, serverId, Manager.LOCK_TYPE_WRITE);
36   }
37
38   public DefaultIdGenerator(final int idLength, final String JavaDoc serverId, int lockType) {
39     random = new SecureRandom JavaDoc();
40     // init
41
random.nextInt();
42
43     this.lockType = lockType;
44     this.idLength = Math.max(idLength, MIN_LENGTH);
45     this.serverId = serverId;
46   }
47
48   public SessionId generateNewId() {
49     final String JavaDoc key = generateKey();
50     final String JavaDoc externalId = key + DLM + serverId;
51     return new DefaultSessionId(key, null, externalId, lockType);
52   }
53
54   public SessionId makeInstanceFromBrowserId(String JavaDoc requestedSessionId) {
55     Assert.pre(requestedSessionId != null);
56     final int dlmIndex = requestedSessionId.indexOf(DLM);
57     // everything before dlmIndex is key, everything after is serverId
58
if (dlmIndex > 0) {
59       final String JavaDoc key = requestedSessionId.substring(0, dlmIndex);
60       final String JavaDoc externalId = key + DLM + serverId;
61       return new DefaultSessionId(key, requestedSessionId, externalId, lockType);
62     } else {
63       // DLM is missing. someone is messing with our session ids!
64
return null;
65     }
66   }
67
68   public SessionId makeInstanceFromInternalKey(String JavaDoc key) {
69     final String JavaDoc externalId = key + DLM + serverId;
70     return new DefaultSessionId(key, externalId, externalId, lockType);
71   }
72
73   /**
74    * NOTE: IMPORTANT!!! don't change MIN_LENGTH without reviewing generateKey method mininum size of generated string
75    * must be 8 chars
76    */

77   protected synchronized String JavaDoc generateKey() {
78     final byte[] bytes = new byte[2];
79     final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
80     // append 4hex:random
81
random.nextBytes(bytes);
82     sb.append(toHex(bytes, 2));
83
84     // append 4hex:nextId
85
toBytes(getNextId(), bytes);
86     sb.append(toHex(bytes, 2));
87
88     // append randome bytes until we reach required length
89
if (sb.length() < idLength) {
90       final byte[] extraBytes = new byte[idLength - MIN_LENGTH];
91       random.nextBytes(extraBytes);
92       sb.append(toHex(extraBytes, extraBytes.length));
93     }
94     return sb.substring(0, idLength);
95   }
96
97   protected synchronized short getNextId() {
98     return nextId++;
99   }
100
101   protected static void toBytes(short s, byte[] bytes) {
102     bytes[0] = (byte) ((s & 0xff00) >> 8);
103     bytes[1] = (byte) (s & 0x00ff);
104   }
105
106   protected static String JavaDoc toHex(byte[] bytes, int byteCnt) {
107     final char[] hexChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
108     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
109     for (int i = 0; i < byteCnt; i++) {
110       byte b = bytes[i];
111       byte b1 = (byte) ((b & 0xf0) >> 4);
112       byte b2 = (byte) (b & 0x0f);
113       sb.append(hexChars[b1]).append(hexChars[b2]);
114     }
115     return sb.toString();
116   }
117 }
118
Popular Tags