1 4 5 package org.enhydra.shark.utilities.hibernate; 6 7 import java.math.BigDecimal ; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 import java.util.Properties ; 11 12 import net.sf.hibernate.Query; 13 import net.sf.hibernate.Session; 14 15 import org.enhydra.shark.api.RootException; 16 17 23 public final class HibernateUtilities { 24 25 public static final String HibernateActivityStateClassName = "class org.enhydra.shark.instancepersistence.data.HibernateActivityState"; 26 public static final String HibernateProcessStateClassName = "class org.enhydra.shark.instancepersistence.data.HibernateProcessState"; 27 public static final String HibernateResourceClassName = "class org.enhydra.shark.instancepersistence.HibernateResource"; 28 public static final String HibernateProcessMgrClassName = "class org.enhydra.shark.instancepersistence.HibernateProcessMgr"; 29 public static final String HibernateProcessClassName = "class org.enhydra.shark.instancepersistence.HibernateProcess"; 30 public static final String HibernateProcessVariableClassName = "class org.enhydra.shark.instancepersistence.HibernateProcessVariable"; 31 public static final String HibernateActivityClassName = "class org.enhydra.shark.instancepersistence.HibernateActivity"; 32 public static final String HibernateActivityVariableClassName = "class org.enhydra.shark.instancepersistence.HibernateActivityVariable"; 33 public static final String HibernateAndJoinEntryClassName = "class org.enhydra.shark.instancepersistence.HibernateAndJoinEntry"; 34 public static final String HibernateDeadlineClassName = "class org.enhydra.shark.instancepersistence.HibernateDeadline"; 35 public static final String HibernateAssignmentClassName = "class org.enhydra.shark.instancepersistence.HibernateAssignment"; 36 public static final String HibernateProcessRequesterClassName = "class org.enhydra.shark.instancepersistence.HibernateProcessRequester"; 37 38 public static final String HibernateEventTypeClassName = "class org.enhydra.shark.eventaudit.data.HibernateEventType"; 39 public static final String HibernateProcessStateEventAuditClassName = "class org.enhydra.shark.eventaudit.data.HibernateProcessStateEventAudit"; 40 public static final String HibernateActivityStateEventAuditClassName = "class org.enhydra.shark.eventaudit.data.HibernateActivityStateEventAudit"; 41 public static final String HibernateStateEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateStateEventAudit"; 42 public static final String HibernateCreateProcessEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateCreateProcessEventAudit"; 43 public static final String HibernateAssignmentEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateAssignmentEventAudit"; 44 public static final String HibernateDataEventAuditClassName = "class org.enhydra.shark.eventaudit.HibernateDataEventAudit"; 45 46 public final static String NULL_VALUE_FOR_PROCID = "~@#proc_id#@~"; 47 48 private static Map counterCachesSizes=new HashMap (); 49 private static Map counterCachesMax=new HashMap (); 50 private static Map counterCachesCurrent=new HashMap (); 51 private static Map counterCachesNext=new HashMap (); 52 53 private static Properties props; 54 55 private static final String COUNTER_CACHE_PREFFIX="Hibernate.IdGenerator."; 56 private static final String COUNTER_CACHE_POSTFIX=".CacheSize"; 57 private static final String 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 HibernateUtilities() { } 64 65 76 public static synchronized BigDecimal getNext(String 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)); counterCachesNext.put(objectName,((BigDecimal )counterCachesNext.get(objectName)).add(new BigDecimal ("1"))); 88 return (BigDecimal )counterCachesCurrent.get(objectName); 89 } catch (Exception 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 objectName) throws RootException { 96 SharkHibernateUtilityTransaction t = null; 97 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 dbNext = new BigDecimal (1); 108 BigDecimal dbLast = new BigDecimal (1); 109 if (countObj == null){ 110 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 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 e) { 135 e.printStackTrace(); 136 if (t != null) { 137 t = null; 138 } 139 } 140 } 141 throw new RootException("Can't allocate Id for object table "+objectName); 143 } 144 145 private static long getCacheSize (String objectName) { 146 long cacheSize=-1; 147 Object cs=counterCachesSizes.get(objectName); 148 if (cs==null) { 149 String propName=COUNTER_CACHE_PREFFIX+objectName+COUNTER_CACHE_POSTFIX; 150 String cSize=props.getProperty(propName); 151 String 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 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 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 (cacheSize)); 181 } else { 182 cacheSize=((Long )cs).longValue(); 183 } 184 return cacheSize; 185 } 186 187 188 196 public static synchronized void init(Properties propsToSet) throws RootException { 197 try { 198 props = propsToSet; 199 } catch (Exception e) { 200 throw new RootException("Hibernate init problem.", e); 201 } 202 } 203 } 204 205 | Popular Tags |