1 23 package com.sun.enterprise.util; 24 25 import java.rmi.RemoteException ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import java.util.Hashtable ; 29 import com.sun.enterprise.log.Log; 30 31 import java.util.logging.*; 33 import com.sun.logging.*; 34 36 37 41 class SimpleUniqueValueGenerator implements UniqueValueGenerator { 42 43 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 45 47 private static Hashtable contextBlocks_; 49 50 private static UniqueValueGeneratorBackend generatorBackend_; 52 53 private static String generatorBackendId_; 56 57 private String context_; 59 60 static { 61 generatorBackendId_ = null; 62 generatorBackend_ = null; 63 contextBlocks_ = new Hashtable (); 64 } 65 66 71 SimpleUniqueValueGenerator(String context) { 72 context_ = context; 73 } 74 75 private static synchronized 76 UniqueValueGeneratorBackend getBackendGenerator() 77 throws Exception { 78 if( generatorBackend_ == null ) { 79 InitialContext jndiContext = new InitialContext (); 80 generatorBackend_ = (UniqueValueGeneratorBackend) 81 jndiContext.lookup(UniqueValueGeneratorBackend.JNDI_NAME); 82 } 83 return generatorBackend_; 84 } 85 86 private static synchronized String getGeneratorBackendId() 87 throws UniqueValueGeneratorException { 88 if( generatorBackendId_ == null ) { 89 try { 90 UniqueValueGeneratorBackend backend = getBackendGenerator(); 91 generatorBackendId_ = backend.getGeneratorId(); 92 } catch(Exception e) { 93 96 _logger.log(Level.SEVERE,"enterprise_util.excep_suidgen_getgenbackendid",e); 98 100 throw new UniqueValueGeneratorException(e.getMessage()); 101 } 102 } 103 return generatorBackendId_; 104 } 105 106 private static synchronized long nextNumberInternal(String 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 e) { 121 124 _logger.log(Level.SEVERE,"enterprise_util.excep_suidgen_nextnuminternal",e); 126 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 nextId() throws UniqueValueGeneratorException { 138 return getGeneratorBackendId() + "_" + nextNumber(); 139 } 140 141 public String getContext() { 142 return context_; 143 } 144 145 } 146 | Popular Tags |