KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > sfsb > util > SimpleKeyGenerator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 /**
30  * A utility class that generates stateful session keys using two longs
31  * The session id generated by this class is guarenteed to be unique as
32  * long as the system clock is never reset to a previous value
33  *
34  * The hashCode of the SessionKey generated by SimpleKeyGenerator
35  * also allows uniform distribution of keys when hashed in a HashMap
36  *
37  * @author Mahesh Kannan
38  */

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     //Initial suffix
61
this.suffix = System.currentTimeMillis();
62
63     //Inital isCounter value
64
this.idCounter = 0;
65
66     }
67
68     /**
69      * Create and return the sessionKey.
70      * @return the sessionKey object
71      */

72     public Object JavaDoc createSessionKey() {
73         int id = 0;
74         synchronized (this) {
75             id = idCounter++;
76             if (id < 0) {
77                 //idCounter wrapped around!!
78
id = idCounter = 0;
79                 suffix = System.currentTimeMillis();
80             }
81         }
82
83         return new SimpleSessionKey(prefix, suffix, id);
84     }
85     
86     /**
87      * Called from the Container before publishing an IOR.
88      * The method must convert the sessionKey into a byte[]
89      * @return A byte[] representation of the key. The byte[]
90      * could be created using serialization.
91      */

92     public byte[] keyToByteArray(Object JavaDoc 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      /**
104       * Return the sessionKey that represents the sessionKey.
105       * This has to be super efficient as the container calls this
106       * method on every invocation. Two objects obtained from identical
107       * byte[] must satisfy both o1.equals(o2) and
108       * o1.hashCode() == o2.hashCode()
109       * @return the sessionKey object
110       */

111     public Object JavaDoc 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         //Inital isCounter value
137
this.idCounter = 0;
138
139         //Set the default suffix value
140
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 JavaDoc
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 JavaDoc 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 JavaDoc toString() {
183         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
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