KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.security.SecureRandom JavaDoc;
27
28 import com.sun.ejb.spi.sfsb.util.SFSBUUIDUtil;
29 import com.sun.enterprise.util.Utility;
30
31 /**
32  * A utility class that generates stateful session keys using two longs
33  * The session id generated by this class is guarenteed to be unique as
34  * long as the system clock is never reset to a previous value
35  *
36  * The hashCode of the SessionKey generated by ScrambledKeyGenerator
37  * also allows uniform distribution of keys when hashed in a HashMap
38  *
39  * @author Mahesh Kannan
40  */

41 public class ScrambledKeyGenerator
42     extends SimpleKeyGenerator
43 {
44    
45     private SecureRandom JavaDoc random = new SecureRandom JavaDoc();
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         //Inital isCounter value
82
this.idCounter = 0;
83
84         //Set the default time value
85
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     /**
97      * Create and return the sessionKey.
98      * @return the sessionKey object
99      */

100     public Object JavaDoc createSessionKey() {
101         int id = 0;
102         synchronized (this) {
103             id = ++idCounter;
104             if (id < 0) {
105                 //idCounter wrapped around!!
106
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 + System.identityHashCode(new Object()), id
117
suffix + random.nextInt(), id
118     );
119     }
120     
121 }
122     
123
Popular Tags