KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > utilities > hibernate > HibernateUtilities


1 /*
2  * Shark Hibernate Utilities - Open Wide
3  */

4
5 package org.enhydra.shark.utilities.hibernate;
6
7 import java.math.BigDecimal JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import net.sf.hibernate.Query;
13 import net.sf.hibernate.Session;
14
15 import org.enhydra.shark.api.RootException;
16
17 /**
18  * HibernateUtilities represents a <i>toolbox</i> for Hibernate based modules.
19  * There are static methods only in charge of some specific task.
20  *
21  * @author Vladislav Pernin
22  */

23 public final class HibernateUtilities {
24
25     public static final String JavaDoc HibernateActivityStateClassName = "class org.enhydra.shark.instancepersistence.data.HibernateActivityState";
26     public static final String JavaDoc HibernateProcessStateClassName = "class org.enhydra.shark.instancepersistence.data.HibernateProcessState";
27     public static final String JavaDoc HibernateResourceClassName = "class org.enhydra.shark.instancepersistence.HibernateResource";
28     public static final String JavaDoc HibernateProcessMgrClassName = "class org.enhydra.shark.instancepersistence.HibernateProcessMgr";
29     public static final String JavaDoc HibernateProcessClassName = "class org.enhydra.shark.instancepersistence.HibernateProcess";
30     public static final String JavaDoc HibernateProcessVariableClassName = "class org.enhydra.shark.instancepersistence.HibernateProcessVariable";
31     public static final String JavaDoc HibernateActivityClassName = "class org.enhydra.shark.instancepersistence.HibernateActivity";
32     public static final String JavaDoc HibernateActivityVariableClassName = "class org.enhydra.shark.instancepersistence.HibernateActivityVariable";
33     public static final String JavaDoc HibernateAndJoinEntryClassName = "class org.enhydra.shark.instancepersistence.HibernateAndJoinEntry";
34     public static final String JavaDoc HibernateDeadlineClassName = "class org.enhydra.shark.instancepersistence.HibernateDeadline";
35     public static final String JavaDoc HibernateAssignmentClassName = "class org.enhydra.shark.instancepersistence.HibernateAssignment";
36     public static final String JavaDoc HibernateProcessRequesterClassName = "class org.enhydra.shark.instancepersistence.HibernateProcessRequester";
37     
38     public static final String JavaDoc HibernateEventTypeClassName = "class org.enhydra.shark.eventaudit.data.HibernateEventType";
39     public static final String JavaDoc HibernateProcessStateEventAuditClassName = "class org.enhydra.shark.eventaudit.data.HibernateProcessStateEventAudit";
40     public static final String JavaDoc HibernateActivityStateEventAuditClassName = "class org.enhydra.shark.eventaudit.data.HibernateActivityStateEventAudit";
41     public static final String JavaDoc HibernateStateEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateStateEventAudit";
42     public static final String JavaDoc HibernateCreateProcessEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateCreateProcessEventAudit";
43     public static final String JavaDoc HibernateAssignmentEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateAssignmentEventAudit";
44     public static final String JavaDoc HibernateDataEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateDataEventAudit";
45         
46     public final static String JavaDoc NULL_VALUE_FOR_PROCID = "~@#proc_id#@~";
47     
48     private static Map JavaDoc counterCachesSizes=new HashMap JavaDoc();
49     private static Map JavaDoc counterCachesMax=new HashMap JavaDoc();
50     private static Map JavaDoc counterCachesCurrent=new HashMap JavaDoc();
51     private static Map JavaDoc counterCachesNext=new HashMap JavaDoc();
52
53     private static Properties JavaDoc props;
54
55     private static final String JavaDoc COUNTER_CACHE_PREFFIX="Hibernate.IdGenerator.";
56     private static final String JavaDoc COUNTER_CACHE_POSTFIX=".CacheSize";
57     private static final String JavaDoc COUNTER_DEFAULT_CACHE="Hibernate.defaults.IdGenerator.CacheSize";
58     private static final long DEFAULT_CACHE_SIZE=100;
59
60     private static int allocTryCount=50;
61     
62    //private constructor to prevent instantiate of Counter
63
private HibernateUtilities() { }
64
65    /**
66     * Gets next value for counter named in objectName parameter
67     * if counter exists, otherwise new one will be started with value 1.
68     *
69     * @param objectName name of a counter
70     *
71     * @return next value available for the counter
72     *
73     * @exception RootException thrown if counter value cannot be acquired
74     *
75     */

76    public static synchronized BigDecimal JavaDoc getNext(String JavaDoc objectName) throws RootException {
77     if (objectName==null) {
78         throw new RootException("Object name parameter can't be null");
79     }
80     try {
81         if (counterCachesNext.get(objectName) == null ||
82                 counterCachesMax.get(objectName) == null ||
83                 counterCachesNext.get(objectName).equals(counterCachesMax.get(objectName))) {
84             updateCaches(objectName);
85         }
86         counterCachesCurrent.put(objectName,counterCachesNext.get(objectName)); // next free
87
counterCachesNext.put(objectName,((BigDecimal JavaDoc)counterCachesNext.get(objectName)).add(new BigDecimal JavaDoc("1")));
88         return (BigDecimal JavaDoc)counterCachesCurrent.get(objectName);
89     } catch (Exception JavaDoc e) {
90         e.printStackTrace();
91         throw new RootException("Counter Id Allocator failed to allocate object id.",e);
92     }
93    }
94
95    private static void updateCaches (String JavaDoc objectName) throws RootException {
96     SharkHibernateUtilityTransaction t = null;
97     // try it 50 times
98
for (int iTry = 0; iTry < allocTryCount; iTry++) {
99         try {
100             t = (SharkHibernateUtilityTransaction)new HibernateUtilityTransactionFactory().createTransaction();
101             Session session = t.getSession();
102             
103             Query qCounter = session.createQuery("from HibernateCounter counter where counter.name = :keyValueParam");
104             qCounter.setString("keyValueParam", objectName);
105             HibernateCounter countObj = (HibernateCounter)qCounter.uniqueResult();
106
107             BigDecimal JavaDoc dbNext = new BigDecimal JavaDoc(1);
108             BigDecimal JavaDoc dbLast = new BigDecimal JavaDoc(1);
109             if (countObj == null){
110                 // insert new Counter
111
countObj = new HibernateCounter();
112                 dbNext = dbNext.add(BigDecimal.valueOf(getCacheSize(objectName)));
113                 countObj.setName(objectName);
114                 countObj.setNumber(dbNext);
115                 session.save(countObj);
116                 t.commit();
117             } else {
118                 // increment existing Counter
119
dbNext = countObj.getNumber();
120                 dbLast = dbNext;
121                 dbNext = dbNext.add(BigDecimal.valueOf(getCacheSize(objectName)));
122                 countObj.setNumber(dbNext);
123                 session.update(countObj);
124                 t.commit();
125             }
126             
127             counterCachesNext.put(objectName,dbLast);
128             counterCachesMax.put(objectName,dbNext);
129             
130             session.clear();
131             session.close();
132     
133             return;
134         } catch (Exception JavaDoc e) {
135             e.printStackTrace();
136             if (t != null) {
137                 t = null;
138             }
139         }
140     }
141     // this line should normaly never be executed
142
throw new RootException("Can't allocate Id for object table "+objectName);
143    }
144    
145    private static long getCacheSize (String JavaDoc objectName) {
146     long cacheSize=-1;
147     Object JavaDoc cs=counterCachesSizes.get(objectName);
148     if (cs==null) {
149         String JavaDoc propName=COUNTER_CACHE_PREFFIX+objectName+COUNTER_CACHE_POSTFIX;
150         String JavaDoc cSize=props.getProperty(propName);
151         String JavaDoc defCSize=props.getProperty(COUNTER_DEFAULT_CACHE);
152         if (cSize!=null) {
153             try {
154                 cacheSize=Long.parseLong(cSize);
155                 if (cacheSize<=0) {
156                     cacheSize=-1;
157                     System.err.println("Wrong value for "+objectName+" cache size");
158                 }
159             } catch (Exception JavaDoc ex) {
160                 cacheSize=-1;
161                 System.err.println("Wrong value for "+objectName+" cache size");
162             }
163         }
164         if (cacheSize==-1) {
165             if (defCSize!=null) {
166                 try {
167                     cacheSize=Long.parseLong(defCSize);
168                     if (cacheSize<=0) {
169                         cacheSize=DEFAULT_CACHE_SIZE;
170                         System.err.println("Wrong value for default cache size, using default size "+DEFAULT_CACHE_SIZE);
171                     }
172                 } catch (Exception JavaDoc ex) {
173                     cacheSize=DEFAULT_CACHE_SIZE;
174                     System.err.println("Wrong value for default cache size, using default size "+DEFAULT_CACHE_SIZE);
175                 }
176             } else {
177                 cacheSize=DEFAULT_CACHE_SIZE;
178             }
179         }
180         counterCachesSizes.put(objectName,new Long JavaDoc(cacheSize));
181     } else {
182         cacheSize=((Long JavaDoc)cs).longValue();
183     }
184     return cacheSize;
185    }
186    
187    
188    /**
189     * Hibernate runtime initialization.
190     *
191     * @param propsToSet properites which will be used for init
192     *
193     * @exception RootException
194     *
195     */

196    public static synchronized void init(Properties JavaDoc propsToSet) throws RootException {
197          try {
198             props = propsToSet;
199          } catch (Exception JavaDoc e) {
200             throw new RootException("Hibernate init problem.", e);
201       }
202    }
203 }
204
205
Popular Tags