KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > utilities > dods > DODSUtilities


1 /* DODSUtilities.java */
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 JavaDoc;
12 import java.io.StringWriter JavaDoc;
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.math.BigDecimal JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
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 /**
26  * DODSUtilities represents a <i>toolbox</i> for DODS based modules.
27  * There are static methods only in charge of some specific task.
28  *
29  * @author Zoran Milakovic
30  * @author Vladimir Puskas
31  * @author Sasa Bojanic
32  */

33 public class DODSUtilities {
34
35    public final static String JavaDoc NULL_VALUE_FOR_PROCID = "~@#proc_id#@~";
36
37    private static Map JavaDoc counterCachesSizes=new HashMap JavaDoc();
38    private static Map JavaDoc counterCachesMax=new HashMap JavaDoc();
39    private static Map JavaDoc counterCachesCurrent=new HashMap JavaDoc();
40    private static Map JavaDoc counterCachesNext=new HashMap JavaDoc();
41
42    private static Properties JavaDoc props;
43
44    private static final String JavaDoc COUNTER_CACHE_PREFFIX="DODS.IdGenerator.";
45    private static final String JavaDoc COUNTER_CACHE_POSTFIX=".CacheSize";
46    private static final String JavaDoc 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 constructor to prevent instantiate of Counter
51
private DODSUtilities() { }
52
53    /**
54     * Gets next value for counter named in objectName parameter
55     * if counter exists, otherwise new one will be started with value 1.
56     *
57     * @param objectName name of a counter
58     *
59     * @return next value available for the counter
60     *
61     * @exception RootException thrown if counter value cannot be acquired
62     *
63     */

64    public static synchronized BigDecimal JavaDoc getNext(String JavaDoc 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)); // next free
75
counterCachesNext.put(objectName,((BigDecimal JavaDoc)counterCachesNext.get(objectName)).add(new BigDecimal JavaDoc("1")));
76          return (BigDecimal JavaDoc)counterCachesCurrent.get(objectName);
77       } catch (Exception JavaDoc e) {
78          throw new RootException("Counter Id Allocator failed to allocate object id.",e);
79       }
80    }
81
82    private static void updateCaches (String JavaDoc objectName) throws RootException {
83       DBTransaction trans = null;
84       // try it 50 times
85
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 JavaDoc dbNext;
95             if (doCounter == null) {
96                // insert new Counter
97
doCounter = CounterDO.createVirgin(trans);
98                doCounter.setName(objectName);
99                dbNext = new BigDecimal JavaDoc("1");
100             } else {
101                // increment existing Counter
102
dbNext = doCounter.getThe_number();
103             }
104
105             BigDecimal JavaDoc 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 JavaDoc e) {
119             if (trans != null) {
120                trans.release();
121             }
122          }
123       }
124       // this line should normaly never be executed
125
throw new RootException("Can't allocate Id for object table "+objectName);
126    }
127
128    private static long getCacheSize (String JavaDoc objectName) {
129       long cacheSize=-1;
130       Object JavaDoc cs=counterCachesSizes.get(objectName);
131       if (cs==null) {
132          String JavaDoc propName=COUNTER_CACHE_PREFFIX+objectName+COUNTER_CACHE_POSTFIX;
133          String JavaDoc cSize=DODSUtilities.props.getProperty(propName);
134          String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc(cacheSize));
164       } else {
165          cacheSize=((Long JavaDoc)cs).longValue();
166       }
167       return cacheSize;
168    }
169
170
171    /**
172     * DODS runtime initialization.
173     *
174     * @param props properites which will be used for init
175     *
176     * @exception RootException
177     *
178     */

179    public static synchronized void init(final Properties JavaDoc props) throws RootException {
180       if (DODSUtilities.props==null) {
181          DODSUtilities.props=new Properties JavaDoc(props);
182       }
183       if (null == DODS.getDatabaseManager()) {
184          Logger enhydraLogger = new Log4jLogger(true) {
185             public void configure(String JavaDoc s) {
186                PropertyConfigurator.configure(props);
187             }
188          };
189          DODS.registerDefaultLogChannel(enhydraLogger
190                                            .getChannel("DatabaseManager"));
191          DODS.setThreading(false);
192          try {
193             StringWriter JavaDoc sw = new StringWriter JavaDoc();
194             for (Iterator JavaDoc it = props.entrySet().iterator(); it.hasNext();) {
195                Map.Entry JavaDoc me = (Map.Entry JavaDoc)it.next();
196                String JavaDoc key =((String JavaDoc)me.getKey()).trim();
197                if (key.startsWith("DatabaseManager.")) {
198                   sw.write(key+" = "+me.getValue()+"\n");
199                }
200             }
201             /**
202             ConfigParser cp = new ConfigParser(new StringReader(sw.getBuffer().toString()));
203             ConfigFile cf = new ConfigFile();
204             cp.process(cf);
205             */

206             
207             ByteArrayInputStream JavaDoc baris = new ByteArrayInputStream JavaDoc(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 JavaDoc e) {
214             throw new RootException("DODS init problem.", e);
215          }
216       }
217    }
218 }
219 /* End of DODSUtilities.java */
220
221
Popular Tags