KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > instance > UniqueIdGenerator


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 /*
25  * @(#) UniqueIdGenerator.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.instance;
38
39 import java.util.logging.Logger JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import com.sun.logging.LogDomains;
42
43
44 /**
45  * Generates unique id for an application or stand alone ejb module.
46  *
47  * It uses the low order 6 bytes of the system current time. The remaining
48  * 2 bytes are reserved for the beans inside the application and stand
49  * alone ejb module.
50  *
51  * The unique id generated by this generator will eventually wrap after
52  * 2^48 milli-seconds (8,925.25 years).
53  *
54  * The two bytes reserved for the beans in the application or stand alone ejb
55  * module will allow max 2^16 (65,536) beans.
56  *
57  * @author Nazrul Islam
58  * @since JDK1.4
59  */

60 public class UniqueIdGenerator {
61
62     /** last unique id given out by this id generator */
63     private long _lastUid = 0;
64
65     /** the singleton instance */
66     private static UniqueIdGenerator _instance = null;
67     
68     private static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
69
70     static {
71         _instance = new UniqueIdGenerator();
72     }
73
74     private UniqueIdGenerator() { }
75
76     /**
77      * Returns the singleton instance of this class.
78      *
79      * @return the singleton instance
80      */

81     public static UniqueIdGenerator getInstance() {
82         return _instance;
83     }
84
85     /**
86      * Returns the next unique id for an application or
87      * stand alone ejb module.
88      *
89      * @return the next unique id
90      */

91     public long getNextUniqueId() {
92
93         synchronized (this) {
94             while (true) {
95                 long uid = System.currentTimeMillis();
96
97                 // uses the low order 6 bytes as unique id;
98
// remaining 2 bytes are reserved for the beans inside the app
99
uid = (uid << 16);
100
101                 // ensures that we have a unique id
102
if (this._lastUid != uid) {
103                     this._lastUid = uid;
104                     break;
105                 } else {
106                     try {
107                         Thread.currentThread().sleep(1);
108                     } catch (InterruptedException JavaDoc ie) {
109                         // log debug msg here
110
_logger.log(Level.WARNING,"Thread.sleep interrupted ",ie);
111                     }
112                 }
113             }
114             return this._lastUid;
115         }
116     }
117 }
118
119
Popular Tags