1 23 24 package com.sun.ejb.base.sfsb.util; 25 26 import java.security.SecureRandom ; 27 28 import com.sun.ejb.spi.sfsb.util.SFSBUUIDUtil; 29 import com.sun.enterprise.util.Utility; 30 31 41 public class ScrambledKeyGenerator 42 extends SimpleKeyGenerator 43 { 44 45 private SecureRandom random = new SecureRandom (); 46 private int cachedTime = (int) System.currentTimeMillis(); 47 48 public ScrambledKeyGenerator() { 49 init((int) System.currentTimeMillis(), System.identityHashCode(this)); 50 } 51 52 public ScrambledKeyGenerator(byte[] ipAddress, int port) { 53 init(Utility.bytesToInt(ipAddress, 0), port); 54 } 55 56 public ScrambledKeyGenerator(long val) { 57 init((int) (val >>> 32), (int) ((val << 32) >>> 32)); 58 } 59 60 private void init(int hi, int lo) { 61 byte[] hiBytes = new byte[4]; 62 Utility.intToBytes(hi, hiBytes, 0); 63 byte[] loBytes = new byte[4]; 64 Utility.intToBytes(lo, loBytes, 0); 65 66 swapBytes(hiBytes, loBytes, 2, 3); 67 swapBytes(loBytes, loBytes, 2, 3); 68 swapBytes(hiBytes, loBytes, 3, 0); 69 swapBytes(hiBytes, hiBytes, 0, 3); 70 swapBytes(hiBytes, loBytes, 1, 3); 71 72 swapBytes(hiBytes, hiBytes, 0, 1); 73 swapBytes(loBytes, loBytes, 2, 3); 74 75 this.prefix = Utility.bytesToInt(hiBytes, 0); 76 this.prefix <<= 32; 77 78 this.suffix = Utility.bytesToInt(loBytes, 0); 79 this.suffix <<= 32; 80 81 this.idCounter = 0; 83 84 this.cachedTime = (int) System.currentTimeMillis(); 86 } 87 88 private static final void swapBytes(byte[] a, byte[] b, 89 int index1, int index2) 90 { 91 byte temp = a[index1]; 92 a[index1] = b[index2]; 93 b[index2] = temp; 94 } 95 96 100 public Object createSessionKey() { 101 int id = 0; 102 synchronized (this) { 103 id = ++idCounter; 104 if (id < 0) { 105 id = idCounter = 1; 107 } 108 109 if ((id & 0x000000FF) == 0) { 110 cachedTime = (int) System.currentTimeMillis(); 111 } 112 } 113 114 return new SimpleSessionKey( 115 prefix + cachedTime, 116 suffix + random.nextInt(), id 118 ); 119 } 120 121 } 122 123 | Popular Tags |