1 2 3 package org.enhydra.shark.utilities.dods; 4 5 import com.lutris.appserver.server.sql.DBTransaction; 6 import com.lutris.appserver.server.sql.DatabaseManager; 7 import com.lutris.appserver.server.sql.StandardDatabaseManager; 8 import com.lutris.util.Config; 9 import com.lutris.util.ConfigFile; 10 import com.lutris.util.ConfigParser; 11 import java.io.StringReader ; 12 import java.io.StringWriter ; 13 import java.io.ByteArrayInputStream ; 14 import java.math.BigDecimal ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 import org.enhydra.dods.DODS; 20 import org.enhydra.shark.api.RootException; 21 import com.lutris.logging.Logger; 22 import com.lutris.logging.Log4jLogger; 23 import org.apache.log4j.PropertyConfigurator; 24 25 33 public class DODSUtilities { 34 35 public final static String NULL_VALUE_FOR_PROCID = "~@#proc_id#@~"; 36 37 private static Map counterCachesSizes=new HashMap (); 38 private static Map counterCachesMax=new HashMap (); 39 private static Map counterCachesCurrent=new HashMap (); 40 private static Map counterCachesNext=new HashMap (); 41 42 private static Properties props; 43 44 private static final String COUNTER_CACHE_PREFFIX="DODS.IdGenerator."; 45 private static final String COUNTER_CACHE_POSTFIX=".CacheSize"; 46 private static final String COUNTER_DEFAULT_CACHE="DODS.defaults.IdGenerator.CacheSize"; 47 private static final long DEFAULT_CACHE_SIZE=100; 48 49 private static int allocTryCount=50; 50 private DODSUtilities() { } 52 53 64 public static synchronized BigDecimal getNext(String objectName) throws RootException { 65 if (objectName==null) { 66 throw new RootException("Object name parameter can't be null"); 67 } 68 try { 69 if (counterCachesNext.get(objectName) == null || 70 counterCachesMax.get(objectName) == null || 71 counterCachesNext.get(objectName).equals(counterCachesMax.get(objectName))) { 72 updateCaches(objectName); 73 } 74 counterCachesCurrent.put(objectName,counterCachesNext.get(objectName)); counterCachesNext.put(objectName,((BigDecimal )counterCachesNext.get(objectName)).add(new BigDecimal ("1"))); 76 return (BigDecimal )counterCachesCurrent.get(objectName); 77 } catch (Exception e) { 78 throw new RootException("Counter Id Allocator failed to allocate object id.",e); 79 } 80 } 81 82 private static void updateCaches (String objectName) throws RootException { 83 DBTransaction trans = null; 84 for (int iTry = 0; iTry < allocTryCount; iTry++) { 86 try { 87 88 trans = DODS.getDatabaseManager().createTransaction(); 89 CounterQuery qryCounter = new CounterQuery(trans); 90 qryCounter.requireUniqueInstance(); 91 qryCounter.setQueryName(objectName); 92 CounterDO doCounter = qryCounter.getNextDO(); 93 94 BigDecimal dbNext; 95 if (doCounter == null) { 96 doCounter = CounterDO.createVirgin(trans); 98 doCounter.setName(objectName); 99 dbNext = new BigDecimal ("1"); 100 } else { 101 dbNext = doCounter.getThe_number(); 103 } 104 105 BigDecimal dbLast = dbNext; 106 107 dbNext = dbNext.add(BigDecimal.valueOf(getCacheSize(objectName))); 108 109 doCounter.setThe_number(dbNext); 110 doCounter.save(trans); 111 trans.commit(); 112 trans.release(); 113 114 counterCachesNext.put(objectName,dbLast); 115 counterCachesMax.put(objectName,dbNext); 116 117 return; 118 } catch (Exception e) { 119 if (trans != null) { 120 trans.release(); 121 } 122 } 123 } 124 throw new RootException("Can't allocate Id for object table "+objectName); 126 } 127 128 private static long getCacheSize (String objectName) { 129 long cacheSize=-1; 130 Object cs=counterCachesSizes.get(objectName); 131 if (cs==null) { 132 String propName=COUNTER_CACHE_PREFFIX+objectName+COUNTER_CACHE_POSTFIX; 133 String cSize=DODSUtilities.props.getProperty(propName); 134 String defCSize=DODSUtilities.props.getProperty(COUNTER_DEFAULT_CACHE); 135 if (cSize!=null) { 136 try { 137 cacheSize=Long.parseLong(cSize); 138 if (cacheSize<=0) { 139 cacheSize=-1; 140 System.err.println("Wrong value for "+objectName+" cache size"); 141 } 142 } catch (Exception ex) { 143 cacheSize=-1; 144 System.err.println("Wrong value for "+objectName+" cache size"); 145 } 146 } 147 if (cacheSize==-1) { 148 if (defCSize!=null) { 149 try { 150 cacheSize=Long.parseLong(defCSize); 151 if (cacheSize<=0) { 152 cacheSize=DEFAULT_CACHE_SIZE; 153 System.err.println("Wrong value for default cache size, using default size "+DEFAULT_CACHE_SIZE); 154 } 155 } catch (Exception ex) { 156 cacheSize=DEFAULT_CACHE_SIZE; 157 System.err.println("Wrong value for default cache size, using default size "+DEFAULT_CACHE_SIZE); 158 } 159 } else { 160 cacheSize=DEFAULT_CACHE_SIZE; 161 } 162 } 163 counterCachesSizes.put(objectName,new Long (cacheSize)); 164 } else { 165 cacheSize=((Long )cs).longValue(); 166 } 167 return cacheSize; 168 } 169 170 171 179 public static synchronized void init(final Properties props) throws RootException { 180 if (DODSUtilities.props==null) { 181 DODSUtilities.props=new Properties (props); 182 } 183 if (null == DODS.getDatabaseManager()) { 184 Logger enhydraLogger = new Log4jLogger(true) { 185 public void configure(String s) { 186 PropertyConfigurator.configure(props); 187 } 188 }; 189 DODS.registerDefaultLogChannel(enhydraLogger 190 .getChannel("DatabaseManager")); 191 DODS.setThreading(false); 192 try { 193 StringWriter sw = new StringWriter (); 194 for (Iterator it = props.entrySet().iterator(); it.hasNext();) { 195 Map.Entry me = (Map.Entry )it.next(); 196 String key =((String )me.getKey()).trim(); 197 if (key.startsWith("DatabaseManager.")) { 198 sw.write(key+" = "+me.getValue()+"\n"); 199 } 200 } 201 206 207 ByteArrayInputStream baris = new ByteArrayInputStream (sw.getBuffer().toString().getBytes()); 208 ConfigFile cf = new ConfigFile(baris); 209 210 Config dmConfig = new Config(cf.getConfig().getSection("DatabaseManager")); 211 DatabaseManager dbMgr = new StandardDatabaseManager(dmConfig); 212 DODS.registerDefault(dbMgr); 213 } catch (Exception e) { 214 throw new RootException("DODS init problem.", e); 215 } 216 } 217 } 218 } 219 220 221 | Popular Tags |