1 23 24 package org.apache.slide.store.txfile; 25 26 import java.util.HashMap ; 27 import java.util.Hashtable ; 28 import java.util.Map ; 29 30 import javax.transaction.xa.XAException ; 31 import javax.transaction.xa.XAResource ; 32 import javax.transaction.xa.Xid ; 33 34 import org.apache.commons.transaction.file.FileSequence; 35 import org.apache.commons.transaction.file.ResourceManagerException; 36 import org.apache.slide.common.ServiceAccessException; 37 import org.apache.slide.common.ServiceConnectionFailedException; 38 import org.apache.slide.common.ServiceDisconnectionFailedException; 39 import org.apache.slide.common.ServiceParameterErrorException; 40 import org.apache.slide.common.ServiceParameterMissingException; 41 import org.apache.slide.common.ServiceResetFailedException; 42 import org.apache.slide.common.AbstractServiceBase; 43 import org.apache.slide.store.SequenceStore; 44 45 import org.apache.slide.util.logger.Logger; 46 import org.apache.slide.util.logger.TxLogger; 47 48 52 public class FileSequenceStore extends AbstractServiceBase implements SequenceStore { 53 54 protected static final String LOG_CHANNEL = FileSequenceStore.class.getName(); 55 56 protected static final String STORE_DIR_PARAMETER = "rootpath"; 57 58 protected FileSequence fileSequence; 59 protected String storeDir = null; 60 protected Map sequenceHash = new HashMap (); 61 protected int sequenceIncrement = 100; 62 protected long sequenceStartValue = 1; 63 64 public void setParameters(Hashtable parameters) 65 throws ServiceParameterErrorException, ServiceParameterMissingException { 66 67 storeDir = (String ) parameters.get(STORE_DIR_PARAMETER); 68 if (storeDir == null) { 69 throw new ServiceParameterMissingException(this, STORE_DIR_PARAMETER); 70 } 71 72 try { 73 fileSequence = new FileSequence(storeDir, new TxLogger(getLogger(), LOG_CHANNEL)); 74 getLogger().log("File Sequence Store configured to " + storeDir, LOG_CHANNEL, Logger.INFO); 75 } catch (ResourceManagerException e) { 76 getLogger().log("Can not initialize File Sequence Store", e, LOG_CHANNEL, Logger.CRITICAL); 77 throw new ServiceParameterErrorException(this, STORE_DIR_PARAMETER); 78 } 79 } 80 81 public String toString() { 82 return "FileSequenceStore at " + storeDir; 83 } 84 85 88 public void connect() throws ServiceConnectionFailedException { 89 } 90 91 94 public void disconnect() throws ServiceDisconnectionFailedException { 95 } 96 97 100 public void reset() throws ServiceResetFailedException { 101 } 102 103 106 public boolean isConnected() throws ServiceAccessException { 107 return true; 108 } 109 110 113 public boolean isSequenceSupported() { 114 return true; 115 } 116 117 120 public boolean sequenceExists(String sequenceName) throws ServiceAccessException { 121 SeqContext seq = (SeqContext) sequenceHash.get(sequenceName); 122 if (seq != null) { 123 return true; 124 } else { 125 return fileSequence.exists(sequenceName); 126 } 127 } 128 129 132 public synchronized boolean createSequence(String sequenceName) throws ServiceAccessException { 133 try { 134 if (fileSequence.create(sequenceName, sequenceStartValue)) { 135 sequenceHash.put(sequenceName, new SeqContext(sequenceName, sequenceStartValue, sequenceStartValue)); 136 return true; 137 } else { 138 return false; 139 } 140 } catch (ResourceManagerException e) { 141 throw new ServiceAccessException(this, e); 142 } 143 } 144 145 148 public synchronized long nextSequenceValue(String sequenceName) throws ServiceAccessException { 149 SeqContext seq = (SeqContext) sequenceHash.get(sequenceName); 151 if (seq != null && seq.pos < seq.end - 1) { 152 seq.pos++; 153 return seq.pos; 154 } else { 155 try { 156 if (seq == null) { 157 seq = new SeqContext(sequenceName); 158 sequenceHash.put(sequenceName, seq); 159 } 160 long pos = fileSequence.nextSequenceValueBottom(sequenceName, sequenceIncrement); 161 seq.pos = pos; 162 seq.end = seq.pos + sequenceIncrement; 163 return seq.pos; 164 } catch (ResourceManagerException e) { 165 throw new ServiceAccessException(this, e); 166 } 167 } 168 } 169 170 173 public void commit(Xid xid, boolean onePhase) throws XAException { 174 } 176 177 180 public void end(Xid xid, int flags) throws XAException { 181 } 183 184 187 public void forget(Xid xid) throws XAException { 188 } 190 191 194 public int getTransactionTimeout() throws XAException { 195 return 0; 197 } 198 199 202 public boolean isSameRM(XAResource xaResource) throws XAException { 203 return (xaResource == this); 204 } 205 206 209 public int prepare(Xid xid) throws XAException { 210 return XA_RDONLY; 212 } 213 214 217 public Xid [] recover(int flag) throws XAException { 218 return null; 220 } 221 222 225 public void rollback(Xid xid) throws XAException { 226 } 228 229 232 public boolean setTransactionTimeout(int seconds) throws XAException { 233 return true; 235 } 236 237 240 public void start(Xid xid, int flags) throws XAException { 241 243 } 244 245 protected static class SeqContext { 246 public String name; 247 public long pos; 248 public long end; 249 250 public SeqContext(String name) { 251 this(name, -1, -1); 252 } 253 public SeqContext(String name, long pos, long end) { 254 this.name = name; 255 this.pos = pos; 256 this.end = end; 257 } 258 } 259 } 260 | Popular Tags |