1 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 ; 11 12 15 public class DefaultIdGenerator implements SessionIdGenerator { 16 17 private static final int MIN_LENGTH = 8; 19 private static final char DLM = '!'; 20 private final SecureRandom random; 21 private final int idLength; 22 private final String 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 servetId = cp.getServerId(); 30 return new DefaultIdGenerator(idLength, servetId, lockType); 31 } 32 33 public DefaultIdGenerator(final int idLength, final String serverId) { 35 this(idLength, serverId, Manager.LOCK_TYPE_WRITE); 36 } 37 38 public DefaultIdGenerator(final int idLength, final String serverId, int lockType) { 39 random = new SecureRandom (); 40 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 key = generateKey(); 50 final String externalId = key + DLM + serverId; 51 return new DefaultSessionId(key, null, externalId, lockType); 52 } 53 54 public SessionId makeInstanceFromBrowserId(String requestedSessionId) { 55 Assert.pre(requestedSessionId != null); 56 final int dlmIndex = requestedSessionId.indexOf(DLM); 57 if (dlmIndex > 0) { 59 final String key = requestedSessionId.substring(0, dlmIndex); 60 final String externalId = key + DLM + serverId; 61 return new DefaultSessionId(key, requestedSessionId, externalId, lockType); 62 } else { 63 return null; 65 } 66 } 67 68 public SessionId makeInstanceFromInternalKey(String key) { 69 final String externalId = key + DLM + serverId; 70 return new DefaultSessionId(key, externalId, externalId, lockType); 71 } 72 73 77 protected synchronized String generateKey() { 78 final byte[] bytes = new byte[2]; 79 final StringBuffer sb = new StringBuffer (); 80 random.nextBytes(bytes); 82 sb.append(toHex(bytes, 2)); 83 84 toBytes(getNextId(), bytes); 86 sb.append(toHex(bytes, 2)); 87 88 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 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 sb = new StringBuffer (); 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 |