KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > UniqueValueGeneratorBackendImpl


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 package com.sun.enterprise.util;
24
25 import javax.rmi.PortableRemoteObject JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.util.Hashtable JavaDoc;
28
29 //START OF IASRI 4660742
30
import java.util.logging.*;
31 import com.sun.logging.*;
32 //END OF IASRI 4660742
33

34
35 /**
36  * Doles out blocks of unique numbers for each context. A single instance
37  * of this object is registered per server instance(single-vm mode or
38  * multi-vm mode).
39  *
40  * @author Kenneth Saks
41  */

42 public class UniqueValueGeneratorBackendImpl extends PortableRemoteObject JavaDoc implements UniqueValueGeneratorBackend {
43
44     // START OF IASRI 4660742
45
static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
46     // END OF IASRI 4660742
47

48     // START OF IASRI 4679641
49
// private static final boolean debug = false;
50
private static final boolean debug = com.sun.enterprise.util.logging.Debug.enabled;
51     // END OF IASRI 4679641
52

53     private static final long BLOCK_SIZE = 100;
54     private static final long NUM_BLOCKS_PER_CONTEXT =
55         (Long.MAX_VALUE / BLOCK_SIZE);
56
57     private String JavaDoc id_;
58     private Hashtable JavaDoc contexts_;
59
60     public UniqueValueGeneratorBackendImpl() throws RemoteException JavaDoc {
61         contexts_ = new Hashtable JavaDoc();
62         id_ = System.currentTimeMillis() + "";
63     }
64
65     public String JavaDoc getGeneratorId() throws RemoteException JavaDoc {
66         return id_;
67     }
68
69     public UniqueValueBlock getNextValueBlock(String JavaDoc context)
70         throws RemoteException JavaDoc {
71
72         int blockIndex = 0;
73
74         synchronized( this ) {
75             if( contexts_.containsKey(context) ) {
76                 Integer JavaDoc currentBlock = (Integer JavaDoc) contexts_.get(context);
77                 blockIndex = currentBlock.intValue();
78             }
79             contexts_.put(context, new Integer JavaDoc(blockIndex + 1));
80         }
81
82         if( blockIndex >= NUM_BLOCKS_PER_CONTEXT ) {
83             throw new RemoteException JavaDoc("Block overflow");
84         }
85
86         if( debug ) {
87               /** IASRI 4660742
88             System.out.println("Returning block " + blockIndex +
89                                " of size " + BLOCK_SIZE + " for context " +
90                                context);
91               **/

92               // START OF IASRI 4660742
93
if (_logger.isLoggable(Level.FINE)) {
94                   _logger.log(Level.FINE,"Returning block " + blockIndex +
95                        " of size " + BLOCK_SIZE + " for context " + context);
96
97                }
98               // END OF IASRI 4660742
99

100         }
101         return new UniqueValueBlock((blockIndex * BLOCK_SIZE), BLOCK_SIZE);
102     }
103
104 }
105
Popular Tags