1 package org.apache.ojb.broker.util.sequence; 2 3 17 18 import org.apache.ojb.broker.PersistenceBroker; 19 import org.apache.ojb.broker.metadata.FieldDescriptor; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 87 public class SequenceManagerInMemoryImpl extends AbstractSequenceManager 88 { 89 protected static Map sequencesDBMap = new HashMap (); 90 private long sequenceStart; 91 92 public SequenceManagerInMemoryImpl(PersistenceBroker broker) 93 { 94 super(broker); 95 Long start = SequenceManagerHelper.getSeqStart(getConfigurationProperties()); 96 sequenceStart = start != null ? start.longValue() : 1; 97 } 98 99 protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException 100 { 101 String seqName = calculateSequenceName(field); 102 synchronized (SequenceManagerInMemoryImpl.class) 104 { 105 Long currentId = getSequence(seqName); 107 if (currentId == null) 109 { 110 long maxKey = SequenceManagerHelper.getMaxForExtent(getBrokerForClass(), field); 111 maxKey = sequenceStart > maxKey ? sequenceStart : maxKey; 112 currentId = new Long (maxKey); 113 } 114 currentId = new Long (currentId.longValue() + 1); 115 addSequence(seqName, currentId); 117 return currentId.intValue(); 118 } 119 } 120 121 128 private Long getSequence(String sequenceName) 129 { 130 Long result = null; 131 Map mapForDB = (Map ) sequencesDBMap.get(getBrokerForClass() 133 .serviceConnectionManager().getConnectionDescriptor().getJcdAlias()); 134 if(mapForDB != null) 135 { 136 result = (Long ) mapForDB.get(sequenceName); 137 } 138 return result; 139 } 140 141 146 private void addSequence(String sequenceName, Long seq) 147 { 148 String jcdAlias = getBrokerForClass() 150 .serviceConnectionManager().getConnectionDescriptor().getJcdAlias(); 151 Map mapForDB = (Map ) sequencesDBMap.get(jcdAlias); 152 if(mapForDB == null) 153 { 154 mapForDB = new HashMap (); 155 } 156 mapForDB.put(sequenceName, seq); 157 sequencesDBMap.put(jcdAlias, mapForDB); 158 } 159 160 165 protected void removeSequence(String sequenceName) 166 { 167 Map mapForDB = (Map ) sequencesDBMap.get(getBrokerForClass() 169 .serviceConnectionManager().getConnectionDescriptor().getJcdAlias()); 170 if(mapForDB != null) 171 { 172 synchronized(SequenceManagerInMemoryImpl.class) 173 { 174 mapForDB.remove(sequenceName); 175 } 176 } 177 } 178 } 179 | Popular Tags |