1 23 24 package com.sun.ejb.base.sfsb; 25 26 import com.sun.ejb.spi.sfsb.SFSBUUIDUtil; 27 import com.sun.enterprise.util.Utility; 28 29 39 public class SimpleKeyGenerator 40 implements SFSBUUIDUtil 41 { 42 43 private int idCounter; 44 private long time; 45 private long prefix; 46 47 public SimpleKeyGenerator() { 48 this.idCounter = 0; 50 51 this.time = System.currentTimeMillis(); 53 54 this.prefix = 0; 56 } 57 58 public void setPrefix(int val) { 59 this.prefix = val; 60 this.prefix = this.prefix << 32; 61 } 62 63 67 public Object createSessionKey() { 68 int id = 0; 69 synchronized (this) { 70 id = ++idCounter; 71 if (id < 0) { 72 id = idCounter = 0; 74 time = System.currentTimeMillis(); 75 } 76 } 77 78 return new SimpleSessionKey(time, prefix + id); 79 } 80 81 87 public byte[] keyToByteArray(Object sessionKey) { 88 SimpleSessionKey key = (SimpleSessionKey) sessionKey; 89 byte[] array = new byte[16]; 90 91 Utility.longToBytes(key.time, array, 0); 92 Utility.longToBytes(key.id, array, 8); 93 94 return array; 95 } 96 97 105 public Object byteArrayToKey(byte[] array, int startIndex, int len) { 106 long myTime = Utility.bytesToLong(array, startIndex); 107 long myId = Utility.bytesToLong(array, startIndex+8); 108 109 return new SimpleSessionKey(myTime, myId); 110 } 111 112 } 113 114 class SimpleSessionKey 115 implements java.io.Serializable 116 { 117 118 long time; 119 long id; 120 121 public SimpleSessionKey(long time, long id) { 122 this.time = time; 123 this.id = id; 124 } 125 126 public int hashCode() { 127 return (int) id; 128 } 129 130 public boolean equals(Object otherObj) { 131 if (otherObj instanceof SimpleSessionKey) { 132 SimpleSessionKey other = (SimpleSessionKey) otherObj; 133 return ((id == other.id) && (time == other.time)); 134 } 135 136 return false; 137 } 138 139 public String toString() { 140 return "" + time + "-" + id; 141 } 142 143 } 144 145 | Popular Tags |