KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > sfsb > 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;
25
26 import com.sun.ejb.spi.sfsb.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     private int idCounter;
44     private long time;
45     private long prefix;
46
47     public SimpleKeyGenerator() {
48         //Inital isCounter value
49
this.idCounter = 0;
50
51         //Set the default time value
52
this.time = System.currentTimeMillis();
53
54         //Default value for prefix
55
this.prefix = 0;
56     }
57
58     public void setPrefix(int val) {
59         this.prefix = val;
60         this.prefix = this.prefix << 32;
61     }
62
63     /**
64      * Create and return the sessionKey.
65      * @return the sessionKey object
66      */

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

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

105     public Object JavaDoc 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 JavaDoc
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 JavaDoc 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 JavaDoc toString() {
140         return "" + time + "-" + id;
141     }
142
143 }
144     
145
Popular Tags