1 23 24 package com.sun.ejb.base.sfsb.util; 25 26 import com.sun.ejb.spi.sfsb.util.SFSBUUIDUtil; 27 import com.sun.enterprise.util.Utility; 28 29 39 public class SimpleKeyGenerator 40 implements SFSBUUIDUtil 41 { 42 43 protected long prefix; 44 protected long suffix; 45 protected int idCounter; 46 47 protected SimpleKeyGenerator() { 48 long now = System.currentTimeMillis(); 49 now = ((int) (now >>> 32)) + ((int) now); 50 scramble((int) now, System.identityHashCode(this)); 51 } 52 53 public SimpleKeyGenerator(byte[] ipAddress, int port) { 54 scramble((Utility.bytesToInt(ipAddress, 0) << 32), port); 55 } 56 57 public SimpleKeyGenerator(long uniquePrefix) { 58 this.prefix = uniquePrefix; 59 60 this.suffix = System.currentTimeMillis(); 62 63 this.idCounter = 0; 65 66 } 67 68 72 public Object createSessionKey() { 73 int id = 0; 74 synchronized (this) { 75 id = idCounter++; 76 if (id < 0) { 77 id = idCounter = 0; 79 suffix = System.currentTimeMillis(); 80 } 81 } 82 83 return new SimpleSessionKey(prefix, suffix, id); 84 } 85 86 92 public byte[] keyToByteArray(Object sessionKey) { 93 SimpleSessionKey key = (SimpleSessionKey) sessionKey; 94 byte[] array = new byte[20]; 95 96 Utility.longToBytes(key.prefix, array, 0); 97 Utility.longToBytes(key.suffix, array, 8); 98 Utility.intToBytes(key.id, array, 16); 99 100 return array; 101 } 102 103 111 public Object byteArrayToKey(byte[] array, int startIndex, int len) { 112 long myPrefix = Utility.bytesToLong(array, startIndex); 113 long mySuffix = Utility.bytesToLong(array, startIndex+8); 114 int myId = Utility.bytesToInt(array, startIndex+16); 115 116 return new SimpleSessionKey(myPrefix, mySuffix, myId); 117 } 118 119 private void scramble(int hi, int lo) { 120 byte[] hiBytes = new byte[4]; 121 Utility.intToBytes(hi, hiBytes, 0); 122 byte[] loBytes = new byte[4]; 123 Utility.intToBytes(lo, loBytes, 0); 124 125 swapBytes(hiBytes, loBytes, 2, 3); 126 swapBytes(hiBytes, loBytes, 3, 0); 127 swapBytes(hiBytes, loBytes, 1, 3); 128 swapBytes(hiBytes, hiBytes, 0, 3); 129 swapBytes(loBytes, loBytes, 2, 3); 130 131 this.prefix = Utility.bytesToInt(hiBytes, 0); 132 133 this.prefix = 134 (this.prefix << 32) + Utility.bytesToInt(loBytes, 0); 135 136 this.idCounter = 0; 138 139 this.suffix = (int) System.currentTimeMillis(); 141 } 142 143 private static final void swapBytes(byte[] a, byte[] b, 144 int index1, int index2) 145 { 146 byte temp = a[index1]; 147 a[index1] = b[index2]; 148 b[index2] = temp; 149 } 150 151 152 protected static class SimpleSessionKey 153 implements java.io.Serializable 154 { 155 156 long prefix; 157 long suffix; 158 int id; 159 160 public SimpleSessionKey(long prefix, long suffix, int id) { 161 this.prefix = prefix; 162 this.suffix = suffix; 163 this.id = id; 164 } 165 166 public int hashCode() { 167 return (int) id; 168 } 169 170 public boolean equals(Object otherObj) { 171 if (otherObj instanceof SimpleSessionKey) { 172 SimpleSessionKey other = (SimpleSessionKey) otherObj; 173 return ( 174 (id == other.id) && (prefix == other.prefix) 175 && (suffix == other.suffix) 176 ); 177 } 178 179 return false; 180 } 181 182 public String toString() { 183 StringBuffer sbuf = new StringBuffer (); 184 sbuf.append(Long.toHexString(prefix)).append("-") 185 .append(Long.toHexString(suffix)).append("-") 186 .append(Integer.toHexString(id)); 187 return sbuf.toString(); 188 } 189 } 190 191 } 192 193 | Popular Tags |