KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.rmi.RemoteException JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import com.sun.enterprise.log.Log;
30
31 //START OF IASRI 4660742
32
import java.util.logging.*;
33 import com.sun.logging.*;
34 //END OF IASRI 4660742
35

36
37 /**
38  *
39  * @author Kenneth Saks
40  */

41 class SimpleUniqueValueGenerator implements UniqueValueGenerator {
42
43     // START OF IASRI 4660742
44
static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
45     // END OF IASRI 4660742
46

47     // Map context name to value block.
48
private static Hashtable JavaDoc contextBlocks_;
49
50     // Remote object that provides the unique number service.
51
private static UniqueValueGeneratorBackend generatorBackend_;
52
53     // Id of remote generator that is guaranteed to be unique
54
// across invocations of a single j2ee server.
55
private static String JavaDoc generatorBackendId_;
56
57     // Namespace within which unique values will be generated.
58
private String JavaDoc context_;
59
60     static {
61         generatorBackendId_ = null;
62         generatorBackend_ = null;
63         contextBlocks_ = new Hashtable JavaDoc();
64     }
65
66     /**
67      * Use package-level access since clients should use a
68      * UniqueValueGeneratorFactory to create concrete instances
69      * this class.
70      */

71     SimpleUniqueValueGenerator(String JavaDoc context) {
72         context_ = context;
73     }
74
75     private static synchronized
76         UniqueValueGeneratorBackend getBackendGenerator()
77         throws Exception JavaDoc {
78         if( generatorBackend_ == null ) {
79             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
80             generatorBackend_ = (UniqueValueGeneratorBackend)
81                 jndiContext.lookup(UniqueValueGeneratorBackend.JNDI_NAME);
82         }
83         return generatorBackend_;
84     }
85
86     private static synchronized String JavaDoc getGeneratorBackendId()
87         throws UniqueValueGeneratorException {
88         if( generatorBackendId_ == null ) {
89             try {
90                 UniqueValueGeneratorBackend backend = getBackendGenerator();
91                 generatorBackendId_ = backend.getGeneratorId();
92             } catch(Exception JavaDoc e) {
93                 /** IASRI 4660742
94                 Log.err.println(e);
95                 **/

96                     // START OF IASRI 4660742
97
_logger.log(Level.SEVERE,"enterprise_util.excep_suidgen_getgenbackendid",e);
98                 // END OF IASRI 4660742
99

100                 throw new UniqueValueGeneratorException(e.getMessage());
101             }
102         }
103         return generatorBackendId_;
104     }
105
106     private static synchronized long nextNumberInternal(String JavaDoc context)
107         throws UniqueValueGeneratorException {
108
109         UniqueValueBlock valueBlock = null;
110         try {
111             UniqueValueGeneratorBackend generatorBackend =
112                 getBackendGenerator();
113
114             valueBlock = (UniqueValueBlock) contextBlocks_.get(context);
115             
116             if( (valueBlock == null) || (!valueBlock.hasNext()) ) {
117                 valueBlock = generatorBackend.getNextValueBlock(context);
118                 contextBlocks_.put(context, valueBlock);
119             }
120         } catch(Exception JavaDoc e) {
121             /** IASRI 4660742
122             Log.err.println(e);
123             **/

124               // START OF IASRI 4660742
125
_logger.log(Level.SEVERE,"enterprise_util.excep_suidgen_nextnuminternal",e);
126             // END OF IASRI 4660742
127
throw new UniqueValueGeneratorException(e.getMessage());
128         }
129         
130         return valueBlock.next();
131     }
132
133     public long nextNumber() throws UniqueValueGeneratorException {
134         return nextNumberInternal(getContext());
135     }
136     
137     public String JavaDoc nextId() throws UniqueValueGeneratorException {
138         return getGeneratorBackendId() + "_" + nextNumber();
139     }
140
141     public String JavaDoc getContext() {
142         return context_;
143     }
144     
145 }
146
Popular Tags