1 27 28 package org.objectweb.speedo.sequence.lib; 29 30 import javax.jdo.JDODataStoreException; 31 import javax.jdo.datastore.Sequence; 32 33 import org.objectweb.jorm.facility.naming.generator.LongGen; 34 import org.objectweb.jorm.facility.naming.generator.LongGenIncrAccessor; 35 import org.objectweb.speedo.pm.api.ProxyManager; 36 import org.objectweb.speedo.pm.api.ProxyManagerFactory; 37 import org.objectweb.speedo.sequence.api.SequenceManager; 38 import org.objectweb.speedo.sequence.api.SerializableSequence; 39 40 51 public class SpeedoSequence implements Sequence, SerializableSequence { 52 53 public final static byte NON_TRANSACTIONAL = 1; 54 public final static byte CONTIGUOUS = 2; 55 public final static byte TRANSACTIONAL = 3; 56 57 public final static String NON_TRANSACTIONAL_STRING = "nontransactional"; 58 public final static String CONTIGUOUS_STRING = "contiguous"; 59 public final static String TRANSACTIONAL_STRING = "transactional"; 60 61 64 public String name; 65 66 70 public String datastoreName; 71 72 76 public String factoryClass; 77 78 81 public byte strategy; 82 83 86 public String packageName; 87 88 public Integer increment = new Integer (1); 89 public Integer start; 90 public Integer cache; 91 92 private SequenceManager sequenceManager = null; 94 95 private LongGen longGen = null; 97 98 private int countDown = 0; 100 private long currentId = 0; 101 102 public LongGen getLongGen() { 103 return longGen; 104 } 105 public void setLongGen(LongGen longGen) { 106 this.longGen = longGen; 107 this.longGen.setIncrement(this.increment.intValue()); 109 } 110 111 117 public static byte strategyToByte(String s) { 118 if (s.equalsIgnoreCase(TRANSACTIONAL_STRING)) 119 return TRANSACTIONAL; 120 else if (s.equalsIgnoreCase(CONTIGUOUS_STRING)) 121 return CONTIGUOUS; 122 else 123 return NON_TRANSACTIONAL; 124 } 125 126 131 public static String strategyToString(byte b) { 132 if (b == TRANSACTIONAL) 133 return TRANSACTIONAL_STRING; 134 else if (b == CONTIGUOUS) 135 return CONTIGUOUS_STRING; 136 else 137 return NON_TRANSACTIONAL_STRING; 138 } 139 140 public SequenceManager getSequenceManager() { 141 return sequenceManager; 142 } 143 public void setSequenceManager(SequenceManager sequenceManager) { 144 this.sequenceManager = sequenceManager; 145 } 146 147 150 155 public void allocate(int additional) { 156 if (longGen != null) { 157 try { 158 currentId = longGen.allocateIds(additional) - (additional * increment.intValue()); 159 countDown += additional; 160 } catch (Exception e) { 161 return; 162 } 163 } 164 } 165 166 169 public Object current() { 170 if (longGen != null) { 171 if (checkStrategy()) { 172 if (countDown != 0) { 173 return new Long (currentId); 174 } else { 175 try { 176 return new Long (longGen.current()); 177 } catch (Exception e) { 178 throw new JDODataStoreException("Problem on current() with sequence " + name 179 + ": " + e.getMessage()); 180 } 181 } 182 } 183 } 184 throw new JDODataStoreException("Problem on current() with sequence " + name 185 + ": id generator not ready."); 186 } 187 188 191 public String getName() { 192 return (packageName == null)?(""):(packageName + ".") + name; 193 } 194 195 198 public Object next() { 199 if (longGen != null) { 200 if (checkStrategy()) { 201 if (countDown != 0) { 203 long res; 204 countDown --; 205 res = currentId; 206 currentId += increment.intValue(); 207 return new Long (res); 208 } else { 209 try { 211 return new Long (longGen.genId()); 212 } catch (Exception e) { 213 throw new JDODataStoreException("Problem on next() with sequence " + name 214 + ": " + e.getMessage()); 215 } 216 } 217 218 } 219 } 220 throw new JDODataStoreException("Problem on next() with sequence " + name 221 + ": id generator not ready."); 222 } 223 224 public long nextValue() { 225 return ((Long ) next()).longValue(); 226 } 227 228 public long currentValue() { 229 return ((Long ) current()).longValue(); 230 } 231 232 236 private boolean checkStrategy() { 237 if (strategy != NON_TRANSACTIONAL) { 238 ProxyManagerFactory pmf = sequenceManager.getPMF(); 240 ProxyManager pm = pmf.lookup(); 242 return (pm != null && pm.currentTransaction().isActive()); 244 } 245 return true; 246 } 247 } 248 | Popular Tags |